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]
select a.ca_state state,count(*) cnt from customer_address a,customer c,store_sales s,date_dim d,item i where a.ca_address_sk = c.c_current_addr_sk and c.c_customer_sk = s.ss_customer_sk and s.ss_sold_date_sk = d.d_date_sk and s.ss_item_sk = i.i_item_sk and d.d_month_seq = (select distinct (d_month_seq) from date_dim where d_year = 2002 and d_moy = 3 ) and i.i_current_price > 1.2 * (select avg(j.i_current_price) from item j where j.i_category = i.i_category) group by a.ca_state having count(*) >= 10 order by cnt,a.ca_state limit 100;[REWRITTEN QUERY]
WITH avg_price AS ( SELECT i_category, AVG(i_current_price) avg_price FROM item GROUP BY i_category ), month_seq AS ( SELECT d_month_seq FROM date_dim WHERE d_year = 2002 AND d_moy = 3 LIMIT 1 ) SELECT a.ca_state state, COUNT(*) cnt FROM customer_address a JOIN customer c ON a.ca_address_sk = c.c_current_addr_sk JOIN store_sales s ON c.c_customer_sk = s.ss_customer_sk JOIN date_dim d ON s.ss_sold_date_sk = d.d_date_sk JOIN item i ON s.ss_item_sk = i.i_item_sk JOIN avg_price ap ON i.i_category = ap.i_category JOIN month_seq ms ON d.d_month_seq = ms.d_month_seq WHERE i.i_current_price > 1.2 * ap.avg_price GROUP BY a.ca_state HAVING COUNT(*) >= 10 ORDER BY cnt, a.ca_state 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: ['catalog_returns', 'store_returns', 'store_sales']. Below is the join graph for these fact tables: ['catalog_returns(cr_reason_sk) references reason(r_reason_sk);', 'catalog_returns(cr_returning_cdemo_sk) references customer_demographics(cd_demo_sk);', 'catalog_returns(cr_call_center_sk) references call_center(cc_call_center_sk);'] ['store_returns(sr_return_time_sk) references time_dim(t_time_sk);', 'store_returns(sr_customer_sk) references customer(c_customer_sk);', 'store_returns(sr_reason_sk) references reason(r_reason_sk);'] ['store_sales(ss_store_sk) references store(s_store_sk);', 'store_sales(ss_promo_sk) references promotion(p_promo_sk);', 'store_sales(ss_hdemo_sk) references household_demographics(hd_demo_sk);']
- The original query must employ 3 joins.
- The original query must employ the following aggregate functions: ['min', 'avg'].
- 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 min_price AS ( SELECT ss_store_sk, MIN(ss_sales_price) min_price FROM store_sales GROUP BY ss_store_sk ), avg_demo AS ( SELECT sr_customer_sk, AVG(cd_income_band) avg_income FROM customer_demographics JOIN store_returns ON store_returns.sr_returning_cdemo_sk = customer_demographics.cd_demo_sk GROUP BY sr_customer_sk ) SELECT cr_call_center_sk, MIN(cr_net_loss) AS min_loss FROM catalog_returns JOIN min_price ON catalog_returns.cr_net_loss > min_price.min_price JOIN avg_demo ON catalog_returns.cr_returning_cdemo_sk = avg_demo.sr_customer_sk GROUP BY cr_call_center_sk HAVING MIN(cr_net_loss) < 100 ORDER BY min_loss, cr_call_center_sk LIMIT 100;[REWRITTEN QUERY]
WITH min_store_price AS ( SELECT ss_store_sk, MIN(ss_sales_price) AS min_price FROM store_sales GROUP BY ss_store_sk ), avg_income_band AS ( SELECT sr_customer_sk, AVG(cd_income_band) AS avg_income FROM store_returns JOIN customer_demographics ON store_returns.sr_returning_cdemo_sk = customer_demographics.cd_demo_sk GROUP BY sr_customer_sk ) SELECT cr_call_center_sk, MIN(cr_net_loss) AS min_loss FROM catalog_returns JOIN catalog_returns cr ON cr.cr_net_loss > ( SELECT min_price FROM min_store_price WHERE ss_store_sk = cr.cr_store_sk ) JOIN avg_income_band aib ON cr.cr_returning_cdemo_sk = aib.sr_customer_sk GROUP BY cr_call_center_sk HAVING MIN(cr_net_loss) < 100 ORDER BY min_loss, cr_call_center_sk LIMIT 100;