-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 9 (Case when).sql
73 lines (61 loc) · 1.85 KB
/
Day 9 (Case when).sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Case
use customer;
select * from customer;
select * from order_details;
select *,
case
when customer_id in (1,2,5) then 'IT'
when customer_id in (7,10) then 'HR'
end as deptname
from customer;
select *,
case
when customer_id in (1,2,5) then 'IT'
when customer_id in (7) then 'HR'
else 'OutofComapny'
end as deptname
from customer;
-- When country India-Globe7 ,Australia - Globe7, America - Globe9
select *,
case
when country in ('India','Australia') then 'Globe7'
when country in ('America') then 'Globe9'
end as Globe
from customer;
-- When country India-Globe7 ,Australia - OutofGlobemeeting, America - Globe9
select *,
case
when country ='India' then 'Globe7'
when country ='America' then 'Globe9'
else 'Outofglobemeeting'
end as Globe
from customer;
select * from customer;
select * from order_details;
-- Howmany customers are there in each country
select country, count(*) from customer
group by country;
-- Howmany distinct customer names are there in each country
select country, count(distinct customer_name) from customer
group by country;
-- Howmany orders do we have for each customer ? (include customer name)
SELECT c.customer_name, COUNT(o.order_id) AS order_count
FROM customer AS c
left JOIN order_details AS o ON c.customer_id = o.customer_id
GROUP BY c.customer_name;
SELECT c.customer_name, COUNT(o.order_id) AS order_count
FROM order_details AS o
right JOIN customer AS c ON c.customer_id = o.customer_id
GROUP BY c.customer_name;
-- Howmany orders we have after April 2012 include the country name
select c.country, count(o.order_id)
from order_details as o
left join customer as c
on c.customer_id = o.customer_id
where order_date >= '2012-04-01'
group by c.country
;
-- howmany contact name we have for each customer and country level
select customer_name, country, count(contact_name) as countofcontact
from customer
group by customer_name, country;