Skip to content

Commit abb887b

Browse files
authored
Last 25 queries of TPC-DS (#668)
1 parent d56b8b1 commit abb887b

24 files changed

+1102
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{% include 'header.sql.jinja' %}
2+
3+
-- NB: Subquerys
4+
-- start query 1 in stream 0 using template query76.tpl and seed 2031708268
5+
select channel, col_name, d_year, d_qoy, i_category, COUNT(*) sales_cnt, SUM(ext_sales_price) sales_amt FROM (
6+
SELECT 'store' as channel, 'ss_customer_sk' col_name, d_year, d_qoy, i_category, ss_ext_sales_price ext_sales_price
7+
FROM {{store_sales}} as store_sales
8+
cross join {{item}} as item
9+
cross join {{date_dim}} as date_dim
10+
WHERE ss_customer_sk IS NULL
11+
AND ss_sold_date_sk=d_date_sk
12+
AND ss_item_sk=i_item_sk
13+
UNION ALL
14+
SELECT 'web' as channel, 'ws_ship_addr_sk' col_name, d_year, d_qoy, i_category, ws_ext_sales_price ext_sales_price
15+
FROM {{web_sales}} as web_sales
16+
cross join {{item}} as item
17+
cross join {{date_dim}} as date_dim
18+
WHERE ws_ship_addr_sk IS NULL
19+
AND ws_sold_date_sk=d_date_sk
20+
AND ws_item_sk=i_item_sk
21+
UNION ALL
22+
SELECT 'catalog' as channel, 'cs_ship_mode_sk' col_name, d_year, d_qoy, i_category, cs_ext_sales_price ext_sales_price
23+
FROM {{catalog_sales}} as catalog_sales
24+
cross join {{item}} as item
25+
cross join {{date_dim}} as date_dim
26+
WHERE cs_ship_mode_sk IS NULL
27+
AND cs_sold_date_sk=d_date_sk
28+
AND cs_item_sk=i_item_sk) foo
29+
GROUP BY channel, col_name, d_year, d_qoy, i_category
30+
ORDER BY channel, col_name, d_year, d_qoy, i_category
31+
limit 100;
32+
33+
-- end query 1 in stream 0 using template query76.tpl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{% include 'header.sql.jinja' %}
2+
3+
-- NB: Subquerys
4+
$ss =
5+
(select store.s_store_sk s_store_sk,
6+
sum(ss_ext_sales_price) as sales,
7+
sum(ss_net_profit) as profit
8+
from {{store_sales}} as store_sales cross join
9+
{{date_dim}} as date_dim cross join
10+
{{store}} as store
11+
where ss_sold_date_sk = d_date_sk
12+
and cast(d_date as date) between cast('2000-08-16' as date)
13+
and (cast('2000-08-16' as date) + DateTime::IntervalFromDays(30))
14+
and ss_store_sk = s_store_sk
15+
group by store.s_store_sk);
16+
17+
$sr =
18+
(select store.s_store_sk s_store_sk,
19+
sum(sr_return_amt) as returns,
20+
sum(sr_net_loss) as profit_loss
21+
from {{store_returns}} as store_returns cross join
22+
{{date_dim}} as date_dim cross join
23+
{{store}} as store
24+
where sr_returned_date_sk = d_date_sk
25+
and cast(d_date as date) between cast('2000-08-16' as date)
26+
and (cast('2000-08-16' as date) + DateTime::IntervalFromDays(30))
27+
and sr_store_sk = s_store_sk
28+
group by store.s_store_sk);
29+
$cs =
30+
(select catalog_sales.cs_call_center_sk cs_call_center_sk,
31+
sum(cs_ext_sales_price) as sales,
32+
sum(cs_net_profit) as profit
33+
from {{catalog_sales}} as catalog_sales cross join
34+
{{date_dim}} as date_dim
35+
where cs_sold_date_sk = d_date_sk
36+
and cast(d_date as date) between cast('2000-08-16' as date)
37+
and (cast('2000-08-16' as date) + DateTime::IntervalFromDays(30))
38+
group by catalog_sales.cs_call_center_sk
39+
);
40+
$cr =
41+
(select catalog_returns.cr_call_center_sk cr_call_center_sk,
42+
sum(cr_return_amount) as returns,
43+
sum(cr_net_loss) as profit_loss
44+
from {{catalog_returns}} as catalog_returns cross join
45+
{{date_dim}} as date_dim
46+
where cr_returned_date_sk = d_date_sk
47+
and cast(d_date as date) between cast('2000-08-16' as date)
48+
and (cast('2000-08-16' as date) + DateTime::IntervalFromDays(30))
49+
group by catalog_returns.cr_call_center_sk
50+
);
51+
$ws =
52+
( select web_page.wp_web_page_sk wp_web_page_sk,
53+
sum(ws_ext_sales_price) as sales,
54+
sum(ws_net_profit) as profit
55+
from {{web_sales}} as web_sales cross join
56+
{{date_dim}} as date_dim cross join
57+
{{web_page}} as web_page
58+
where ws_sold_date_sk = d_date_sk
59+
and cast(d_date as date) between cast('2000-08-16' as date)
60+
and (cast('2000-08-16' as date) + DateTime::IntervalFromDays(30))
61+
and ws_web_page_sk = wp_web_page_sk
62+
group by web_page.wp_web_page_sk);
63+
$wr =
64+
(select web_page.wp_web_page_sk wp_web_page_sk,
65+
sum(wr_return_amt) as returns,
66+
sum(wr_net_loss) as profit_loss
67+
from {{web_returns}} as web_returns cross join
68+
{{date_dim}} as date_dim cross join
69+
{{web_page}} as web_page
70+
where wr_returned_date_sk = d_date_sk
71+
and cast(d_date as date) between cast('2000-08-16' as date)
72+
and (cast('2000-08-16' as date) + DateTime::IntervalFromDays(30))
73+
and wr_web_page_sk = wp_web_page_sk
74+
group by web_page.wp_web_page_sk);
75+
-- start query 1 in stream 0 using template query77.tpl and seed 1819994127
76+
select channel
77+
, id
78+
, sum(sales) as sales
79+
, sum(returns) as returns
80+
, sum(profit) as profit
81+
from
82+
(select 'store channel' as channel
83+
, ss.s_store_sk as id
84+
, sales
85+
, coalesce(returns, 0) as returns
86+
, (profit - coalesce(profit_loss,0)) as profit
87+
from $ss ss left join $sr sr
88+
on ss.s_store_sk = sr.s_store_sk
89+
union all
90+
select 'catalog channel' as channel
91+
, cs_call_center_sk as id
92+
, sales
93+
, returns
94+
, (profit - profit_loss) as profit
95+
from $cs cs
96+
cross join $cr cr
97+
union all
98+
select 'web channel' as channel
99+
, ws.wp_web_page_sk as id
100+
, sales
101+
, coalesce(returns, 0) returns
102+
, (profit - coalesce(profit_loss,0)) as profit
103+
from $ws ws left join $wr wr
104+
on ws.wp_web_page_sk = wr.wp_web_page_sk
105+
) x
106+
group by rollup (channel, id)
107+
order by channel
108+
,id
109+
limit 100;
110+
111+
-- end query 1 in stream 0 using template query77.tpl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{% include 'header.sql.jinja' %}
2+
3+
-- NB: Subquerys
4+
$ws =
5+
(select date_dim.d_year AS ws_sold_year, web_sales.ws_item_sk ws_item_sk,
6+
web_sales.ws_bill_customer_sk ws_customer_sk,
7+
sum(ws_quantity) ws_qty,
8+
sum(ws_wholesale_cost) ws_wc,
9+
sum(ws_sales_price) ws_sp
10+
from {{web_sales}} as web_sales
11+
left join {{web_returns}} as web_returns on web_returns.wr_order_number=web_sales.ws_order_number and web_sales.ws_item_sk=web_returns.wr_item_sk
12+
join {{date_dim}} as date_dim on web_sales.ws_sold_date_sk = date_dim.d_date_sk
13+
where wr_order_number is null
14+
group by date_dim.d_year, web_sales.ws_item_sk, web_sales.ws_bill_customer_sk
15+
);
16+
$cs =
17+
(select date_dim.d_year AS cs_sold_year, catalog_sales.cs_item_sk cs_item_sk,
18+
catalog_sales.cs_bill_customer_sk cs_customer_sk,
19+
sum(cs_quantity) cs_qty,
20+
sum(cs_wholesale_cost) cs_wc,
21+
sum(cs_sales_price) cs_sp
22+
from {{catalog_sales}} as catalog_sales
23+
left join {{catalog_returns}} as catalog_returns on catalog_returns.cr_order_number=catalog_sales.cs_order_number and catalog_sales.cs_item_sk=catalog_returns.cr_item_sk
24+
join {{date_dim}} as date_dim on catalog_sales.cs_sold_date_sk = date_dim.d_date_sk
25+
where cr_order_number is null
26+
group by date_dim.d_year, catalog_sales.cs_item_sk, catalog_sales.cs_bill_customer_sk
27+
);
28+
$ss=
29+
(select date_dim.d_year AS ss_sold_year, store_sales.ss_item_sk ss_item_sk,
30+
store_sales.ss_customer_sk ss_customer_sk,
31+
sum(ss_quantity) ss_qty,
32+
sum(ss_wholesale_cost) ss_wc,
33+
sum(ss_sales_price) ss_sp
34+
from {{store_sales}} as store_sales
35+
left join {{store_returns}} as store_returns on store_returns.sr_ticket_number=store_sales.ss_ticket_number and store_sales.ss_item_sk=store_returns.sr_item_sk
36+
join {{date_dim}} as date_dim on store_sales.ss_sold_date_sk = date_dim.d_date_sk
37+
where sr_ticket_number is null
38+
group by date_dim.d_year, store_sales.ss_item_sk, store_sales.ss_customer_sk
39+
);
40+
-- start query 1 in stream 0 using template query78.tpl and seed 1819994127
41+
select
42+
ss_customer_sk,
43+
Math::Round(cast(ss_qty as double)/(coalesce(ws_qty,0)+coalesce(cs_qty,0)),-2) ratio,
44+
ss_qty store_qty, ss_wc store_wholesale_cost, ss_sp store_sales_price,
45+
coalesce(ws_qty,0)+coalesce(cs_qty,0) other_chan_qty,
46+
coalesce(ws_wc,0)+coalesce(cs_wc,0) other_chan_wholesale_cost,
47+
coalesce(ws_sp,0)+coalesce(cs_sp,0) other_chan_sales_price
48+
from $ss ss
49+
left join $ws ws on (ws.ws_sold_year=ss.ss_sold_year and ws.ws_item_sk=ss.ss_item_sk and ws.ws_customer_sk=ss.ss_customer_sk)
50+
left join $cs cs on (cs.cs_sold_year=ss.ss_sold_year and cs.cs_item_sk=ss.ss_item_sk and cs.cs_customer_sk=ss.ss_customer_sk)
51+
where (coalesce(ws_qty,0)>0 or coalesce(cs_qty, 0)>0) and ss_sold_year=2001
52+
order by
53+
ss_customer_sk,
54+
store_qty desc, store_wholesale_cost desc, store_sales_price desc,
55+
other_chan_qty,
56+
other_chan_wholesale_cost,
57+
other_chan_sales_price,
58+
ratio
59+
limit 100;
60+
61+
-- end query 1 in stream 0 using template query78.tpl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{% include 'header.sql.jinja' %}
2+
3+
-- NB: Subquerys
4+
-- start query 1 in stream 0 using template query79.tpl and seed 2031708268
5+
select
6+
c_last_name,c_first_name,substring(cast(s_city as string),1,30) bla,ss_ticket_number,amt,profit
7+
from
8+
(select store_sales.ss_ticket_number ss_ticket_number
9+
,store_sales.ss_customer_sk ss_customer_sk
10+
,store.s_city s_city
11+
,sum(ss_coupon_amt) amt
12+
,sum(ss_net_profit) profit
13+
from {{store_sales}} as store_sales
14+
cross join {{date_dim}} as date_dim
15+
cross join {{store}} as store
16+
cross join {{household_demographics}} as household_demographics
17+
where store_sales.ss_sold_date_sk = date_dim.d_date_sk
18+
and store_sales.ss_store_sk = store.s_store_sk
19+
and store_sales.ss_hdemo_sk = household_demographics.hd_demo_sk
20+
and (household_demographics.hd_dep_count = 0 or household_demographics.hd_vehicle_count > 3)
21+
and date_dim.d_dow = 1
22+
and date_dim.d_year in (1998,1998+1,1998+2)
23+
and store.s_number_employees between 200 and 295
24+
group by store_sales.ss_ticket_number,store_sales.ss_customer_sk,store_sales.ss_addr_sk,store.s_city) ms cross join {{customer}} as customer
25+
where ss_customer_sk = c_customer_sk
26+
order by c_last_name,c_first_name,bla, profit
27+
limit 100;
28+
29+
-- end query 1 in stream 0 using template query79.tpl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{% include 'header.sql.jinja' %}
2+
3+
-- NB: Subquerys
4+
$ssr =
5+
(select store.s_store_id as store_id,
6+
sum(ss_ext_sales_price) as sales,
7+
sum(coalesce(sr_return_amt, 0)) as returns,
8+
sum(ss_net_profit - coalesce(sr_net_loss, 0)) as profit
9+
from {{store_sales}} as store_sales
10+
left join {{store_returns}} as store_returns on
11+
(store_sales.ss_item_sk = store_returns.sr_item_sk and store_sales.ss_ticket_number = store_returns.sr_ticket_number) cross join
12+
{{date_dim}} as date_dim cross join
13+
{{store}} as store cross join
14+
{{item}} as item cross join
15+
{{promotion}} as promotion
16+
where ss_sold_date_sk = d_date_sk
17+
and cast(d_date as date) between cast('2002-08-06' as date)
18+
and (cast('2002-08-06' as date) + DateTime::IntervalFromDays(30))
19+
and ss_store_sk = s_store_sk
20+
and ss_item_sk = i_item_sk
21+
and i_current_price > 50
22+
and ss_promo_sk = p_promo_sk
23+
and p_channel_tv = 'N'
24+
group by store.s_store_id);
25+
$csr=
26+
(select catalog_page.cp_catalog_page_id as catalog_page_id,
27+
sum(cs_ext_sales_price) as sales,
28+
sum(coalesce(cr_return_amount, 0)) as returns,
29+
sum(cs_net_profit - coalesce(cr_net_loss, 0)) as profit
30+
from {{catalog_sales}} as catalog_sales left join {{catalog_returns}} as catalog_returns on
31+
(catalog_sales.cs_item_sk = catalog_returns.cr_item_sk and catalog_sales.cs_order_number = catalog_returns.cr_order_number) cross join
32+
{{date_dim}} as date_dim cross join
33+
{{catalog_page}} as catalog_page cross join
34+
{{item}} as item cross join
35+
{{promotion}} as promotion
36+
where cs_sold_date_sk = d_date_sk
37+
and cast(d_date as date) between cast('2002-08-06' as date)
38+
and (cast('2002-08-06' as date) + DateTime::IntervalFromDays(30))
39+
and cs_catalog_page_sk = cp_catalog_page_sk
40+
and cs_item_sk = i_item_sk
41+
and i_current_price > 50
42+
and cs_promo_sk = p_promo_sk
43+
and p_channel_tv = 'N'
44+
group by catalog_page.cp_catalog_page_id)
45+
;
46+
$wsr =
47+
(select web_site.web_site_id web_site_id,
48+
sum(ws_ext_sales_price) as sales,
49+
sum(coalesce(wr_return_amt, 0)) as returns,
50+
sum(ws_net_profit - coalesce(wr_net_loss, 0)) as profit
51+
from {{web_sales}} as web_sales
52+
left outer join {{web_returns}} as web_returns on
53+
(web_sales.ws_item_sk = web_returns.wr_item_sk and web_sales.ws_order_number = web_returns.wr_order_number) cross join
54+
{{date_dim}} as date_dim cross join
55+
{{web_site}} as web_site cross join
56+
{{item}} as item cross join
57+
{{promotion}} as promotion
58+
where ws_sold_date_sk = d_date_sk
59+
and cast(d_date as date) between cast('2002-08-06' as date)
60+
and (cast('2002-08-06' as date) + DateTime::IntervalFromDays(30))
61+
and ws_web_site_sk = web_site_sk
62+
and ws_item_sk = i_item_sk
63+
and i_current_price > 50
64+
and ws_promo_sk = p_promo_sk
65+
and p_channel_tv = 'N'
66+
group by web_site.web_site_id);
67+
-- start query 1 in stream 0 using template query80.tpl and seed 1819994127
68+
select channel
69+
, id
70+
, sum(sales) as sales
71+
, sum(returns) as returns
72+
, sum(profit) as profit
73+
from
74+
(select 'store channel' as channel
75+
, 'store' || store_id as id
76+
, sales
77+
, returns
78+
, profit
79+
from $ssr ssr
80+
union all
81+
select 'catalog channel' as channel
82+
, 'catalog_page' || catalog_page_id as id
83+
, sales
84+
, returns
85+
, profit
86+
from $csr csr
87+
union all
88+
select 'web channel' as channel
89+
, 'web_site' || web_site_id as id
90+
, sales
91+
, returns
92+
, profit
93+
from $wsr wsr
94+
) x
95+
group by rollup (channel, id)
96+
order by channel
97+
,id
98+
limit 100;
99+
100+
-- end query 1 in stream 0 using template query80.tpl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{% include 'header.sql.jinja' %}
2+
3+
-- NB: Subquerys
4+
$customer_total_return =
5+
(select catalog_returns.cr_returning_customer_sk as ctr_customer_sk
6+
,customer_address.ca_state as ctr_state,
7+
sum(cr_return_amt_inc_tax) as ctr_total_return
8+
from {{catalog_returns}} as catalog_returns
9+
cross join {{date_dim}} as date_dim
10+
cross join {{customer_address}} as customer_address
11+
where cr_returned_date_sk = d_date_sk
12+
and d_year =1998
13+
and cr_returning_addr_sk = ca_address_sk
14+
group by catalog_returns.cr_returning_customer_sk
15+
,customer_address.ca_state );
16+
$avg_ctr_total_return = (select ctr_state, avg(ctr_total_return) as ctr_total_return from $customer_total_return group by ctr_state);
17+
-- start query 1 in stream 0 using template query81.tpl and seed 1819994127
18+
select c_customer_id,c_salutation,c_first_name,c_last_name,ca_street_number,ca_street_name
19+
,ca_street_type,ca_suite_number,ca_city,ca_county,ca_state,ca_zip,ca_country,ca_gmt_offset
20+
,ca_location_type,ctr1.ctr_total_return
21+
from $customer_total_return ctr1
22+
join $avg_ctr_total_return ctr2 on (ctr1.ctr_state = ctr2.ctr_state)
23+
cross join {{customer_address}} as customer_address
24+
cross join {{customer}} as customer
25+
where ctr1.ctr_total_return > ctr2.ctr_total_return*1.2
26+
and ca_address_sk = c_current_addr_sk
27+
and ca_state = 'TX'
28+
and ctr1.ctr_customer_sk = c_customer_sk
29+
order by c_customer_id,c_salutation,c_first_name,c_last_name,ca_street_number,ca_street_name
30+
,ca_street_type,ca_suite_number,ca_city,ca_county,ca_state,ca_zip,ca_country,ca_gmt_offset
31+
,ca_location_type,ctr1.ctr_total_return
32+
limit 100;
33+
34+
-- end query 1 in stream 0 using template query81.tpl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{% include 'header.sql.jinja' %}
2+
3+
-- NB: Subquerys
4+
-- start query 1 in stream 0 using template query82.tpl and seed 55585014
5+
select item.i_item_id
6+
,item.i_item_desc
7+
,item.i_current_price
8+
from {{item}} as item
9+
cross join {{inventory}} as inventory
10+
cross join {{date_dim}} as date_dim
11+
cross join {{store_sales}} as store_sales
12+
where i_current_price between 49 and 49+30
13+
and inv_item_sk = i_item_sk
14+
and d_date_sk=inv_date_sk
15+
and cast(d_date as date) between cast('2001-01-28' as date) and (cast('2001-01-28' as date) + DateTime::IntervalFromDays(60))
16+
and i_manufact_id in (80,675,292,17)
17+
and inv_quantity_on_hand between 100 and 500
18+
and ss_item_sk = i_item_sk
19+
group by item.i_item_id,item.i_item_desc,item.i_current_price
20+
order by item.i_item_id
21+
limit 100;
22+
23+
-- end query 1 in stream 0 using template query82.tpl

0 commit comments

Comments
 (0)