Skip to content

Commit c2bcc03

Browse files
authored
adding tpcds queries (apache#15)
Signed-off-by: Yuan Zhou <yuan.zhou@intel.com> Signed-off-by: Yuan Zhou <yuan.zhou@intel.com>
1 parent d985c8d commit c2bcc03

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+3889
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--q1.sql--
2+
3+
WITH customer_total_return AS
4+
(SELECT sr_customer_sk AS ctr_customer_sk, sr_store_sk AS ctr_store_sk,
5+
sum(sr_return_amt) AS ctr_total_return
6+
FROM store_returns, date_dim
7+
WHERE sr_returned_date_sk = d_date_sk AND d_year = 2000
8+
GROUP BY sr_customer_sk, sr_store_sk)
9+
SELECT c_customer_id
10+
FROM customer_total_return ctr1, store, customer
11+
WHERE ctr1.ctr_total_return >
12+
(SELECT avg(ctr_total_return)*1.2
13+
FROM customer_total_return ctr2
14+
WHERE ctr1.ctr_store_sk = ctr2.ctr_store_sk)
15+
AND s_store_sk = ctr1.ctr_store_sk
16+
AND s_state = 'TN'
17+
AND ctr1.ctr_customer_sk = c_customer_sk
18+
ORDER BY c_customer_id LIMIT 100
19+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--q10.sql--
2+
3+
select
4+
cd_gender, cd_marital_status, cd_education_status, count(*) cnt1,
5+
cd_purchase_estimate, count(*) cnt2, cd_credit_rating, count(*) cnt3,
6+
cd_dep_count, count(*) cnt4, cd_dep_employed_count, count(*) cnt5,
7+
cd_dep_college_count, count(*) cnt6
8+
from
9+
customer c, customer_address ca, customer_demographics
10+
where
11+
c.c_current_addr_sk = ca.ca_address_sk and
12+
ca_county in ('Rush County','Toole County','Jefferson County',
13+
'Dona Ana County','La Porte County') and
14+
cd_demo_sk = c.c_current_cdemo_sk AND
15+
exists (select * from store_sales, date_dim
16+
where c.c_customer_sk = ss_customer_sk AND
17+
ss_sold_date_sk = d_date_sk AND
18+
d_year = 2002 AND
19+
d_moy between 1 AND 1+3) AND
20+
(exists (select * from web_sales, date_dim
21+
where c.c_customer_sk = ws_bill_customer_sk AND
22+
ws_sold_date_sk = d_date_sk AND
23+
d_year = 2002 AND
24+
d_moy between 1 AND 1+3) or
25+
exists (select * from catalog_sales, date_dim
26+
where c.c_customer_sk = cs_ship_customer_sk AND
27+
cs_sold_date_sk = d_date_sk AND
28+
d_year = 2002 AND
29+
d_moy between 1 AND 1+3))
30+
group by cd_gender,
31+
cd_marital_status,
32+
cd_education_status,
33+
cd_purchase_estimate,
34+
cd_credit_rating,
35+
cd_dep_count,
36+
cd_dep_employed_count,
37+
cd_dep_college_count
38+
order by cd_gender,
39+
cd_marital_status,
40+
cd_education_status,
41+
cd_purchase_estimate,
42+
cd_credit_rating,
43+
cd_dep_count,
44+
cd_dep_employed_count,
45+
cd_dep_college_count
46+
LIMIT 100
47+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--q11.sql--
2+
3+
with year_total as (
4+
select c_customer_id customer_id
5+
,c_first_name customer_first_name
6+
,c_last_name customer_last_name
7+
,c_preferred_cust_flag customer_preferred_cust_flag
8+
,c_birth_country customer_birth_country
9+
,c_login customer_login
10+
,c_email_address customer_email_address
11+
,d_year dyear
12+
,sum(ss_ext_list_price-ss_ext_discount_amt) year_total
13+
,'s' sale_type
14+
from customer, store_sales, date_dim
15+
where c_customer_sk = ss_customer_sk
16+
and ss_sold_date_sk = d_date_sk
17+
group by c_customer_id
18+
,c_first_name
19+
,c_last_name
20+
,c_preferred_cust_flag
21+
,c_birth_country
22+
,c_login
23+
,c_email_address
24+
,d_year
25+
union all
26+
select c_customer_id customer_id
27+
,c_first_name customer_first_name
28+
,c_last_name customer_last_name
29+
,c_preferred_cust_flag customer_preferred_cust_flag
30+
,c_birth_country customer_birth_country
31+
,c_login customer_login
32+
,c_email_address customer_email_address
33+
,d_year dyear
34+
,sum(ws_ext_list_price-ws_ext_discount_amt) year_total
35+
,'w' sale_type
36+
from customer, web_sales, date_dim
37+
where c_customer_sk = ws_bill_customer_sk
38+
and ws_sold_date_sk = d_date_sk
39+
group by
40+
c_customer_id, c_first_name, c_last_name, c_preferred_cust_flag, c_birth_country,
41+
c_login, c_email_address, d_year)
42+
select
43+
t_s_secyear.customer_id
44+
,t_s_secyear.customer_first_name
45+
,t_s_secyear.customer_last_name
46+
,t_s_secyear.customer_preferred_cust_flag
47+
from year_total t_s_firstyear
48+
,year_total t_s_secyear
49+
,year_total t_w_firstyear
50+
,year_total t_w_secyear
51+
where t_s_secyear.customer_id = t_s_firstyear.customer_id
52+
and t_s_firstyear.customer_id = t_w_secyear.customer_id
53+
and t_s_firstyear.customer_id = t_w_firstyear.customer_id
54+
and t_s_firstyear.sale_type = 's'
55+
and t_w_firstyear.sale_type = 'w'
56+
and t_s_secyear.sale_type = 's'
57+
and t_w_secyear.sale_type = 'w'
58+
and t_s_firstyear.dyear = 2001
59+
and t_s_secyear.dyear = 2001+1
60+
and t_w_firstyear.dyear = 2001
61+
and t_w_secyear.dyear = 2001+1
62+
and t_s_firstyear.year_total > 0
63+
and t_w_firstyear.year_total > 0
64+
and case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else 0.0 end
65+
> case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else 0.0 end
66+
order by
67+
t_s_secyear.customer_id
68+
,t_s_secyear.customer_first_name
69+
,t_s_secyear.customer_last_name
70+
,
71+
t_s_secyear.customer_preferred_cust_flag
72+
LIMIT 100
73+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--q12.sql--
2+
3+
select i_item_id,
4+
i_item_desc, i_category, i_class, i_current_price,
5+
sum(ws_ext_sales_price) as itemrevenue,
6+
sum(ws_ext_sales_price)*100/sum(sum(ws_ext_sales_price)) over
7+
(partition by i_class) as revenueratio
8+
from
9+
web_sales, item, date_dim
10+
where
11+
ws_item_sk = i_item_sk
12+
and i_category in ('Sports', 'Books', 'Home')
13+
and ws_sold_date_sk = d_date_sk
14+
and d_date between cast('1999-02-22' as date)
15+
and (cast('1999-02-22' as date) + interval '30' day)
16+
group by
17+
i_item_id, i_item_desc, i_category, i_class, i_current_price
18+
order by
19+
i_category, i_class, i_item_id, i_item_desc, revenueratio
20+
LIMIT 100
21+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--q13.sql--
2+
3+
select avg(ss_quantity)
4+
,avg(ss_ext_sales_price)
5+
,avg(ss_ext_wholesale_cost)
6+
,sum(ss_ext_wholesale_cost)
7+
from store_sales
8+
,store
9+
,customer_demographics
10+
,household_demographics
11+
,customer_address
12+
,date_dim
13+
where s_store_sk = ss_store_sk
14+
and ss_sold_date_sk = d_date_sk and d_year = 2001
15+
and((ss_hdemo_sk=hd_demo_sk
16+
and cd_demo_sk = ss_cdemo_sk
17+
and cd_marital_status = 'M'
18+
and cd_education_status = 'Advanced Degree'
19+
and ss_sales_price between 100.00 and 150.00
20+
and hd_dep_count = 3
21+
)or
22+
(ss_hdemo_sk=hd_demo_sk
23+
and cd_demo_sk = ss_cdemo_sk
24+
and cd_marital_status = 'S'
25+
and cd_education_status = 'College'
26+
and ss_sales_price between 50.00 and 100.00
27+
and hd_dep_count = 1
28+
) or
29+
(ss_hdemo_sk=hd_demo_sk
30+
and cd_demo_sk = ss_cdemo_sk
31+
and cd_marital_status = 'W'
32+
and cd_education_status = '2 yr Degree'
33+
and ss_sales_price between 150.00 and 200.00
34+
and hd_dep_count = 1
35+
))
36+
and((ss_addr_sk = ca_address_sk
37+
and ca_country = 'United States'
38+
and ca_state in ('TX', 'OH', 'TX')
39+
and ss_net_profit between 100 and 200
40+
) or
41+
(ss_addr_sk = ca_address_sk
42+
and ca_country = 'United States'
43+
and ca_state in ('OR', 'NM', 'KY')
44+
and ss_net_profit between 150 and 300
45+
) or
46+
(ss_addr_sk = ca_address_sk
47+
and ca_country = 'United States'
48+
and ca_state in ('VA', 'TX', 'MS')
49+
and ss_net_profit between 50 and 250
50+
))
51+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
--q14a.sql--
2+
3+
with cross_items as
4+
(select i_item_sk ss_item_sk
5+
from item,
6+
(select iss.i_brand_id brand_id, iss.i_class_id class_id, iss.i_category_id category_id
7+
from store_sales, item iss, date_dim d1
8+
where ss_item_sk = iss.i_item_sk
9+
and ss_sold_date_sk = d1.d_date_sk
10+
and d1.d_year between 1999 AND 1999 + 2
11+
intersect
12+
select ics.i_brand_id, ics.i_class_id, ics.i_category_id
13+
from catalog_sales, item ics, date_dim d2
14+
where cs_item_sk = ics.i_item_sk
15+
and cs_sold_date_sk = d2.d_date_sk
16+
and d2.d_year between 1999 AND 1999 + 2
17+
intersect
18+
select iws.i_brand_id, iws.i_class_id, iws.i_category_id
19+
from web_sales, item iws, date_dim d3
20+
where ws_item_sk = iws.i_item_sk
21+
and ws_sold_date_sk = d3.d_date_sk
22+
and d3.d_year between 1999 AND 1999 + 2) x
23+
where i_brand_id = brand_id
24+
and i_class_id = class_id
25+
and i_category_id = category_id
26+
),
27+
avg_sales as
28+
(select avg(quantity*list_price) average_sales
29+
from (
30+
select ss_quantity quantity, ss_list_price list_price
31+
from store_sales, date_dim
32+
where ss_sold_date_sk = d_date_sk
33+
and d_year between 1999 and 2001
34+
union all
35+
select cs_quantity quantity, cs_list_price list_price
36+
from catalog_sales, date_dim
37+
where cs_sold_date_sk = d_date_sk
38+
and d_year between 1999 and 1999 + 2
39+
union all
40+
select ws_quantity quantity, ws_list_price list_price
41+
from web_sales, date_dim
42+
where ws_sold_date_sk = d_date_sk
43+
and d_year between 1999 and 1999 + 2) x)
44+
select channel, i_brand_id,i_class_id,i_category_id,sum(sales), sum(number_sales)
45+
from(
46+
select 'store' channel, i_brand_id,i_class_id
47+
,i_category_id,sum(ss_quantity*ss_list_price) sales
48+
, count(*) number_sales
49+
from store_sales, item, date_dim
50+
where ss_item_sk in (select ss_item_sk from cross_items)
51+
and ss_item_sk = i_item_sk
52+
and ss_sold_date_sk = d_date_sk
53+
and d_year = 1999+2
54+
and d_moy = 11
55+
group by i_brand_id,i_class_id,i_category_id
56+
having sum(ss_quantity*ss_list_price) > (select average_sales from avg_sales)
57+
union all
58+
select 'catalog' channel, i_brand_id,i_class_id,i_category_id, sum(cs_quantity*cs_list_price) sales, count(*) number_sales
59+
from catalog_sales, item, date_dim
60+
where cs_item_sk in (select ss_item_sk from cross_items)
61+
and cs_item_sk = i_item_sk
62+
and cs_sold_date_sk = d_date_sk
63+
and d_year = 1999+2
64+
and d_moy = 11
65+
group by i_brand_id,i_class_id,i_category_id
66+
having sum(cs_quantity*cs_list_price) > (select average_sales from avg_sales)
67+
union all
68+
select 'web' channel, i_brand_id,i_class_id,i_category_id, sum(ws_quantity*ws_list_price) sales , count(*) number_sales
69+
from web_sales, item, date_dim
70+
where ws_item_sk in (select ss_item_sk from cross_items)
71+
and ws_item_sk = i_item_sk
72+
and ws_sold_date_sk = d_date_sk
73+
and d_year = 1999+2
74+
and d_moy = 11
75+
group by i_brand_id,i_class_id,i_category_id
76+
having sum(ws_quantity*ws_list_price) > (select average_sales from avg_sales)
77+
) y
78+
group by rollup (channel, i_brand_id,i_class_id,i_category_id)
79+
order by channel,i_brand_id,i_class_id,i_category_id
80+
limit 100
81+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--q14b.sql--
2+
3+
with cross_items as
4+
(select i_item_sk ss_item_sk
5+
from item,
6+
(select iss.i_brand_id brand_id, iss.i_class_id class_id, iss.i_category_id category_id
7+
from store_sales, item iss, date_dim d1
8+
where ss_item_sk = iss.i_item_sk
9+
and ss_sold_date_sk = d1.d_date_sk
10+
and d1.d_year between 1999 AND 1999 + 2
11+
intersect
12+
select ics.i_brand_id, ics.i_class_id, ics.i_category_id
13+
from catalog_sales, item ics, date_dim d2
14+
where cs_item_sk = ics.i_item_sk
15+
and cs_sold_date_sk = d2.d_date_sk
16+
and d2.d_year between 1999 AND 1999 + 2
17+
intersect
18+
select iws.i_brand_id, iws.i_class_id, iws.i_category_id
19+
from web_sales, item iws, date_dim d3
20+
where ws_item_sk = iws.i_item_sk
21+
and ws_sold_date_sk = d3.d_date_sk
22+
and d3.d_year between 1999 AND 1999 + 2) x
23+
where i_brand_id = brand_id
24+
and i_class_id = class_id
25+
and i_category_id = category_id
26+
),
27+
avg_sales as
28+
(select avg(quantity*list_price) average_sales
29+
from (select ss_quantity quantity, ss_list_price list_price
30+
from store_sales, date_dim
31+
where ss_sold_date_sk = d_date_sk and d_year between 1999 and 1999 + 2
32+
union all
33+
select cs_quantity quantity, cs_list_price list_price
34+
from catalog_sales, date_dim
35+
where cs_sold_date_sk = d_date_sk and d_year between 1999 and 1999 + 2
36+
union all
37+
select ws_quantity quantity, ws_list_price list_price
38+
from web_sales, date_dim
39+
where ws_sold_date_sk = d_date_sk and d_year between 1999 and 1999 + 2) x)
40+
select * from
41+
(select 'store' channel, i_brand_id,i_class_id,i_category_id
42+
,sum(ss_quantity*ss_list_price) sales, count(*) number_sales
43+
from store_sales, item, date_dim
44+
where ss_item_sk in (select ss_item_sk from cross_items)
45+
and ss_item_sk = i_item_sk
46+
and ss_sold_date_sk = d_date_sk
47+
and d_week_seq = (select d_week_seq from date_dim
48+
where d_year = 1999 + 1 and d_moy = 12 and d_dom = 11)
49+
group by i_brand_id,i_class_id,i_category_id
50+
having sum(ss_quantity*ss_list_price) > (select average_sales from avg_sales)) this_year,
51+
(select 'store' channel, i_brand_id,i_class_id
52+
,i_category_id, sum(ss_quantity*ss_list_price) sales, count(*) number_sales
53+
from store_sales, item, date_dim
54+
where ss_item_sk in (select ss_item_sk from cross_items)
55+
and ss_item_sk = i_item_sk
56+
and ss_sold_date_sk = d_date_sk
57+
and d_week_seq = (select d_week_seq from date_dim
58+
where d_year = 1999 and d_moy = 12 and d_dom = 11)
59+
group by i_brand_id,i_class_id,i_category_id
60+
having sum(ss_quantity*ss_list_price) > (select average_sales from avg_sales)) last_year
61+
where this_year.i_brand_id= last_year.i_brand_id
62+
and this_year.i_class_id = last_year.i_class_id
63+
and this_year.i_category_id = last_year.i_category_id
64+
order by this_year.channel, this_year.i_brand_id, this_year.i_class_id, this_year.i_category_id
65+
limit 100
66+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--q15.sql--
2+
3+
select ca_zip, sum(cs_sales_price)
4+
from catalog_sales, customer, customer_address, date_dim
5+
where cs_bill_customer_sk = c_customer_sk
6+
and c_current_addr_sk = ca_address_sk
7+
and ( substr(ca_zip,1,5) in ('85669', '86197','88274','83405','86475',
8+
'85392', '85460', '80348', '81792')
9+
or ca_state in ('CA','WA','GA')
10+
or cs_sales_price > 500)
11+
and cs_sold_date_sk = d_date_sk
12+
and d_qoy = 2 and d_year = 2001
13+
group by ca_zip
14+
order by ca_zip
15+
limit 100
16+

0 commit comments

Comments
 (0)