-
Notifications
You must be signed in to change notification settings - Fork 0
/
warehouse SQL aggregations.sql
56 lines (38 loc) · 1.62 KB
/
warehouse SQL aggregations.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
/* USING THE AGGREGATE FUNCTIONS */
-- what is the total retail sales for each supplier ?
select supplier, sum(retailsales) as total_retail_sales
from project
group by supplier;
-- how many distinct item codes are in the dataset?
select count(distinct itemcode) as distinct_item_codes
from project;
-- what is the total retail sales for each item type
select itemtype, sum(retailsales) as total_retail_sales
from project
group by itemtype;
-- what is the total retail sales for each combination of supplier and month
select supplier, year, month, sum(retailsales) as total_retail_sales
from project
group by supplier, year, month;
-- what is the maximum warehouse sales for each item description?
select itemdescription, max(retailsales) as max_warehouse_sales
from project
group by itemdescription;
-- what is the average retail transfer for each year
select year, avg(retailtransfers) as avg_retail_transfers
from project
group by year;
/* for each item description, what is the difference between the maximum and minimum retail sales? */
select itemdescription, max(retailsales) - min(retailsales) as diff_max_min_retail_sales
from project
group by itemdescription;
/* what is the total retail sales for each item type where the retail sales exceed 1000?
*/
select itemtype, sum(retailsales) as total_retail_sales
from project
where retailsales > 1000
group by itemtype;
/* what is the average daily retail sales for each combination of supplier and item code? */
select supplier, itemcode, avg(retailsales) as avg_daily_retail_sales
from project
group by supplier, itemcode;