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 ca_state,cd_gender,cd_marital_status,cd_dep_count,count(*) cnt1,avg(cd_dep_count),stddev_samp(cd_dep_count),sum(cd_dep_count),cd_dep_employed_count,count(*) cnt2,avg(cd_dep_employed_count),stddev_samp(cd_dep_employed_count),sum(cd_dep_employed_count),cd_dep_college_count,count(*) cnt3,avg(cd_dep_college_count),stddev_samp(cd_dep_college_count),sum(cd_dep_college_count) from customer c,customer_address ca,customer_demographics where c.c_current_addr_sk = ca.ca_address_sk and cd_demo_sk = c.c_current_cdemo_sk and exists (select * from store_sales,date_dim where c.c_customer_sk = ss_customer_sk and ss_sold_date_sk = d_date_sk and d_year = 1999 and d_qoy < 4) and (exists (select * from web_sales,date_dim where c.c_customer_sk = ws_bill_customer_sk and ws_sold_date_sk = d_date_sk and d_year = 1999 and d_qoy < 4) or exists (select * from catalog_sales,date_dim where c.c_customer_sk = cs_ship_customer_sk and cs_sold_date_sk = d_date_sk and d_year = 1999 and d_qoy < 4)) group by ca_state,cd_gender,cd_marital_status,cd_dep_count,cd_dep_employed_count,cd_dep_college_count order by ca_state,cd_gender,cd_marital_status,cd_dep_count,cd_dep_employed_count,cd_dep_college_count limit 100;```
[REWRITTEN QUERY]```select ca_state, cd_gender, cd_marital_status, cd_dep_count, count(*) cnt1, avg(cd_dep_count), stddev_samp(cd_dep_count), sum(cd_dep_count), cd_dep_employed_count, count(*) cnt2, avg(cd_dep_employed_count), stddev_samp(cd_dep_employed_count), sum(cd_dep_employed_count), cd_dep_college_count, count(*) cnt3, avg(cd_dep_college_count), stddev_samp(cd_dep_college_count), sum(cd_dep_college_count) from customer c join customer_address ca on c.c_current_addr_sk = ca.ca_address_sk join customer_demographics cd on cd.cd_demo_sk = c.c_current_cdemo_sk where c.c_customer_sk in ( select ss_customer_sk from store_sales join date_dim on ss_sold_date_sk = d_date_sk where d_year = 1999 and d_qoy < 4 ) and ( c.c_customer_sk in ( select ws_bill_customer_sk from web_sales join date_dim on ws_sold_date_sk = d_date_sk where d_year = 1999 and d_qoy < 4 ) or c.c_customer_sk in ( select cs_ship_customer_sk from catalog_sales join date_dim on cs_sold_date_sk = d_date_sk where d_year = 1999 and d_qoy < 4 ) ) group by ca_state, cd_gender, cd_marital_status, cd_dep_count, cd_dep_employed_count, cd_dep_college_count order by ca_state, cd_gender, cd_marital_status, cd_dep_count, cd_dep_employed_count, cd_dep_college_count 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:
1. The original query must contain CTEs.
2. The original query must make use of the following fact tables: ['store_returns', 'catalog_sales']. Below is the join graph for these fact tables:
['store_returns(sr_cdemo_sk) references customer_demographics(cd_demo_sk);', 'store_returns(sr_item_sk) references item(i_item_sk);', 'store_returns(sr_store_sk) references store(s_store_sk);']
['catalog_sales(cs_bill_hdemo_sk) references household_demographics(hd_demo_sk);', 'catalog_sales(cs_bill_customer_sk) references customer(c_customer_sk);', 'catalog_sales(cs_ship_hdemo_sk) references household_demographics(hd_demo_sk);']
5. The original query must employ 4 joins.
6. The original query must employ the following operators in its WHERE clause: ['sum', 'max', 'min'].
7. 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
Here's a new pair of original and rewritten SQL queries following the constraints and rewriting pattern provided:
[ORIGINAL QUERY]
```sql
with recent_store_returns as (
select sr_customer_sk, sr_store_sk, sr_item_sk
from store_returns sr
join date_dim dd on sr.sr_returned_date_sk = dd.d_date_sk
where dd.d_year = 2001
),
high_catalog_sales as (
select cs_bill_customer_sk, cs_sold_date_sk
from catalog_sales cs
join date_dim dd on cs.cs_sold_date_sk = dd.d_date_sk
where cs.cs_quantity > 10
)
select i_item_id, cd_gender, min(sr_return_quantity) as min_qty, max(cs_quantity) as max_qty, sum(cs_ext_sales_price) as total_sales
from item i
join store_returns sr on i.i_item_sk = sr.sr_item_sk
join customer_demographics cd on sr.sr_cdemo_sk = cd.cd_demo_sk
join recent_store_returns r on sr.sr_customer_sk = r.sr_customer_sk
left join high_catalog_sales hc on sr.sr_customer_sk = hc.cs_bill_customer_sk
group by i_item_id, cd_gender
having sum(cs_ext_sales_price) > 1000;
```
[REWRITTEN QUERY]
```sql
select i_item_id, cd_gender, min(sr_return_quantity) as min_qty, max(cs_quantity) as max_qty, sum(cs_ext_sales_price) as total_sales
from item i
join store_returns sr on i.i_item_sk = sr.sr_item_sk
join customer_demographics cd on sr.sr_cdemo_sk = cd.cd_demo_sk
where sr.sr_customer_sk in (
select sr_customer_sk
from store_returns sr
join date_dim dd on sr.sr_returned_date_sk = dd.d_date_sk
where dd.d_year = 2001
)
and (sr.sr_customer_sk in (
select cs_bill_customer_sk
from catalog_sales cs
join date_dim dd on cs.cs_sold_date_sk = dd.d_date_sk
where cs.cs_quantity > 10
) or sr.sr_customer_sk is null)
group by i_item_id, cd_gender
having sum(cs_ext_sales_price) > 1000;
```
This pair of queries achieves the transformation similar to the example given:
1. **From CTEs to Subqueries:** The original query utilizes CTEs (`recent_store_returns`, `high_catalog_sales`), which are then converted into subqueries within the rewritten query.
2. **Consistency Across Joins and Filters:** Both queries maintain the necessary joins related to the fact tables (`store_returns`, `catalog_sales`) and their connections to `customer_demographics` and `item`. They maintain consistent logic for filtering based on conditions like year and quantity, as well as aggregate functions like `sum`, `min`, and `max`.
3. **Ensuring Logical Equivalence:** The rewritten query should function identically to the original query, yet offer potential performance improvements by simplifying the execution path through the removal of CTEs.