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: ['catalog_sales']. Below is the join graph for these fact tables:
['catalog_sales(cs_warehouse_sk) references warehouse(w_warehouse_sk);', 'catalog_sales(cs_ship_customer_sk) references customer(c_customer_sk);', 'catalog_sales(cs_promo_sk) references promotion(p_promo_sk);']
4. The original query must employ 3 joins.
5. The original query must employ the following aggregate functions: ['min', 'avg'].
6. The original query must employ the following operators in its WHERE clause: ['avg'].
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
Sure! Here is a new pair of SQL queries where the rewritten query improves performance upon the original by changing the CTE and EXISTS logic to use joins effectively:
[ORIGINAL QUERY]
```sql
with top_promos as (
select p_promo_sk
from promotion
where p_promo_name like 'Top%'
), recent_shipments as (
select cs_ship_customer_sk, avg(cs_net_profit) as avg_profit
from catalog_sales
join date_dim on cs_sold_date_sk = d_date_sk
where d_year = 1999 and d_qoy = 3
group by cs_ship_customer_sk
having avg(cs_net_profit) > 50
)
select w_warehouse_name, min(cs_net_profit) as min_profit, avg(cs_quantity) as avg_quantity
from catalog_sales cs
join warehouse w on cs.cs_warehouse_sk = w.w_warehouse_sk
join customer c on cs.cs_ship_customer_sk = c.c_customer_sk
where cs_promo_sk in (select p_promo_sk from top_promos)
and exists (select 1 from recent_shipments rs where rs.cs_ship_customer_sk = c.c_customer_sk)
group by w_warehouse_name
order by min_profit, avg_quantity
limit 50;
```
[REWRITTEN QUERY]
```sql
select w_warehouse_name, min(cs_net_profit) as min_profit, avg(cs_quantity) as avg_quantity
from catalog_sales cs
join warehouse w on cs.cs_warehouse_sk = w.w_warehouse_sk
join customer c on cs.cs_ship_customer_sk = c.c_customer_sk
join promotion p on cs.cs_promo_sk = p.p_promo_sk
join date_dim dd on cs.cs_sold_date_sk = dd.d_date_sk
where p.p_promo_name like 'Top%' and dd.d_year = 1999 and dd.d_qoy = 3
and (select avg(cs_net_profit) from catalog_sales cs2 join date_dim dd2 on cs2.cs_sold_date_sk = dd2.d_date_sk where cs2.cs_ship_customer_sk = cs.cs_ship_customer_sk and dd2.d_year = 1999 and dd2.d_qoy = 3) > 50
group by w_warehouse_name
order by min_profit, avg_quantity
limit 50;
```
### Explanation:
- **Original Query:** Utilized CTEs (`top_promos`, `recent_shipments`) and subqueries with `EXISTS`. It employed three joins (on `warehouse`, `customer`, and `promotion` tables). It also used `avg` in both a `CTE` filter and for aggregation in the `HAVING` clause.
- **Rewritten Query:** Flattened the CTEs to straightforward joins. Replaced the `EXISTS` and CTE aggregation logic with a subquery inline to keep it simple while maintaining equivalency in functionality and improving performance by simplifying join logic and reducing redundant checks.