-
Notifications
You must be signed in to change notification settings - Fork 10
/
q60.sql
62 lines (62 loc) · 1.55 KB
/
q60.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
WITH ss AS (
SELECT
i_item_id,
sum(ss_ext_sales_price) total_sales
FROM store_sales, date_dim, customer_address, item
WHERE
i_item_id IN (SELECT i_item_id
FROM item
WHERE i_category IN ('Music'))
AND ss_item_sk = i_item_sk
AND ss_sold_date_sk = d_date_sk
AND d_year = 1998
AND d_moy = 9
AND ss_addr_sk = ca_address_sk
AND ca_gmt_offset = -5
GROUP BY i_item_id),
cs AS (
SELECT
i_item_id,
sum(cs_ext_sales_price) total_sales
FROM catalog_sales, date_dim, customer_address, item
WHERE
i_item_id IN (SELECT i_item_id
FROM item
WHERE i_category IN ('Music'))
AND cs_item_sk = i_item_sk
AND cs_sold_date_sk = d_date_sk
AND d_year = 1998
AND d_moy = 9
AND cs_bill_addr_sk = ca_address_sk
AND ca_gmt_offset = -5
GROUP BY i_item_id),
ws AS (
SELECT
i_item_id,
sum(ws_ext_sales_price) total_sales
FROM web_sales, date_dim, customer_address, item
WHERE
i_item_id IN (SELECT i_item_id
FROM item
WHERE i_category IN ('Music'))
AND ws_item_sk = i_item_sk
AND ws_sold_date_sk = d_date_sk
AND d_year = 1998
AND d_moy = 9
AND ws_bill_addr_sk = ca_address_sk
AND ca_gmt_offset = -5
GROUP BY i_item_id)
SELECT
i_item_id,
sum(total_sales) total_sales
FROM (SELECT *
FROM ss
UNION ALL
SELECT *
FROM cs
UNION ALL
SELECT *
FROM ws) tmp1
GROUP BY i_item_id
ORDER BY i_item_id, total_sales
LIMIT 100;