forked from agirish/tpcds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query58.sql
72 lines (72 loc) · 3.2 KB
/
query58.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
-- start query 58 in stream 0 using template query58.tpl
WITH ss_items
AS (SELECT i_item_id item_id,
Sum(ss_ext_sales_price) ss_item_rev
FROM store_sales,
item,
date_dim
WHERE ss_item_sk = i_item_sk
AND d_date IN (SELECT d_date
FROM date_dim
WHERE d_week_seq = (SELECT d_week_seq
FROM date_dim
WHERE d_date = '2002-02-25'
))
AND ss_sold_date_sk = d_date_sk
GROUP BY i_item_id),
cs_items
AS (SELECT i_item_id item_id,
Sum(cs_ext_sales_price) cs_item_rev
FROM catalog_sales,
item,
date_dim
WHERE cs_item_sk = i_item_sk
AND d_date IN (SELECT d_date
FROM date_dim
WHERE d_week_seq = (SELECT d_week_seq
FROM date_dim
WHERE d_date = '2002-02-25'
))
AND cs_sold_date_sk = d_date_sk
GROUP BY i_item_id),
ws_items
AS (SELECT i_item_id item_id,
Sum(ws_ext_sales_price) ws_item_rev
FROM web_sales,
item,
date_dim
WHERE ws_item_sk = i_item_sk
AND d_date IN (SELECT d_date
FROM date_dim
WHERE d_week_seq = (SELECT d_week_seq
FROM date_dim
WHERE d_date = '2002-02-25'
))
AND ws_sold_date_sk = d_date_sk
GROUP BY i_item_id)
SELECT ss_items.item_id,
ss_item_rev,
ss_item_rev / ( ss_item_rev + cs_item_rev + ws_item_rev ) / 3 *
100 ss_dev,
cs_item_rev,
cs_item_rev / ( ss_item_rev + cs_item_rev + ws_item_rev ) / 3 *
100 cs_dev,
ws_item_rev,
ws_item_rev / ( ss_item_rev + cs_item_rev + ws_item_rev ) / 3 *
100 ws_dev,
( ss_item_rev + cs_item_rev + ws_item_rev ) / 3
average
FROM ss_items,
cs_items,
ws_items
WHERE ss_items.item_id = cs_items.item_id
AND ss_items.item_id = ws_items.item_id
AND ss_item_rev BETWEEN 0.9 * cs_item_rev AND 1.1 * cs_item_rev
AND ss_item_rev BETWEEN 0.9 * ws_item_rev AND 1.1 * ws_item_rev
AND cs_item_rev BETWEEN 0.9 * ss_item_rev AND 1.1 * ss_item_rev
AND cs_item_rev BETWEEN 0.9 * ws_item_rev AND 1.1 * ws_item_rev
AND ws_item_rev BETWEEN 0.9 * ss_item_rev AND 1.1 * ss_item_rev
AND ws_item_rev BETWEEN 0.9 * cs_item_rev AND 1.1 * cs_item_rev
ORDER BY item_id,
ss_item_rev
LIMIT 100;