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 not have any CTEs.
- The original query must make use of the following fact tables: ['web_sales', 'store_returns']. Below is the join graph for these fact tables: ['web_sales(ws_bill_addr_sk) references customer_address(ca_address_sk);', 'web_sales(ws_ship_mode_sk) references ship_mode(sm_ship_mode_sk);', 'web_sales(ws_ship_addr_sk) references customer_address(ca_address_sk);'] ['store_returns(sr_returned_date_sk) references date_dim(d_date_sk);', 'store_returns(sr_customer_sk) references customer(c_customer_sk);', 'store_returns(sr_store_sk) references store(s_store_sk);']
- The original query must employ 4 joins.
- The original query must employ the following aggregate functions: ['avg', 'max', 'count'].
- The original query must employ the following operators in its WHERE clause: ['max'].
- 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]
select distinct ws.ws_order_number from web_sales ws, store_returns sr, customer c, store s, date_dim d where ws.ws_order_number in ( select sr_order_number from store_returns sr2 group by sr_order_number having count(*) > 5 and max(sr2.sr_fee) > 50 ) and ws.ws_bill_customer_sk = c.c_customer_sk and sr.sr_customer_sk = c.c_customer_sk and sr.sr_store_sk = s.s_store_sk and sr.sr_returned_date_sk = d.d_date_sk and d.d_year = 2000 and s.s_state = 'CA' order by ws.ws_order_number limit 50;[REWRITTEN QUERY]
with frequent_returns as ( select sr_order_number from store_returns group by sr_order_number having count(*) > 5 and max(sr_fee) > 50 ) select distinct ws.ws_order_number from web_sales ws join customer c on ws.ws_bill_customer_sk = c.c_customer_sk join store_returns sr on sr.sr_customer_sk = c.c_customer_sk join store s on sr.sr_store_sk = s.s_store_sk join date_dim d on sr.sr_returned_date_sk = d.d_date_sk join frequent_returns fr on ws.ws_order_number = fr.sr_order_number where d.d_year = 2000 and s.s_state = 'CA' order by ws.ws_order_number 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.