-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarizing_data.sql
More file actions
49 lines (39 loc) · 1.06 KB
/
summarizing_data.sql
File metadata and controls
49 lines (39 loc) · 1.06 KB
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
use sql_invoicing;
-- Aggregate functions
select
max(invoice_total),
min(invoice_total),
avg(invoice_total),
sum(invoice_total * 10),
count(invoice_total), -- count() only returns non null values.
count(*), -- includes null values.
count(distinct client_id)
from invoices
where invoice_date >'2019-07-01';
-- Group by clause
select
c.state,
c.city,
sum(invoice_total) as total_sales
from invoices i
join clients c
using(client_id)
group by state,city;
-- Having clause (used to filter data after grouping data and we can include only columns that are retrived using group by)
select
client_id,
sum(invoice_total) as total_sales,
count(*) as number_of_invoices
from invoices
group by client_id
having total_sales> 800
and number_of_invoices > 5;
-- roll up operator(applies only to columns that aggregate values)
select
state,
city,
sum(invoice_total) as total_sales
from invoices i
join clients c
using (client_id)
group by state,city with rollup -- (roll up calculates aggregate for each group and then total)