forked from agirish/tpcds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query39.sql
117 lines (116 loc) · 3.6 KB
/
query39.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
-- start query 39 in stream 0 using template query39.tpl
WITH inv
AS (SELECT w_warehouse_name,
w_warehouse_sk,
i_item_sk,
d_moy,
stdev,
mean,
CASE mean
WHEN 0 THEN NULL
ELSE stdev / mean
END cov
FROM (SELECT w_warehouse_name,
w_warehouse_sk,
i_item_sk,
d_moy,
Stddev_samp(inv_quantity_on_hand) stdev,
Avg(inv_quantity_on_hand) mean
FROM inventory,
item,
warehouse,
date_dim
WHERE inv_item_sk = i_item_sk
AND inv_warehouse_sk = w_warehouse_sk
AND inv_date_sk = d_date_sk
AND d_year = 2002
GROUP BY w_warehouse_name,
w_warehouse_sk,
i_item_sk,
d_moy) foo
WHERE CASE mean
WHEN 0 THEN 0
ELSE stdev / mean
END > 1)
SELECT inv1.w_warehouse_sk,
inv1.i_item_sk,
inv1.d_moy,
inv1.mean,
inv1.cov,
inv2.w_warehouse_sk,
inv2.i_item_sk,
inv2.d_moy,
inv2.mean,
inv2.cov
FROM inv inv1,
inv inv2
WHERE inv1.i_item_sk = inv2.i_item_sk
AND inv1.w_warehouse_sk = inv2.w_warehouse_sk
AND inv1.d_moy = 1
AND inv2.d_moy = 1 + 1
ORDER BY inv1.w_warehouse_sk,
inv1.i_item_sk,
inv1.d_moy,
inv1.mean,
inv1.cov,
inv2.d_moy,
inv2.mean,
inv2.cov;
WITH inv
AS (SELECT w_warehouse_name,
w_warehouse_sk,
i_item_sk,
d_moy,
stdev,
mean,
CASE mean
WHEN 0 THEN NULL
ELSE stdev / mean
END cov
FROM (SELECT w_warehouse_name,
w_warehouse_sk,
i_item_sk,
d_moy,
Stddev_samp(inv_quantity_on_hand) stdev,
Avg(inv_quantity_on_hand) mean
FROM inventory,
item,
warehouse,
date_dim
WHERE inv_item_sk = i_item_sk
AND inv_warehouse_sk = w_warehouse_sk
AND inv_date_sk = d_date_sk
AND d_year = 2002
GROUP BY w_warehouse_name,
w_warehouse_sk,
i_item_sk,
d_moy) foo
WHERE CASE mean
WHEN 0 THEN 0
ELSE stdev / mean
END > 1)
SELECT inv1.w_warehouse_sk,
inv1.i_item_sk,
inv1.d_moy,
inv1.mean,
inv1.cov,
inv2.w_warehouse_sk,
inv2.i_item_sk,
inv2.d_moy,
inv2.mean,
inv2.cov
FROM inv inv1,
inv inv2
WHERE inv1.i_item_sk = inv2.i_item_sk
AND inv1.w_warehouse_sk = inv2.w_warehouse_sk
AND inv1.d_moy = 1
AND inv2.d_moy = 1 + 1
AND inv1.cov > 1.5
ORDER BY inv1.w_warehouse_sk,
inv1.i_item_sk,
inv1.d_moy,
inv1.mean,
inv1.cov,
inv2.d_moy,
inv2.mean,
inv2.cov;