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: ['web_sales', 'web_sales', 'catalog_sales']. Below is the join graph for these fact tables: ['web_sales(ws_promo_sk) references promotion(p_promo_sk);', 'web_sales(ws_sold_time_sk) references time_dim(t_time_sk);', 'web_sales(ws_warehouse_sk) references warehouse(w_warehouse_sk);'] ['web_sales(ws_sold_date_sk) references date_dim(d_date_sk);', 'web_sales(ws_ship_date_sk) references date_dim(d_date_sk);', 'web_sales(ws_ship_cdemo_sk) references customer_demographics(cd_demo_sk);'] ['catalog_sales(cs_sold_time_sk) references time_dim(t_time_sk);', 'catalog_sales(cs_ship_mode_sk) references ship_mode(sm_ship_mode_sk);', 'catalog_sales(cs_bill_cdemo_sk) references customer_demographics(cd_demo_sk);']
- The original query must employ 3 joins.
- The original query must employ the following aggregate functions: ['avg', 'sum'].
- The original query must employ the following operators in its WHERE clause: [].
- 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 .
- Assistant
[ORIGINAL QUERY]
with sales_summary as ( select ws_sold_date_sk as sold_date_sk, ws_warehouse_sk as warehouse_sk, sum(ws_ext_sales_price) as total_sales from web_sales where ws_sold_date_sk in (select d_date_sk from date_dim where d_year = 2001) group by ws_sold_date_sk, ws_warehouse_sk ), catalog_summary as ( select cs_sold_time_sk as sold_time_sk, cs_bill_cdemo_sk as demo_sk, avg(cs_ext_list_price) as avg_price from catalog_sales where cs_sold_time_sk in (select t_time_sk from time_dim where t_hour between 8 and 16) group by cs_sold_time_sk, cs_bill_cdemo_sk ) select w_warehouse_name from sales_summary ss join warehouse w on ss.warehouse_sk = w.w_warehouse_sk join catalog_summary cs on ss.sold_date_sk = cs.sold_time_sk where ss.total_sales > (select avg(total_sales) * 1.1 from sales_summary where warehouse_sk = ss.warehouse_sk) order by w_warehouse_name limit 50;
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.