-
Notifications
You must be signed in to change notification settings - Fork 32
/
query24.sql
96 lines (95 loc) · 2.99 KB
/
query24.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
-- start query 24 in stream 0 using template query24.tpl
WITH ssales
AS (SELECT c_last_name,
c_first_name,
s_store_name,
ca_state,
s_state,
i_color,
i_current_price,
i_manager_id,
i_units,
i_size,
Sum(ss_net_profit) netpaid
FROM store_sales,
store_returns,
store,
item,
customer,
customer_address
WHERE ss_ticket_number = sr_ticket_number
AND ss_item_sk = sr_item_sk
AND ss_customer_sk = c_customer_sk
AND ss_item_sk = i_item_sk
AND ss_store_sk = s_store_sk
AND c_birth_country = Upper(ca_country)
AND s_zip = ca_zip
AND s_market_id = 6
GROUP BY c_last_name,
c_first_name,
s_store_name,
ca_state,
s_state,
i_color,
i_current_price,
i_manager_id,
i_units,
i_size)
SELECT c_last_name,
c_first_name,
s_store_name,
Sum(netpaid) paid
FROM ssales
WHERE i_color = 'papaya'
GROUP BY c_last_name,
c_first_name,
s_store_name
HAVING Sum(netpaid) > (SELECT 0.05 * Avg(netpaid)
FROM ssales);
WITH ssales
AS (SELECT c_last_name,
c_first_name,
s_store_name,
ca_state,
s_state,
i_color,
i_current_price,
i_manager_id,
i_units,
i_size,
Sum(ss_net_profit) netpaid
FROM store_sales,
store_returns,
store,
item,
customer,
customer_address
WHERE ss_ticket_number = sr_ticket_number
AND ss_item_sk = sr_item_sk
AND ss_customer_sk = c_customer_sk
AND ss_item_sk = i_item_sk
AND ss_store_sk = s_store_sk
AND c_birth_country = Upper(ca_country)
AND s_zip = ca_zip
AND s_market_id = 6
GROUP BY c_last_name,
c_first_name,
s_store_name,
ca_state,
s_state,
i_color,
i_current_price,
i_manager_id,
i_units,
i_size)
SELECT c_last_name,
c_first_name,
s_store_name,
Sum(netpaid) paid
FROM ssales
WHERE i_color = 'chartreuse'
GROUP BY c_last_name,
c_first_name,
s_store_name
HAVING Sum(netpaid) > (SELECT 0.05 * Avg(netpaid)
FROM ssales);