You are a database expert and SQL optimizer. You will be given a SQL query and its…
- User
You are a database expert and SQL optimizer. You will be given a SQL query and its rewritten variant. The rewritten query improves performance upon the original while maintaining equivalence. Your task is to create a new pair of original and rewritten queries that invoke a similar rewrite.
[ORIGINAL QUERY]
with customer_total_return as (select sr_customer_sk as ctr_customer_sk,sr_store_sk as ctr_store_sk,sum(SR_FEE) as ctr_total_return from store_returns,date_dim where sr_returned_date_sk = d_date_sk and d_year =2000 group by sr_customer_sk,sr_store_sk) select c_customer_id from customer_total_return ctr1,store,customer where ctr1.ctr_total_return > (select avg(ctr_total_return)*1.2 from customer_total_return ctr2 where ctr1.ctr_store_sk = ctr2.ctr_store_sk) and s_store_sk = ctr1.ctr_store_sk and s_state = 'TN' and ctr1.ctr_customer_sk = c_customer_sk order by c_customer_id limit 100;[REWRITTEN QUERY]
with customer_total_return as ( select sr_customer_sk as ctr_customer_sk, sr_store_sk as ctr_store_sk, sum(SR_FEE) as ctr_total_return from store_returns join date_dim on sr_returned_date_sk = d_date_sk where d_year = 2000 group by sr_customer_sk , sr_store_sk ), avg_customer_return as ( select ctr_store_sk, avg(ctr_total_return) as avg_return from customer_total_return group by ctr_store_sk ) select c_customer_id from customer_total_return ctr1 join store on s_store_sk = ctr1.ctr_store_sk join customer on ctr1.ctr_customer_sk = c_customer_sk join avg_customer_return acr on ctr1.ctr_store_sk = acr.ctr_store_sk where ctr1.ctr_total_return > acr.avg_return * 1.2 and s_state = 'TN' order by c_customer_id limit 100;Now, consider the TPC-DS schema and create a new original and equivalent rewritten query pair such that the rewrite operation is similar to the provided demonstration.Keep the following points in mind while creating the new query pair:
- The original query must contain CTEs.
- The original query must make use of the following fact tables: ['store_returns', 'web_sales']. Below is the join graph for these fact tables: ['store_returns(sr_store_sk) references store(s_store_sk);', 'store_returns(sr_return_time_sk) references time_dim(t_time_sk);', 'store_returns(sr_returned_date_sk) references date_dim(d_date_sk);'] ['web_sales(ws_sold_time_sk) references time_dim(t_time_sk);', 'web_sales(ws_bill_customer_sk) references customer(c_customer_sk);', 'web_sales(ws_bill_cdemo_sk) references customer_demographics(cd_demo_sk);']
- The original query must employ 4 joins.
- The original query must employ the following aggregate functions: ['min', 'sum', 'avg'].
- The original query must employ the following operators in its WHERE clause: ['avg', 'min', 'count'].
- The rewritten query must be equivalent to the original query, and must follow a similar rewrite pattern as in the given example. Enclose the original and rewritten queries within
and.
- Assistant
[ORIGINAL QUERY]
with store_return_agg as ( select sr_customer_sk as sr_cust_sk, sr_store_sk as sr_store_sk, min(sr_return_amt) as min_return_amt, sum(sr_return_tax) as total_return_tax from store_returns join date_dim on sr_returned_date_sk = d_date_sk where d_year = 2001 group by sr_customer_sk, sr_store_sk ), web_sales_agg as ( select ws_bill_customer_sk as ws_cust_sk, ws_sold_time_sk as ws_time_sk, avg(ws_ext_discount_amt) as avg_discount from web_sales join time_dim on ws_sold_time_sk = t_time_sk group by ws_bill_customer_sk, ws_sold_time_sk ) select sr_cust_sk, sr_store_sk from store_return_agg sra join store on s_store_sk = sra.sr_store_sk join customer on sra.sr_cust_sk = c_customer_sk where total_return_tax > ( select avg(total_return_tax) * 1.1 from store_return_agg sra2 where sra.sr_store_sk = sra2.sr_store_sk ) and avg_discount < ( select min(avg_discount) from web_sales_agg wsa2 where sra.sr_cust_sk = wsa2.ws_cust_sk ) order by sr_cust_sk limit 50;[REWRITTEN QUERY]
with store_return_agg as ( select sr_customer_sk as sr_cust_sk, sr_store_sk as sr_store_sk, min(sr_return_amt) as min_return_amt, sum(sr_return_tax) as total_return_tax from store_returns join date_dim on sr_returned_date_sk = d_date_sk where d_year = 2001 group by sr_customer_sk, sr_store_sk ), web_sales_agg as ( select ws_bill_customer_sk as ws_cust_sk, ws_sold_time_sk as ws_time_sk, avg(ws_ext_discount_amt) as avg_discount from web_sales join time_dim on ws_sold_time_sk = t_time_sk group by ws_bill_customer_sk, ws_sold_time_sk ), avg_return_tax as ( select sr_store_sk, avg(total_return_tax) as avg_tax from store_return_agg group by sr_store_sk ), min_avg_discount as ( select ws_cust_sk, min(avg_discount) as min_avg_discount from web_sales_agg group by ws_cust_sk ) select sr_cust_sk, sr_store_sk from store_return_agg sra join store on s_store_sk = sra.sr_store_sk join customer on sra.sr_cust_sk = c_customer_sk join avg_return_tax art on sra.sr_store_sk = art.sr_store_sk join min_avg_discount mad on sra.sr_cust_sk = mad.ws_cust_sk where total_return_tax > art.avg_tax * 1.1 and avg_discount < mad.min_avg_discount order by sr_cust_sk limit 50;