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: ['store_returns']. Below is the join graph for these fact tables: ['store_returns(sr_cdemo_sk) references customer_demographics(cd_demo_sk);', 'store_returns(sr_return_time_sk) references time_dim(t_time_sk);', 'store_returns(sr_store_sk) references store(s_store_sk);']
- The original query must employ 3 joins.
- The original query must employ the following aggregate functions: ['max', 'avg'].
- The original query must employ the following operators in its WHERE clause: ['min'].
- 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 c_customer_id, s_city, s_state FROM store_returns sr, customer_demographics cd, store s WHERE sr.sr_cdemo_sk = cd.cd_demo_sk AND sr.sr_store_sk = s.s_store_sk AND sr_return_amt > ( SELECT AVG(sr_return_amt) * 1.1 FROM store_returns sr_inner WHERE sr_inner.sr_store_sk = sr.sr_store_sk AND sr_return_amt > ( SELECT MIN(sr_return_amt) FROM store_returns sr_min WHERE sr_min.sr_store_sk = sr.sr_store_sk ) ) AND cd.cd_education_status = 'Advanced Degree' AND s.s_state = 'CA' ORDER BY c_customer_id LIMIT 50;[REWRITTEN QUERY]
WITH avg_store_return AS ( SELECT sr_store_sk, AVG(sr_return_amt) * 1.1 AS avg_return_threshold FROM store_returns GROUP BY sr_store_sk ), min_store_return AS ( SELECT sr_store_sk, MIN(sr_return_amt) AS min_return_amt FROM store_returns GROUP BY sr_store_sk ), filtered_returns AS ( SELECT sr.sr_customer_sk AS customer_sk, sr.sr_store_sk AS store_sk, sr.sr_return_amt FROM store_returns sr JOIN min_store_return m_min ON sr.sr_store_sk = m_min.sr_store_sk WHERE sr.sr_return_amt > m_min.min_return_amt ) SELECT c_customer_id, s_city, s_state FROM filtered_returns fr JOIN avg_store_return a_avg ON fr.store_sk = a_avg.sr_store_sk JOIN customer_demographics cd ON fr.customer_sk = cd.cd_demo_sk JOIN store s ON fr.store_sk = s.s_store_sk WHERE fr.sr_return_amt > a_avg.avg_return_threshold AND cd.cd_education_status = 'Advanced Degree' AND s.s_state = 'CA' ORDER BY c_customer_id LIMIT 50;