Question:
SELECT s_store_name,
       s_company_id,
       s_street_number,
       s_street_name,
       s_street_type,
       s_suite_number,
       s_city,
       s_county,
       s_state,
       s_zip,
       sum(CASE
               WHEN (sr_returned_date_sk - ss_sold_date_sk <= 30) THEN 1
               ELSE 0
           END) AS "30 days",
       sum(CASE
               WHEN (sr_returned_date_sk - ss_sold_date_sk > 30)
                    AND (sr_returned_date_sk - ss_sold_date_sk <= 60) THEN 1
               ELSE 0
           END) AS "31-60 days",
       sum(CASE
               WHEN (sr_returned_date_sk - ss_sold_date_sk > 60)
                    AND (sr_returned_date_sk - ss_sold_date_sk <= 90) THEN 1
               ELSE 0
           END) AS "61-90 days",
       sum(CASE
               WHEN (sr_returned_date_sk - ss_sold_date_sk > 90)
                    AND (sr_returned_date_sk - ss_sold_date_sk <= 120) THEN 1
               ELSE 0
           END) AS "91-120 days",
       sum(CASE
               WHEN (sr_returned_date_sk - ss_sold_date_sk > 120) THEN 1
               ELSE 0
           END) AS ">120 days"
FROM store_sales,
     store_returns,
     store,
     date_dim d1,
     date_dim d2
WHERE d2.d_year = 2001
  AND d2.d_moy = 8
  AND ss_ticket_number = sr_ticket_number
  AND ss_item_sk = sr_item_sk
  AND ss_sold_date_sk = d1.d_date_sk
  AND sr_returned_date_sk = d2.d_date_sk
  AND ss_customer_sk = sr_customer_sk
  AND ss_store_sk = s_store_sk
GROUP BY s_store_name,
         s_company_id,
         s_street_number,
         s_street_name,
         s_street_type,
         s_suite_number,
         s_city,
         s_county,
         s_state,
         s_zip
ORDER BY s_store_name,
         s_company_id,
         s_street_number,
         s_street_name,
         s_street_type,
         s_suite_number,
         s_city,
         s_county,
         s_state,
         s_zip
LIMIT 100;


Question:
WITH web_v1 AS
  (SELECT ws_item_sk item_sk,
          d_date,
          sum(sum(ws_sales_price)) OVER (PARTITION BY ws_item_sk
                                         ORDER BY d_date ROWS BETWEEN unbounded preceding AND CURRENT ROW) cume_sales
   FROM web_sales,
        date_dim
   WHERE ws_sold_date_sk=d_date_sk
     AND d_month_seq BETWEEN 1200 AND 1200+11
     AND ws_item_sk IS NOT NULL
   GROUP BY ws_item_sk,
            d_date),
     store_v1 AS
  (SELECT ss_item_sk item_sk,
          d_date,
          sum(sum(ss_sales_price)) OVER (PARTITION BY ss_item_sk
                                         ORDER BY d_date ROWS BETWEEN unbounded preceding AND CURRENT ROW) cume_sales
   FROM store_sales,
        date_dim
   WHERE ss_sold_date_sk=d_date_sk
     AND d_month_seq BETWEEN 1200 AND 1200+11
     AND ss_item_sk IS NOT NULL
   GROUP BY ss_item_sk,
            d_date)
SELECT *
FROM
  (SELECT item_sk,
          d_date,
          web_sales,
          store_sales,
          max(web_sales) OVER (PARTITION BY item_sk
                               ORDER BY d_date ROWS BETWEEN unbounded preceding AND CURRENT ROW) web_cumulative,
                              max(store_sales) OVER (PARTITION BY item_sk
                                                     ORDER BY d_date ROWS BETWEEN unbounded preceding AND CURRENT ROW) store_cumulative
   FROM
     (SELECT CASE
                 WHEN web.item_sk IS NOT NULL THEN web.item_sk
                 ELSE store.item_sk
             END item_sk,
             CASE
                 WHEN web.d_date IS NOT NULL THEN web.d_date
                 ELSE store.d_date
             END d_date,
             web.cume_sales web_sales,
             store.cume_sales store_sales
      FROM web_v1 web
      FULL OUTER JOIN store_v1 store ON (web.item_sk = store.item_sk
                                         AND web.d_date = store.d_date))x)y
WHERE web_cumulative > store_cumulative
ORDER BY item_sk NULLS FIRST,
         d_date NULLS FIRST
LIMIT 100;


Question:
SELECT dt.d_year,
       item.i_brand_id brand_id,
       item.i_brand brand,
       sum(ss_ext_sales_price) ext_price
FROM date_dim dt,
     store_sales,
     item
WHERE dt.d_date_sk = store_sales.ss_sold_date_sk
  AND store_sales.ss_item_sk = item.i_item_sk
  AND item.i_manager_id = 1
  AND dt.d_moy=11
  AND dt.d_year=2000
GROUP BY dt.d_year,
         item.i_brand,
         item.i_brand_id
ORDER BY dt.d_year,
         ext_price DESC,
         brand_id
LIMIT 100 ;


Question:
SELECT *
FROM
  (SELECT i_manufact_id,
          sum(ss_sales_price) sum_sales,
          avg(sum(ss_sales_price)) OVER (PARTITION BY i_manufact_id) avg_quarterly_sales
   FROM item,
        store_sales,
        date_dim,
        store
   WHERE ss_item_sk = i_item_sk
     AND ss_sold_date_sk = d_date_sk
     AND ss_store_sk = s_store_sk
     AND d_month_seq IN (1200,
                         1200+1,
                         1200+2,
                         1200+3,
                         1200+4,
                         1200+5,
                         1200+6,
                         1200+7,
                         1200+8,
                         1200+9,
                         1200+10,
                         1200+11)
     AND ((i_category IN ('Books',
                          'Children',
                          'Electronics')
           AND i_class IN ('personal',
                           'portable',
                           'reference',
                           'self-help')
           AND i_brand IN ('scholaramalgamalg #14',
                           'scholaramalgamalg #7',
                           'exportiunivamalg #9',
                           'scholaramalgamalg #9')) or(i_category IN ('Women','Music','Men')
                                                       AND i_class IN ('accessories','classical','fragrances','pants')
                                                       AND i_brand IN ('amalgimporto #1','edu packscholar #1','exportiimporto #1', 'importoamalg #1')))
   GROUP BY i_manufact_id,
            d_qoy) tmp1
WHERE CASE
          WHEN avg_quarterly_sales > 0 THEN ABS (sum_sales - avg_quarterly_sales)/ avg_quarterly_sales
          ELSE NULL
      END > 0.1
ORDER BY avg_quarterly_sales,
         sum_sales,
         i_manufact_id
LIMIT 100;


Question:
WITH my_customers AS
  (SELECT DISTINCT c_customer_sk,
                   c_current_addr_sk
   FROM
     (SELECT cs_sold_date_sk sold_date_sk,
             cs_bill_customer_sk customer_sk,
             cs_item_sk item_sk
      FROM catalog_sales
      UNION ALL SELECT ws_sold_date_sk sold_date_sk,
                       ws_bill_customer_sk customer_sk,
                       ws_item_sk item_sk
      FROM web_sales) cs_or_ws_sales,
        item,
        date_dim,
        customer
   WHERE sold_date_sk = d_date_sk
     AND item_sk = i_item_sk
     AND i_category = 'Women'
     AND i_class = 'maternity'
     AND c_customer_sk = cs_or_ws_sales.customer_sk
     AND d_moy = 12
     AND d_year = 1998 ),
     my_revenue AS
  (SELECT c_customer_sk,
          sum(ss_ext_sales_price) AS revenue
   FROM my_customers,
        store_sales,
        customer_address,
        store,
        date_dim
   WHERE c_current_addr_sk = ca_address_sk
     AND ca_county = s_county
     AND ca_state = s_state
     AND ss_sold_date_sk = d_date_sk
     AND c_customer_sk = ss_customer_sk
     AND d_month_seq BETWEEN
       (SELECT DISTINCT d_month_seq+1
        FROM date_dim
        WHERE d_year = 1998
          AND d_moy = 12) AND
       (SELECT DISTINCT d_month_seq+3
        FROM date_dim
        WHERE d_year = 1998
          AND d_moy = 12)
   GROUP BY c_customer_sk),
     segments AS
  (SELECT cast(round(revenue/50) AS int) AS SEGMENT
   FROM my_revenue)
SELECT SEGMENT,
       count(*) AS num_customers,
       SEGMENT*50 AS segment_base
FROM segments
GROUP BY SEGMENT
ORDER BY SEGMENT NULLS FIRST,
         num_customers NULLS FIRST,
         segment_base
LIMIT 100;


Question:
SELECT i_brand_id brand_id,
       i_brand brand,
       sum(ss_ext_sales_price) ext_price
FROM date_dim,
     store_sales,
     item
WHERE d_date_sk = ss_sold_date_sk
  AND ss_item_sk = i_item_sk
  AND i_manager_id=28
  AND d_moy=11
  AND d_year=1999
GROUP BY i_brand,
         i_brand_id
ORDER BY ext_price DESC,
         i_brand_id
LIMIT 100 ;


Question:
WITH ss AS
  (SELECT i_item_id,
          sum(ss_ext_sales_price) total_sales
   FROM store_sales,
        date_dim,
        customer_address,
        item
   WHERE i_item_id IN
       (SELECT i_item_id
        FROM item
        WHERE i_color IN ('slate',
                          'blanched',
                          'burnished'))
     AND ss_item_sk = i_item_sk
     AND ss_sold_date_sk = d_date_sk
     AND d_year = 2001
     AND d_moy = 2
     AND ss_addr_sk = ca_address_sk
     AND ca_gmt_offset = -5
   GROUP BY i_item_id),
     cs AS
  (SELECT i_item_id,
          sum(cs_ext_sales_price) total_sales
   FROM catalog_sales,
        date_dim,
        customer_address,
        item
   WHERE i_item_id IN
       (SELECT i_item_id
        FROM item
        WHERE i_color IN ('slate',
                          'blanched',
                          'burnished'))
     AND cs_item_sk = i_item_sk
     AND cs_sold_date_sk = d_date_sk
     AND d_year = 2001
     AND d_moy = 2
     AND cs_bill_addr_sk = ca_address_sk
     AND ca_gmt_offset = -5
   GROUP BY i_item_id),
     ws AS
  (SELECT i_item_id,
          sum(ws_ext_sales_price) total_sales
   FROM web_sales,
        date_dim,
        customer_address,
        item
   WHERE i_item_id IN
       (SELECT i_item_id
        FROM item
        WHERE i_color IN ('slate',
                          'blanched',
                          'burnished'))
     AND ws_item_sk = i_item_sk
     AND ws_sold_date_sk = d_date_sk
     AND d_year = 2001
     AND d_moy = 2
     AND ws_bill_addr_sk = ca_address_sk
     AND ca_gmt_offset = -5
   GROUP BY i_item_id)
SELECT i_item_id,
       sum(total_sales) total_sales
FROM
  (SELECT *
   FROM ss
   UNION ALL SELECT *
   FROM cs
   UNION ALL SELECT *
   FROM ws) tmp1
GROUP BY i_item_id
ORDER BY total_sales  NULLS FIRST,
         i_item_id NULLS FIRST
LIMIT 100;


Question:
WITH v1 AS
  (SELECT i_category,
          i_brand,
          cc_name,
          d_year,
          d_moy,
          sum(cs_sales_price) sum_sales,
          avg(sum(cs_sales_price)) OVER (PARTITION BY i_category,
                                                      i_brand,
                                                      cc_name,
                                                      d_year) avg_monthly_sales,
                                        rank() OVER (PARTITION BY i_category,
                                                                  i_brand,
                                                                  cc_name
                                                     ORDER BY d_year,
                                                              d_moy) rn
   FROM item,
        catalog_sales,
        date_dim,
        call_center
   WHERE cs_item_sk = i_item_sk
     AND cs_sold_date_sk = d_date_sk
     AND cc_call_center_sk= cs_call_center_sk
     AND (d_year = 1999
          OR (d_year = 1999-1
              AND d_moy =12)
          OR (d_year = 1999+1
              AND d_moy =1))
   GROUP BY i_category,
            i_brand,
            cc_name,
            d_year,
            d_moy),
     v2 AS
  (SELECT v1.i_category,
          v1.i_brand,
          v1.cc_name,
          v1.d_year,
          v1.d_moy,
          v1.avg_monthly_sales,
          v1.sum_sales,
          v1_lag.sum_sales psum,
          v1_lead.sum_sales nsum
   FROM v1,
        v1 v1_lag,
        v1 v1_lead
   WHERE v1.i_category = v1_lag.i_category
     AND v1.i_category = v1_lead.i_category
     AND v1.i_brand = v1_lag.i_brand
     AND v1.i_brand = v1_lead.i_brand
     AND v1. cc_name = v1_lag. cc_name
     AND v1. cc_name = v1_lead. cc_name
     AND v1.rn = v1_lag.rn + 1
     AND v1.rn = v1_lead.rn - 1)
SELECT *
FROM v2
WHERE d_year = 1999
  AND avg_monthly_sales > 0
  AND CASE
          WHEN avg_monthly_sales > 0 THEN abs(sum_sales - avg_monthly_sales) / avg_monthly_sales
          ELSE NULL
      END > 0.1
ORDER BY sum_sales - avg_monthly_sales NULLS FIRST, 1, 2, 3, 4, 5, 6, 7, 8, 9
LIMIT 100;


Question:
WITH ss_items AS
  (SELECT i_item_id item_id,
          sum(ss_ext_sales_price) ss_item_rev
   FROM store_sales,
        item,
        date_dim
   WHERE ss_item_sk = i_item_sk
     AND d_date IN
       (SELECT d_date
        FROM date_dim
        WHERE d_week_seq =
            (SELECT d_week_seq
             FROM date_dim
             WHERE d_date = '2000-01-03'))
     AND ss_sold_date_sk = d_date_sk
   GROUP BY i_item_id),
     cs_items AS
  (SELECT i_item_id item_id,
          sum(cs_ext_sales_price) cs_item_rev
   FROM catalog_sales,
        item,
        date_dim
   WHERE cs_item_sk = i_item_sk
     AND d_date IN
       (SELECT d_date
        FROM date_dim
        WHERE d_week_seq =
            (SELECT d_week_seq
             FROM date_dim
             WHERE d_date = '2000-01-03'))
     AND cs_sold_date_sk = d_date_sk
   GROUP BY i_item_id),
     ws_items AS
  (SELECT i_item_id item_id,
          sum(ws_ext_sales_price) ws_item_rev
   FROM web_sales,
        item,
        date_dim
   WHERE ws_item_sk = i_item_sk
     AND d_date IN
       (SELECT d_date
        FROM date_dim
        WHERE d_week_seq =
            (SELECT d_week_seq
             FROM date_dim
             WHERE d_date = '2000-01-03'))
     AND ws_sold_date_sk = d_date_sk
   GROUP BY i_item_id)
SELECT ss_items.item_id,
       ss_item_rev,
       ss_item_rev/((ss_item_rev+cs_item_rev+ws_item_rev)/3) * 100 ss_dev,
       cs_item_rev,
       cs_item_rev/((ss_item_rev+cs_item_rev+ws_item_rev)/3) * 100 cs_dev,
       ws_item_rev,
       ws_item_rev/((ss_item_rev+cs_item_rev+ws_item_rev)/3) * 100 ws_dev,
       (ss_item_rev+cs_item_rev+ws_item_rev)/3 average
FROM ss_items,
     cs_items,
     ws_items
WHERE ss_items.item_id=cs_items.item_id
  AND ss_items.item_id=ws_items.item_id
  AND ss_item_rev BETWEEN 0.9 * cs_item_rev AND 1.1 * cs_item_rev
  AND ss_item_rev BETWEEN 0.9 * ws_item_rev AND 1.1 * ws_item_rev
  AND cs_item_rev BETWEEN 0.9 * ss_item_rev AND 1.1 * ss_item_rev
  AND cs_item_rev BETWEEN 0.9 * ws_item_rev AND 1.1 * ws_item_rev
  AND ws_item_rev BETWEEN 0.9 * ss_item_rev AND 1.1 * ss_item_rev
  AND ws_item_rev BETWEEN 0.9 * cs_item_rev AND 1.1 * cs_item_rev
ORDER BY ss_items.item_id NULLS FIRST,
         ss_item_rev NULLS FIRST
LIMIT 100;


Question:
WITH wss AS
  (SELECT d_week_seq,
          ss_store_sk,
          sum(CASE
                  WHEN (d_day_name='Sunday') THEN ss_sales_price
                  ELSE NULL
              END) sun_sales,
          sum(CASE
                  WHEN (d_day_name='Monday') THEN ss_sales_price
                  ELSE NULL
              END) mon_sales,
          sum(CASE
                  WHEN (d_day_name='Tuesday') THEN ss_sales_price
                  ELSE NULL
              END) tue_sales,
          sum(CASE
                  WHEN (d_day_name='Wednesday') THEN ss_sales_price
                  ELSE NULL
              END) wed_sales,
          sum(CASE
                  WHEN (d_day_name='Thursday') THEN ss_sales_price
                  ELSE NULL
              END) thu_sales,
          sum(CASE
                  WHEN (d_day_name='Friday') THEN ss_sales_price
                  ELSE NULL
              END) fri_sales,
          sum(CASE
                  WHEN (d_day_name='Saturday') THEN ss_sales_price
                  ELSE NULL
              END) sat_sales
   FROM store_sales,
        date_dim
   WHERE d_date_sk = ss_sold_date_sk
   GROUP BY d_week_seq,
            ss_store_sk)
SELECT s_store_name1,
       s_store_id1,
       d_week_seq1,
       sun_sales1/sun_sales2 AS sun_sales_ratio,
       mon_sales1/mon_sales2 AS mon_sales_ratio,
       tue_sales1/tue_sales2 AS tue_sales_ratio,
       wed_sales1/wed_sales2 AS wed_sales_ratio,
       thu_sales1/thu_sales2 AS thu_sales_ratio,
       fri_sales1/fri_sales2 AS fri_sales_ratio,
       sat_sales1/sat_sales2 AS sat_sales_ratio
FROM
  (SELECT s_store_name s_store_name1,
          wss.d_week_seq d_week_seq1,
          s_store_id s_store_id1,
          sun_sales sun_sales1,
          mon_sales mon_sales1,
          tue_sales tue_sales1,
          wed_sales wed_sales1,
          thu_sales thu_sales1,
          fri_sales fri_sales1,
          sat_sales sat_sales1
   FROM wss,
        store,
        date_dim d
   WHERE d.d_week_seq = wss.d_week_seq
     AND ss_store_sk = s_store_sk
     AND d_month_seq BETWEEN 1212 AND 1212 + 11) y,
  (SELECT s_store_name s_store_name2,
          wss.d_week_seq d_week_seq2,
          s_store_id s_store_id2,
          sun_sales sun_sales2,
          mon_sales mon_sales2,
          tue_sales tue_sales2,
          wed_sales wed_sales2,
          thu_sales thu_sales2,
          fri_sales fri_sales2,
          sat_sales sat_sales2
   FROM wss,
        store,
        date_dim d
   WHERE d.d_week_seq = wss.d_week_seq
     AND ss_store_sk = s_store_sk
     AND d_month_seq BETWEEN 1212 + 12 AND 1212 + 23) x
WHERE s_store_id1=s_store_id2
  AND d_week_seq1=d_week_seq2-52
ORDER BY s_store_name1 NULLS FIRST,
         s_store_id1 NULLS FIRST,
         d_week_seq1 NULLS FIRST
LIMIT 100;


Question:
WITH ss AS
  (SELECT i_item_id,
          sum(ss_ext_sales_price) total_sales
   FROM store_sales,
        date_dim,
        customer_address,
        item
   WHERE i_item_id IN
       (SELECT i_item_id
        FROM item
        WHERE i_category ='Music')
     AND ss_item_sk = i_item_sk
     AND ss_sold_date_sk = d_date_sk
     AND d_year = 1998
     AND d_moy = 9
     AND ss_addr_sk = ca_address_sk
     AND ca_gmt_offset = -5
   GROUP BY i_item_id),
     cs AS
  (SELECT i_item_id,
          sum(cs_ext_sales_price) total_sales
   FROM catalog_sales,
        date_dim,
        customer_address,
        item
   WHERE i_item_id IN
       (SELECT i_item_id
        FROM item
        WHERE i_category ='Music')
     AND cs_item_sk = i_item_sk
     AND cs_sold_date_sk = d_date_sk
     AND d_year = 1998
     AND d_moy = 9
     AND cs_bill_addr_sk = ca_address_sk
     AND ca_gmt_offset = -5
   GROUP BY i_item_id),
     ws AS
  (SELECT i_item_id,
          sum(ws_ext_sales_price) total_sales
   FROM web_sales,
        date_dim,
        customer_address,
        item
   WHERE i_item_id IN
       (SELECT i_item_id
        FROM item
        WHERE i_category = 'Music')
     AND ws_item_sk = i_item_sk
     AND ws_sold_date_sk = d_date_sk
     AND d_year = 1998
     AND d_moy = 9
     AND ws_bill_addr_sk = ca_address_sk
     AND ca_gmt_offset = -5
   GROUP BY i_item_id)
SELECT i_item_id,
       sum(total_sales) total_sales
FROM
  (SELECT *
   FROM ss
   UNION ALL SELECT *
   FROM cs
   UNION ALL SELECT *
   FROM ws) tmp1
GROUP BY i_item_id
ORDER BY i_item_id,
         total_sales
LIMIT 100;


Question:
SELECT promotions,
       total,
       cast(promotions AS decimal(15,4))/cast(total AS decimal(15,4))*100
FROM
  (SELECT sum(ss_ext_sales_price) promotions
   FROM store_sales,
        store,
        promotion,
        date_dim,
        customer,
        customer_address,
        item
   WHERE ss_sold_date_sk = d_date_sk
     AND ss_store_sk = s_store_sk
     AND ss_promo_sk = p_promo_sk
     AND ss_customer_sk= c_customer_sk
     AND ca_address_sk = c_current_addr_sk
     AND ss_item_sk = i_item_sk
     AND ca_gmt_offset = -5
     AND i_category = 'Jewelry'
     AND (p_channel_dmail = 'Y'
          OR p_channel_email = 'Y'
          OR p_channel_tv = 'Y')
     AND s_gmt_offset = -5
     AND d_year = 1998
     AND d_moy = 11) promotional_sales,
  (SELECT sum(ss_ext_sales_price) total
   FROM store_sales,
        store,
        date_dim,
        customer,
        customer_address,
        item
   WHERE ss_sold_date_sk = d_date_sk
     AND ss_store_sk = s_store_sk
     AND ss_customer_sk= c_customer_sk
     AND ca_address_sk = c_current_addr_sk
     AND ss_item_sk = i_item_sk
     AND ca_gmt_offset = -5
     AND i_category = 'Jewelry'
     AND s_gmt_offset = -5
     AND d_year = 1998
     AND d_moy = 11) all_sales
ORDER BY promotions,
         total
LIMIT 100;


Question:
SELECT w_substr,
       sm_type,
       web_name,
       sum(CASE
               WHEN (ws_ship_date_sk - ws_sold_date_sk <= 30) THEN 1
               ELSE 0
           END) AS "30 days",
       sum(CASE
               WHEN (ws_ship_date_sk - ws_sold_date_sk > 30)
                    AND (ws_ship_date_sk - ws_sold_date_sk <= 60) THEN 1
               ELSE 0
           END) AS "31-60 days",
       sum(CASE
               WHEN (ws_ship_date_sk - ws_sold_date_sk > 60)
                    AND (ws_ship_date_sk - ws_sold_date_sk <= 90) THEN 1
               ELSE 0
           END) AS "61-90 days",
       sum(CASE
               WHEN (ws_ship_date_sk - ws_sold_date_sk > 90)
                    AND (ws_ship_date_sk - ws_sold_date_sk <= 120) THEN 1
               ELSE 0
           END) AS "91-120 days",
       sum(CASE
               WHEN (ws_ship_date_sk - ws_sold_date_sk > 120) THEN 1
               ELSE 0
           END) AS ">120 days"
FROM web_sales,
  (SELECT SUBSTRING(w_warehouse_name,1,20) w_substr,
          *
   FROM warehouse) sq1,
     ship_mode,
     web_site,
     date_dim
WHERE d_month_seq BETWEEN 1200 AND 1200 + 11
  AND ws_ship_date_sk = d_date_sk
  AND ws_warehouse_sk = w_warehouse_sk
  AND ws_ship_mode_sk = sm_ship_mode_sk
  AND ws_web_site_sk = web_site_sk
GROUP BY w_substr,
         sm_type,
         web_name
ORDER BY 1 NULLS FIRST,
         2 NULLS FIRST,
         3 NULLS FIRST
LIMIT 100;


Question:
SELECT *
FROM
  (SELECT i_manager_id,
          sum(ss_sales_price) sum_sales,
          avg(sum(ss_sales_price)) OVER (PARTITION BY i_manager_id) avg_monthly_sales
   FROM item,
        store_sales,
        date_dim,
        store
   WHERE ss_item_sk = i_item_sk
     AND ss_sold_date_sk = d_date_sk
     AND ss_store_sk = s_store_sk
     AND d_month_seq IN (1200,
                         1200+1,
                         1200+2,
                         1200+3,
                         1200+4,
                         1200+5,
                         1200+6,
                         1200+7,
                         1200+8,
                         1200+9,
                         1200+10,
                         1200+11)
     AND ((i_category IN ('Books',
                          'Children',
                          'Electronics')
           AND i_class IN ('personal',
                           'portable',
                           'reference',
                           'self-help')
           AND i_brand IN ('scholaramalgamalg #14',
                           'scholaramalgamalg #7',
                           'exportiunivamalg #9',
                           'scholaramalgamalg #9')) or(i_category IN ('Women','Music','Men')
                                                       AND i_class IN ('accessories','classical','fragrances','pants')
                                                       AND i_brand IN ('amalgimporto #1','edu packscholar #1','exportiimporto #1', 'importoamalg #1')))
   GROUP BY i_manager_id,
            d_moy) tmp1
WHERE CASE
          WHEN avg_monthly_sales > 0 THEN ABS (sum_sales - avg_monthly_sales) / avg_monthly_sales
          ELSE NULL
      END > 0.1
ORDER BY i_manager_id,
         avg_monthly_sales,
         sum_sales
LIMIT 100;


Question:
WITH cs_ui AS
  (SELECT cs_item_sk,
          sum(cs_ext_list_price) AS sale,
          sum(cr_refunded_cash+cr_reversed_charge+cr_store_credit) AS refund
   FROM catalog_sales,
        catalog_returns
   WHERE cs_item_sk = cr_item_sk
     AND cs_order_number = cr_order_number
   GROUP BY cs_item_sk
   HAVING sum(cs_ext_list_price)>2*sum(cr_refunded_cash+cr_reversed_charge+cr_store_credit)),
     cross_sales AS
  (SELECT i_product_name product_name,
          i_item_sk item_sk,
          s_store_name store_name,
          s_zip store_zip,
          ad1.ca_street_number b_street_number,
          ad1.ca_street_name b_street_name,
          ad1.ca_city b_city,
          ad1.ca_zip b_zip,
          ad2.ca_street_number c_street_number,
          ad2.ca_street_name c_street_name,
          ad2.ca_city c_city,
          ad2.ca_zip c_zip,
          d1.d_year AS syear,
          d2.d_year AS fsyear,
          d3.d_year s2year,
          count(*) cnt,
          sum(ss_wholesale_cost) s1,
          sum(ss_list_price) s2,
          sum(ss_coupon_amt) s3
   FROM store_sales,
        store_returns,
        cs_ui,
        date_dim d1,
        date_dim d2,
        date_dim d3,
        store,
        customer,
        customer_demographics cd1,
        customer_demographics cd2,
        promotion,
        household_demographics hd1,
        household_demographics hd2,
        customer_address ad1,
        customer_address ad2,
        income_band ib1,
        income_band ib2,
        item
   WHERE ss_store_sk = s_store_sk
     AND ss_sold_date_sk = d1.d_date_sk
     AND ss_customer_sk = c_customer_sk
     AND ss_cdemo_sk= cd1.cd_demo_sk
     AND ss_hdemo_sk = hd1.hd_demo_sk
     AND ss_addr_sk = ad1.ca_address_sk
     AND ss_item_sk = i_item_sk
     AND ss_item_sk = sr_item_sk
     AND ss_ticket_number = sr_ticket_number
     AND ss_item_sk = cs_ui.cs_item_sk
     AND c_current_cdemo_sk = cd2.cd_demo_sk
     AND c_current_hdemo_sk = hd2.hd_demo_sk
     AND c_current_addr_sk = ad2.ca_address_sk
     AND c_first_sales_date_sk = d2.d_date_sk
     AND c_first_shipto_date_sk = d3.d_date_sk
     AND ss_promo_sk = p_promo_sk
     AND hd1.hd_income_band_sk = ib1.ib_income_band_sk
     AND hd2.hd_income_band_sk = ib2.ib_income_band_sk
     AND cd1.cd_marital_status <> cd2.cd_marital_status
     AND i_color IN ('purple',
                     'burlywood',
                     'indian',
                     'spring',
                     'floral',
                     'medium')
     AND i_current_price BETWEEN 64 AND 64 + 10
     AND i_current_price BETWEEN 64 + 1 AND 64 + 15
   GROUP BY i_product_name,
            i_item_sk,
            s_store_name,
            s_zip,
            ad1.ca_street_number,
            ad1.ca_street_name,
            ad1.ca_city,
            ad1.ca_zip,
            ad2.ca_street_number,
            ad2.ca_street_name,
            ad2.ca_city,
            ad2.ca_zip,
            d1.d_year,
            d2.d_year,
            d3.d_year)
SELECT cs1.product_name,
       cs1.store_name,
       cs1.store_zip,
       cs1.b_street_number,
       cs1.b_street_name,
       cs1.b_city,
       cs1.b_zip,
       cs1.c_street_number,
       cs1.c_street_name,
       cs1.c_city,
       cs1.c_zip,
       cs1.syear cs1syear,
       cs1.cnt cs1cnt,
       cs1.s1 AS s11,
       cs1.s2 AS s21,
       cs1.s3 AS s31,
       cs2.s1 AS s12,
       cs2.s2 AS s22,
       cs2.s3 AS s32,
       cs2.syear,
       cs2.cnt
FROM cross_sales cs1,
     cross_sales cs2
WHERE cs1.item_sk=cs2.item_sk
  AND cs1.syear = 1999
  AND cs2.syear = 1999 + 1
  AND cs2.cnt <= cs1.cnt
  AND cs1.store_name = cs2.store_name
  AND cs1.store_zip = cs2.store_zip
ORDER BY cs1.product_name,
         cs1.store_name,
         cs2.cnt,
         cs1.s1,
         cs2.s1;


Question:
SELECT s_store_name,
       i_item_desc,
       sc.revenue,
       i_current_price,
       i_wholesale_cost,
       i_brand
FROM store,
     item,
  (SELECT ss_store_sk,
          avg(revenue) AS ave
   FROM
     (SELECT ss_store_sk,
             ss_item_sk,
             sum(ss_sales_price) AS revenue
      FROM store_sales,
           date_dim
      WHERE ss_sold_date_sk = d_date_sk
        AND d_month_seq BETWEEN 1176 AND 1176+11
      GROUP BY ss_store_sk,
               ss_item_sk) sa
   GROUP BY ss_store_sk) sb,
  (SELECT ss_store_sk,
          ss_item_sk,
          sum(ss_sales_price) AS revenue
   FROM store_sales,
        date_dim
   WHERE ss_sold_date_sk = d_date_sk
     AND d_month_seq BETWEEN 1176 AND 1176+11
   GROUP BY ss_store_sk,
            ss_item_sk) sc
WHERE sb.ss_store_sk = sc.ss_store_sk
  AND sc.revenue <= 0.1 * sb.ave
  AND s_store_sk = sc.ss_store_sk
  AND i_item_sk = sc.ss_item_sk
ORDER BY s_store_name NULLS FIRST,
         i_item_desc NULLS FIRST
LIMIT 100;


Question:
select
         w_warehouse_name
  ,w_warehouse_sq_ft
  ,w_city
  ,w_county
  ,w_state
  ,w_country
        ,ship_carriers
        ,year_
  ,sum(jan_sales) as jan_sales
  ,sum(feb_sales) as feb_sales
  ,sum(mar_sales) as mar_sales
  ,sum(apr_sales) as apr_sales
  ,sum(may_sales) as may_sales
  ,sum(jun_sales) as jun_sales
  ,sum(jul_sales) as jul_sales
  ,sum(aug_sales) as aug_sales
  ,sum(sep_sales) as sep_sales
  ,sum(oct_sales) as oct_sales
  ,sum(nov_sales) as nov_sales
  ,sum(dec_sales) as dec_sales
  ,sum(jan_sales/w_warehouse_sq_ft) as jan_sales_per_sq_foot
  ,sum(feb_sales/w_warehouse_sq_ft) as feb_sales_per_sq_foot
  ,sum(mar_sales/w_warehouse_sq_ft) as mar_sales_per_sq_foot
  ,sum(apr_sales/w_warehouse_sq_ft) as apr_sales_per_sq_foot
  ,sum(may_sales/w_warehouse_sq_ft) as may_sales_per_sq_foot
  ,sum(jun_sales/w_warehouse_sq_ft) as jun_sales_per_sq_foot
  ,sum(jul_sales/w_warehouse_sq_ft) as jul_sales_per_sq_foot
  ,sum(aug_sales/w_warehouse_sq_ft) as aug_sales_per_sq_foot
  ,sum(sep_sales/w_warehouse_sq_ft) as sep_sales_per_sq_foot
  ,sum(oct_sales/w_warehouse_sq_ft) as oct_sales_per_sq_foot
  ,sum(nov_sales/w_warehouse_sq_ft) as nov_sales_per_sq_foot
  ,sum(dec_sales/w_warehouse_sq_ft) as dec_sales_per_sq_foot
  ,sum(jan_net) as jan_net
  ,sum(feb_net) as feb_net
  ,sum(mar_net) as mar_net
  ,sum(apr_net) as apr_net
  ,sum(may_net) as may_net
  ,sum(jun_net) as jun_net
  ,sum(jul_net) as jul_net
  ,sum(aug_net) as aug_net
  ,sum(sep_net) as sep_net
  ,sum(oct_net) as oct_net
  ,sum(nov_net) as nov_net
  ,sum(dec_net) as dec_net
 from (
     select
  w_warehouse_name
  ,w_warehouse_sq_ft
  ,w_city
  ,w_county
  ,w_state
  ,w_country
  ,'DHL,BARIAN' as ship_carriers
       ,d_year as year_
  ,sum(case when d_moy = 1
    then ws_ext_sales_price* ws_quantity else 0 end) as jan_sales
  ,sum(case when d_moy = 2
    then ws_ext_sales_price* ws_quantity else 0 end) as feb_sales
  ,sum(case when d_moy = 3
    then ws_ext_sales_price* ws_quantity else 0 end) as mar_sales
  ,sum(case when d_moy = 4
    then ws_ext_sales_price* ws_quantity else 0 end) as apr_sales
  ,sum(case when d_moy = 5
    then ws_ext_sales_price* ws_quantity else 0 end) as may_sales
  ,sum(case when d_moy = 6
    then ws_ext_sales_price* ws_quantity else 0 end) as jun_sales
  ,sum(case when d_moy = 7
    then ws_ext_sales_price* ws_quantity else 0 end) as jul_sales
  ,sum(case when d_moy = 8
    then ws_ext_sales_price* ws_quantity else 0 end) as aug_sales
  ,sum(case when d_moy = 9
    then ws_ext_sales_price* ws_quantity else 0 end) as sep_sales
  ,sum(case when d_moy = 10
    then ws_ext_sales_price* ws_quantity else 0 end) as oct_sales
  ,sum(case when d_moy = 11
    then ws_ext_sales_price* ws_quantity else 0 end) as nov_sales
  ,sum(case when d_moy = 12
    then ws_ext_sales_price* ws_quantity else 0 end) as dec_sales
  ,sum(case when d_moy = 1
    then ws_net_paid * ws_quantity else 0 end) as jan_net
  ,sum(case when d_moy = 2
    then ws_net_paid * ws_quantity else 0 end) as feb_net
  ,sum(case when d_moy = 3
    then ws_net_paid * ws_quantity else 0 end) as mar_net
  ,sum(case when d_moy = 4
    then ws_net_paid * ws_quantity else 0 end) as apr_net
  ,sum(case when d_moy = 5
    then ws_net_paid * ws_quantity else 0 end) as may_net
  ,sum(case when d_moy = 6
    then ws_net_paid * ws_quantity else 0 end) as jun_net
  ,sum(case when d_moy = 7
    then ws_net_paid * ws_quantity else 0 end) as jul_net
  ,sum(case when d_moy = 8
    then ws_net_paid * ws_quantity else 0 end) as aug_net
  ,sum(case when d_moy = 9
    then ws_net_paid * ws_quantity else 0 end) as sep_net
  ,sum(case when d_moy = 10
    then ws_net_paid * ws_quantity else 0 end) as oct_net
  ,sum(case when d_moy = 11
    then ws_net_paid * ws_quantity else 0 end) as nov_net
  ,sum(case when d_moy = 12
    then ws_net_paid * ws_quantity else 0 end) as dec_net
     from
          web_sales
         ,warehouse
         ,date_dim
         ,time_dim
    ,ship_mode
     where
            ws_warehouse_sk =  w_warehouse_sk
        and ws_sold_date_sk = d_date_sk
        and ws_sold_time_sk = t_time_sk
  and ws_ship_mode_sk = sm_ship_mode_sk
        and d_year = 2001
  and t_time between 30838 and 30838+28800
  and sm_carrier in ('DHL','BARIAN')
     group by
        w_warehouse_name
  ,w_warehouse_sq_ft
  ,w_city
  ,w_county
  ,w_state
  ,w_country
       ,d_year
 union all
     select
  w_warehouse_name
  ,w_warehouse_sq_ft
  ,w_city
  ,w_county
  ,w_state
  ,w_country
  ,'DHL,BARIAN' as ship_carriers
       ,d_year as year_
  ,sum(case when d_moy = 1
    then cs_sales_price* cs_quantity else 0 end) as jan_sales
  ,sum(case when d_moy = 2
    then cs_sales_price* cs_quantity else 0 end) as feb_sales
  ,sum(case when d_moy = 3
    then cs_sales_price* cs_quantity else 0 end) as mar_sales
  ,sum(case when d_moy = 4
    then cs_sales_price* cs_quantity else 0 end) as apr_sales
  ,sum(case when d_moy = 5
    then cs_sales_price* cs_quantity else 0 end) as may_sales
  ,sum(case when d_moy = 6
    then cs_sales_price* cs_quantity else 0 end) as jun_sales
  ,sum(case when d_moy = 7
    then cs_sales_price* cs_quantity else 0 end) as jul_sales
  ,sum(case when d_moy = 8
    then cs_sales_price* cs_quantity else 0 end) as aug_sales
  ,sum(case when d_moy = 9
    then cs_sales_price* cs_quantity else 0 end) as sep_sales
  ,sum(case when d_moy = 10
    then cs_sales_price* cs_quantity else 0 end) as oct_sales
  ,sum(case when d_moy = 11
    then cs_sales_price* cs_quantity else 0 end) as nov_sales
  ,sum(case when d_moy = 12
    then cs_sales_price* cs_quantity else 0 end) as dec_sales
  ,sum(case when d_moy = 1
    then cs_net_paid_inc_tax * cs_quantity else 0 end) as jan_net
  ,sum(case when d_moy = 2
    then cs_net_paid_inc_tax * cs_quantity else 0 end) as feb_net
  ,sum(case when d_moy = 3
    then cs_net_paid_inc_tax * cs_quantity else 0 end) as mar_net
  ,sum(case when d_moy = 4
    then cs_net_paid_inc_tax * cs_quantity else 0 end) as apr_net
  ,sum(case when d_moy = 5
    then cs_net_paid_inc_tax * cs_quantity else 0 end) as may_net
  ,sum(case when d_moy = 6
    then cs_net_paid_inc_tax * cs_quantity else 0 end) as jun_net
  ,sum(case when d_moy = 7
    then cs_net_paid_inc_tax * cs_quantity else 0 end) as jul_net
  ,sum(case when d_moy = 8
    then cs_net_paid_inc_tax * cs_quantity else 0 end) as aug_net
  ,sum(case when d_moy = 9
    then cs_net_paid_inc_tax * cs_quantity else 0 end) as sep_net
  ,sum(case when d_moy = 10
    then cs_net_paid_inc_tax * cs_quantity else 0 end) as oct_net
  ,sum(case when d_moy = 11
    then cs_net_paid_inc_tax * cs_quantity else 0 end) as nov_net
  ,sum(case when d_moy = 12
    then cs_net_paid_inc_tax * cs_quantity else 0 end) as dec_net
     from
          catalog_sales
         ,warehouse
         ,date_dim
         ,time_dim
   ,ship_mode
     where
            cs_warehouse_sk =  w_warehouse_sk
        and cs_sold_date_sk = d_date_sk
        and cs_sold_time_sk = t_time_sk
  and cs_ship_mode_sk = sm_ship_mode_sk
        and d_year = 2001
  and t_time between 30838 AND 30838+28800
  and sm_carrier in ('DHL','BARIAN')
     group by
        w_warehouse_name
  ,w_warehouse_sq_ft
  ,w_city
  ,w_county
  ,w_state
  ,w_country
       ,d_year
 ) x
 group by
        w_warehouse_name
  ,w_warehouse_sq_ft
  ,w_city
  ,w_county
  ,w_state
  ,w_country
  ,ship_carriers
       ,year_
 order by w_warehouse_name NULLS FIRST
LIMIT 100;


Question:
SELECT *
FROM
  (SELECT i_category,
          i_class,
          i_brand,
          i_product_name,
          d_year,
          d_qoy,
          d_moy,
          s_store_id,
          sumsales,
          rank() OVER (PARTITION BY i_category
                       ORDER BY sumsales DESC) rk
   FROM
     (SELECT i_category,
             i_class,
             i_brand,
             i_product_name,
             d_year,
             d_qoy,
             d_moy,
             s_store_id,
             sum(coalesce(ss_sales_price*ss_quantity,0)) sumsales
      FROM store_sales,
           date_dim,
           store,
           item
      WHERE ss_sold_date_sk=d_date_sk
        AND ss_item_sk=i_item_sk
        AND ss_store_sk = s_store_sk
        AND d_month_seq BETWEEN 1200 AND 1200+11
      GROUP BY rollup(i_category, i_class, i_brand, i_product_name, d_year, d_qoy, d_moy,s_store_id))dw1) dw2
WHERE rk <= 100
ORDER BY i_category NULLS FIRST,
         i_class NULLS FIRST,
         i_brand NULLS FIRST,
         i_product_name NULLS FIRST,
         d_year NULLS FIRST,
         d_qoy NULLS FIRST,
         d_moy NULLS FIRST,
         s_store_id NULLS FIRST,
         sumsales NULLS FIRST,
         rk NULLS FIRST
LIMIT 100;


Question:
SELECT c_last_name,
       c_first_name,
       ca_city,
       bought_city,
       ss_ticket_number,
       extended_price,
       extended_tax,
       list_price
FROM
  (SELECT ss_ticket_number,
          ss_customer_sk,
          ca_city bought_city,
          sum(ss_ext_sales_price) extended_price,
          sum(ss_ext_list_price) list_price,
          sum(ss_ext_tax) extended_tax
   FROM store_sales,
        date_dim,
        store,
        household_demographics,
        customer_address
   WHERE store_sales.ss_sold_date_sk = date_dim.d_date_sk
     AND store_sales.ss_store_sk = store.s_store_sk
     AND store_sales.ss_hdemo_sk = household_demographics.hd_demo_sk
     AND store_sales.ss_addr_sk = customer_address.ca_address_sk
     AND date_dim.d_dom BETWEEN 1 AND 2
     AND (household_demographics.hd_dep_count = 4
          OR household_demographics.hd_vehicle_count= 3)
     AND date_dim.d_year IN (1999,
                             1999+1,
                             1999+2)
     AND store.s_city IN ('Fairview',
                          'Midway')
   GROUP BY ss_ticket_number,
            ss_customer_sk,
            ss_addr_sk,
            ca_city) dn,
     customer,
     customer_address current_addr
WHERE ss_customer_sk = c_customer_sk
  AND customer.c_current_addr_sk = current_addr.ca_address_sk
  AND current_addr.ca_city <> bought_city
ORDER BY c_last_name NULLS FIRST,
         ss_ticket_number NULLS FIRST
LIMIT 100;


Question:
SELECT cd_gender,
       cd_marital_status,
       cd_education_status,
       count(*) cnt1,
       cd_purchase_estimate,
       count(*) cnt2,
       cd_credit_rating,
       count(*) cnt3
FROM customer c,
     customer_address ca,
     customer_demographics
WHERE c.c_current_addr_sk = ca.ca_address_sk
  AND ca_state IN ('KY',
                   'GA',
                   'NM')
  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 = 2001
       AND d_moy BETWEEN 4 AND 4+2)
  AND (NOT 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 = 2001
            AND d_moy BETWEEN 4 AND 4+2)
       AND NOT 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 = 2001
            AND d_moy BETWEEN 4 AND 4+2))
GROUP BY cd_gender,
         cd_marital_status,
         cd_education_status,
         cd_purchase_estimate,
         cd_credit_rating
ORDER BY cd_gender,
         cd_marital_status,
         cd_education_status,
         cd_purchase_estimate,
         cd_credit_rating
LIMIT 100;


Question:
SELECT sum(ss_net_profit) AS total_sum,
       s_state,
       s_county,
       grouping(s_state)+grouping(s_county) AS lochierarchy,
       rank() OVER (PARTITION BY grouping(s_state)+grouping(s_county),
                                 CASE
                                     WHEN grouping(s_county) = 0 THEN s_state
                                 END
                    ORDER BY sum(ss_net_profit) DESC) AS rank_within_parent
FROM store_sales,
     date_dim d1,
     store
WHERE d1.d_month_seq BETWEEN 1200 AND 1200+11
  AND d1.d_date_sk = ss_sold_date_sk
  AND s_store_sk = ss_store_sk
  AND s_state IN
    (SELECT s_state
     FROM
       (SELECT s_state AS s_state,
               rank() OVER (PARTITION BY s_state
                            ORDER BY sum(ss_net_profit) DESC) AS ranking
        FROM store_sales,
             store,
             date_dim
        WHERE d_month_seq BETWEEN 1200 AND 1200+11
          AND d_date_sk = ss_sold_date_sk
          AND s_store_sk = ss_store_sk
        GROUP BY s_state) tmp1
     WHERE ranking <= 5 )
GROUP BY rollup(s_state,s_county)
ORDER BY lochierarchy DESC ,
         CASE
             WHEN grouping(s_state)+grouping(s_county) = 0 THEN s_state
         END ,
         rank_within_parent
LIMIT 100;


Question:
SELECT i_brand_id brand_id,
       i_brand brand,
       t_hour,
       t_minute,
       sum(ext_price) ext_price
FROM item,
  (SELECT ws_ext_sales_price AS ext_price,
          ws_sold_date_sk AS sold_date_sk,
          ws_item_sk AS sold_item_sk,
          ws_sold_time_sk AS time_sk
   FROM web_sales,
        date_dim
   WHERE d_date_sk = ws_sold_date_sk
     AND d_moy=11
     AND d_year=1999
   UNION ALL SELECT cs_ext_sales_price AS ext_price,
                    cs_sold_date_sk AS sold_date_sk,
                    cs_item_sk AS sold_item_sk,
                    cs_sold_time_sk AS time_sk
   FROM catalog_sales,
        date_dim
   WHERE d_date_sk = cs_sold_date_sk
     AND d_moy=11
     AND d_year=1999
   UNION ALL SELECT ss_ext_sales_price AS ext_price,
                    ss_sold_date_sk AS sold_date_sk,
                    ss_item_sk AS sold_item_sk,
                    ss_sold_time_sk AS time_sk
   FROM store_sales,
        date_dim
   WHERE d_date_sk = ss_sold_date_sk
     AND d_moy=11
     AND d_year=1999 ) tmp,
     time_dim
WHERE sold_item_sk = i_item_sk
  AND i_manager_id=1
  AND time_sk = t_time_sk
  AND (t_meal_time = 'breakfast'
       OR t_meal_time = 'dinner')
GROUP BY i_brand,
         i_brand_id,
         t_hour,
         t_minute
ORDER BY ext_price DESC NULLS FIRST,
         i_brand_id NULLS FIRST,
         t_hour NULLS FIRST;


Question:
SELECT i_item_desc,
       w_warehouse_name,
       d1.d_week_seq,
       sum(CASE
               WHEN p_promo_sk IS NULL THEN 1
               ELSE 0
           END) no_promo,
       sum(CASE
               WHEN p_promo_sk IS NOT NULL THEN 1
               ELSE 0
           END) promo,
       count(*) total_cnt
FROM catalog_sales
JOIN inventory ON (cs_item_sk = inv_item_sk)
JOIN warehouse ON (w_warehouse_sk=inv_warehouse_sk)
JOIN item ON (i_item_sk = cs_item_sk)
JOIN customer_demographics ON (cs_bill_cdemo_sk = cd_demo_sk)
JOIN household_demographics ON (cs_bill_hdemo_sk = hd_demo_sk)
JOIN date_dim d1 ON (cs_sold_date_sk = d1.d_date_sk)
JOIN date_dim d2 ON (inv_date_sk = d2.d_date_sk)
JOIN date_dim d3 ON (cs_ship_date_sk = d3.d_date_sk)
LEFT OUTER JOIN promotion ON (cs_promo_sk=p_promo_sk)
LEFT OUTER JOIN catalog_returns ON (cr_item_sk = cs_item_sk
                                    AND cr_order_number = cs_order_number)
WHERE d1.d_week_seq = d2.d_week_seq
  AND inv_quantity_on_hand < cs_quantity
  AND d3.d_date > d1.d_date + 5 -- SQL Server: DATEADD(day, 5, d1.d_date)
  AND hd_buy_potential = '>10000'
  AND d1.d_year = 1999
  AND cd_marital_status = 'D'
GROUP BY i_item_desc,
         w_warehouse_name,
         d1.d_week_seq
ORDER BY total_cnt DESC NULLS FIRST,
         i_item_desc NULLS FIRST,
         w_warehouse_name NULLS FIRST,
         d1.d_week_seq NULLS FIRST
LIMIT 100;


Question:
SELECT c_last_name,
       c_first_name,
       c_salutation,
       c_preferred_cust_flag,
       ss_ticket_number,
       cnt
FROM
  (SELECT ss_ticket_number,
          ss_customer_sk,
          count(*) cnt
   FROM store_sales,
        date_dim,
        store,
        household_demographics
   WHERE store_sales.ss_sold_date_sk = date_dim.d_date_sk
     AND store_sales.ss_store_sk = store.s_store_sk
     AND store_sales.ss_hdemo_sk = household_demographics.hd_demo_sk
     AND date_dim.d_dom BETWEEN 1 AND 2
     AND (household_demographics.hd_buy_potential = 'Unknown'
          OR household_demographics.hd_buy_potential = '>10000')
     AND household_demographics.hd_vehicle_count > 0
     AND CASE
             WHEN household_demographics.hd_vehicle_count > 0 THEN (household_demographics.hd_dep_count*1.000)/ household_demographics.hd_vehicle_count
             ELSE NULL
         END > 1
     AND date_dim.d_year IN (1999,
                             1999+1,
                             1999+2)
     AND store.s_county IN ('Orange County',
                            'Bronx County',
                            'Franklin Parish',
                            'Williamson County')
   GROUP BY ss_ticket_number,
            ss_customer_sk) dj,
     customer
WHERE ss_customer_sk = c_customer_sk
  AND cnt BETWEEN 1 AND 5
ORDER BY cnt DESC,
         c_last_name ASC;


Question:
WITH year_total AS
  (SELECT c_customer_id customer_id,
          c_first_name customer_first_name,
          c_last_name customer_last_name,
          d_year AS year_,
          sum(ss_net_paid) year_total,
          's' sale_type
   FROM customer,
        store_sales,
        date_dim
   WHERE c_customer_sk = ss_customer_sk
     AND ss_sold_date_sk = d_date_sk
     AND d_year IN (2001,
                    2001+1)
   GROUP BY c_customer_id,
            c_first_name,
            c_last_name,
            d_year
   UNION ALL SELECT c_customer_id customer_id,
                    c_first_name customer_first_name,
                    c_last_name customer_last_name,
                    d_year AS year_,
                    sum(ws_net_paid) year_total,
                    'w' sale_type
   FROM customer,
        web_sales,
        date_dim
   WHERE c_customer_sk = ws_bill_customer_sk
     AND ws_sold_date_sk = d_date_sk
     AND d_year IN (2001,
                    2001+1)
   GROUP BY c_customer_id,
            c_first_name,
            c_last_name,
            d_year)
SELECT t_s_secyear.customer_id,
       t_s_secyear.customer_first_name,
       t_s_secyear.customer_last_name
FROM year_total t_s_firstyear,
     year_total t_s_secyear,
     year_total t_w_firstyear,
     year_total t_w_secyear
WHERE t_s_secyear.customer_id = t_s_firstyear.customer_id
  AND t_s_firstyear.customer_id = t_w_secyear.customer_id
  AND t_s_firstyear.customer_id = t_w_firstyear.customer_id
  AND t_s_firstyear.sale_type = 's'
  AND t_w_firstyear.sale_type = 'w'
  AND t_s_secyear.sale_type = 's'
  AND t_w_secyear.sale_type = 'w'
  AND t_s_firstyear.year_ = 2001
  AND t_s_secyear.year_ = 2001+1
  AND t_w_firstyear.year_ = 2001
  AND t_w_secyear.year_ = 2001+1
  AND t_s_firstyear.year_total > 0
  AND t_w_firstyear.year_total > 0
  AND CASE
          WHEN t_w_firstyear.year_total > 0 THEN t_w_secyear.year_total / t_w_firstyear.year_total
          ELSE NULL
      END > CASE
                WHEN t_s_firstyear.year_total > 0 THEN t_s_secyear.year_total / t_s_firstyear.year_total
                ELSE NULL
            END
ORDER BY 1 NULLS FIRST
LIMIT 100;


Question:
WITH all_sales AS
  ( SELECT d_year ,
           i_brand_id ,
           i_class_id ,
           i_category_id ,
           i_manufact_id ,
           SUM(sales_cnt) AS sales_cnt ,
           SUM(sales_amt) AS sales_amt
   FROM
     (SELECT d_year ,
             i_brand_id ,
             i_class_id ,
             i_category_id ,
             i_manufact_id ,
             cs_quantity - COALESCE(cr_return_quantity,0) AS sales_cnt ,
             cs_ext_sales_price - COALESCE(cr_return_amount,0.0) AS sales_amt
      FROM catalog_sales
      JOIN item ON i_item_sk=cs_item_sk
      JOIN date_dim ON d_date_sk=cs_sold_date_sk
      LEFT JOIN catalog_returns ON (cs_order_number=cr_order_number
                                    AND cs_item_sk=cr_item_sk)
      WHERE i_category='Books'
      UNION SELECT d_year ,
                   i_brand_id ,
                   i_class_id ,
                   i_category_id ,
                   i_manufact_id ,
                   ss_quantity - COALESCE(sr_return_quantity,0) AS sales_cnt ,
                   ss_ext_sales_price - COALESCE(sr_return_amt,0.0) AS sales_amt
      FROM store_sales
      JOIN item ON i_item_sk=ss_item_sk
      JOIN date_dim ON d_date_sk=ss_sold_date_sk
      LEFT JOIN store_returns ON (ss_ticket_number=sr_ticket_number
                                  AND ss_item_sk=sr_item_sk)
      WHERE i_category='Books'
      UNION SELECT d_year ,
                   i_brand_id ,
                   i_class_id ,
                   i_category_id ,
                   i_manufact_id ,
                   ws_quantity - COALESCE(wr_return_quantity,0) AS sales_cnt ,
                   ws_ext_sales_price - COALESCE(wr_return_amt,0.0) AS sales_amt
      FROM web_sales
      JOIN item ON i_item_sk=ws_item_sk
      JOIN date_dim ON d_date_sk=ws_sold_date_sk
      LEFT JOIN web_returns ON (ws_order_number=wr_order_number
                                AND ws_item_sk=wr_item_sk)
      WHERE i_category='Books') sales_detail
   GROUP BY d_year,
            i_brand_id,
            i_class_id,
            i_category_id,
            i_manufact_id)
SELECT prev_yr.d_year AS prev_year ,
       curr_yr.d_year AS year_ ,
       curr_yr.i_brand_id ,
       curr_yr.i_class_id ,
       curr_yr.i_category_id ,
       curr_yr.i_manufact_id ,
       prev_yr.sales_cnt AS prev_yr_cnt ,
       curr_yr.sales_cnt AS curr_yr_cnt ,
       curr_yr.sales_cnt-prev_yr.sales_cnt AS sales_cnt_diff ,
       curr_yr.sales_amt-prev_yr.sales_amt AS sales_amt_diff
FROM all_sales curr_yr,
     all_sales prev_yr
WHERE curr_yr.i_brand_id=prev_yr.i_brand_id
  AND curr_yr.i_class_id=prev_yr.i_class_id
  AND curr_yr.i_category_id=prev_yr.i_category_id
  AND curr_yr.i_manufact_id=prev_yr.i_manufact_id
  AND curr_yr.d_year=2002
  AND prev_yr.d_year=2002-1
  AND CAST(curr_yr.sales_cnt AS DECIMAL(17,2))/CAST(prev_yr.sales_cnt AS DECIMAL(17,2))<0.9
ORDER BY sales_cnt_diff,
         sales_amt_diff
LIMIT 100;


Question:
SELECT channel,
       col_name,
       d_year,
       d_qoy,
       i_category,
       COUNT(*) sales_cnt,
       SUM(ext_sales_price) sales_amt
FROM
  ( SELECT 'store' AS channel,
           'ss_store_sk' col_name,
                         d_year,
                         d_qoy,
                         i_category,
                         ss_ext_sales_price ext_sales_price
   FROM store_sales,
        item,
        date_dim
   WHERE ss_store_sk IS NULL
     AND ss_sold_date_sk=d_date_sk
     AND ss_item_sk=i_item_sk
   UNION ALL SELECT 'web' AS channel,
                    'ws_ship_customer_sk' col_name,
                                          d_year,
                                          d_qoy,
                                          i_category,
                                          ws_ext_sales_price ext_sales_price
   FROM web_sales,
        item,
        date_dim
   WHERE ws_ship_customer_sk IS NULL
     AND ws_sold_date_sk=d_date_sk
     AND ws_item_sk=i_item_sk
   UNION ALL SELECT 'catalog' AS channel,
                    'cs_ship_addr_sk' col_name,
                                      d_year,
                                      d_qoy,
                                      i_category,
                                      cs_ext_sales_price ext_sales_price
   FROM catalog_sales,
        item,
        date_dim
   WHERE cs_ship_addr_sk IS NULL
     AND cs_sold_date_sk=d_date_sk
     AND cs_item_sk=i_item_sk) foo
GROUP BY channel,
         col_name,
         d_year,
         d_qoy,
         i_category
ORDER BY channel NULLS FIRST,
         col_name NULLS FIRST,
         d_year NULLS FIRST,
         d_qoy NULLS FIRST,
         i_category NULLS FIRST
LIMIT 100;


Question:
WITH ss AS
  (SELECT s_store_sk,
          sum(ss_ext_sales_price) AS sales,
          sum(ss_net_profit) AS profit
   FROM store_sales,
        date_dim,
        store
   WHERE ss_sold_date_sk = d_date_sk
     AND d_date BETWEEN cast('2000-08-23' AS date) AND cast('2000-09-22' AS date)
     AND ss_store_sk = s_store_sk
   GROUP BY s_store_sk) ,
     sr AS
  (SELECT s_store_sk,
          sum(sr_return_amt) AS returns_,
          sum(sr_net_loss) AS profit_loss
   FROM store_returns,
        date_dim,
        store
   WHERE sr_returned_date_sk = d_date_sk
     AND d_date BETWEEN cast('2000-08-23' AS date) AND cast('2000-09-22' AS date)
     AND sr_store_sk = s_store_sk
   GROUP BY s_store_sk),
     cs AS
  (SELECT cs_call_center_sk,
          sum(cs_ext_sales_price) AS sales,
          sum(cs_net_profit) AS profit
   FROM catalog_sales,
        date_dim
   WHERE cs_sold_date_sk = d_date_sk
     AND d_date BETWEEN cast('2000-08-23' AS date) AND cast('2000-09-22' AS date)
   GROUP BY cs_call_center_sk),
     cr AS
  (SELECT cr_call_center_sk,
          sum(cr_return_amount) AS returns_,
          sum(cr_net_loss) AS profit_loss
   FROM catalog_returns,
        date_dim
   WHERE cr_returned_date_sk = d_date_sk
     AND d_date BETWEEN cast('2000-08-23' AS date) AND cast('2000-09-22' AS date)
   GROUP BY cr_call_center_sk ),
     ws AS
  (SELECT wp_web_page_sk,
          sum(ws_ext_sales_price) AS sales,
          sum(ws_net_profit) AS profit
   FROM web_sales,
        date_dim,
        web_page
   WHERE ws_sold_date_sk = d_date_sk
     AND d_date BETWEEN cast('2000-08-23' AS date) AND cast('2000-09-22' AS date)
     AND ws_web_page_sk = wp_web_page_sk
   GROUP BY wp_web_page_sk),
     wr AS
  (SELECT wp_web_page_sk,
          sum(wr_return_amt) AS returns_,
          sum(wr_net_loss) AS profit_loss
   FROM web_returns,
        date_dim,
        web_page
   WHERE wr_returned_date_sk = d_date_sk
     AND d_date BETWEEN cast('2000-08-23' AS date) AND cast('2000-09-22' AS date)
     AND wr_web_page_sk = wp_web_page_sk
   GROUP BY wp_web_page_sk)
SELECT channel ,
       id ,
       sum(sales) AS sales ,
       sum(returns_) AS returns_ ,
       sum(profit) AS profit
FROM
  (SELECT 'store channel' AS channel ,
          ss.s_store_sk AS id ,
          sales ,
          coalesce(returns_, 0) AS returns_ ,
          (profit - coalesce(profit_loss,0)) AS profit
   FROM ss
   LEFT JOIN sr ON ss.s_store_sk = sr.s_store_sk
   UNION ALL SELECT 'catalog channel' AS channel ,
                    cs_call_center_sk AS id ,
                    sales ,
                    returns_ ,
                    (profit - profit_loss) AS profit
   FROM cs ,
        cr
   UNION ALL SELECT 'web channel' AS channel ,
                    ws.wp_web_page_sk AS id ,
                    sales ,
                    coalesce(returns_, 0) returns_ ,
                    (profit - coalesce(profit_loss,0)) AS profit
   FROM ws
   LEFT JOIN wr ON ws.wp_web_page_sk = wr.wp_web_page_sk ) x
GROUP BY ROLLUP (channel,
                 id)
ORDER BY channel NULLS FIRST,
         id NULLS FIRST,
         returns_ DESC
LIMIT 100;


Question:
WITH ws AS
  (SELECT d_year AS ws_sold_year,
          ws_item_sk,
          ws_bill_customer_sk ws_customer_sk,
          sum(ws_quantity) ws_qty,
          sum(ws_wholesale_cost) ws_wc,
          sum(ws_sales_price) ws_sp
   FROM web_sales
   LEFT JOIN web_returns ON wr_order_number=ws_order_number
   AND ws_item_sk=wr_item_sk
   JOIN date_dim ON ws_sold_date_sk = d_date_sk
   WHERE wr_order_number IS NULL
   GROUP BY d_year,
            ws_item_sk,
            ws_bill_customer_sk ),
     cs AS
  (SELECT d_year AS cs_sold_year,
          cs_item_sk,
          cs_bill_customer_sk cs_customer_sk,
          sum(cs_quantity) cs_qty,
          sum(cs_wholesale_cost) cs_wc,
          sum(cs_sales_price) cs_sp
   FROM catalog_sales
   LEFT JOIN catalog_returns ON cr_order_number=cs_order_number
   AND cs_item_sk=cr_item_sk
   JOIN date_dim ON cs_sold_date_sk = d_date_sk
   WHERE cr_order_number IS NULL
   GROUP BY d_year,
            cs_item_sk,
            cs_bill_customer_sk ),
     ss AS
  (SELECT d_year AS ss_sold_year,
          ss_item_sk,
          ss_customer_sk,
          sum(ss_quantity) ss_qty,
          sum(ss_wholesale_cost) ss_wc,
          sum(ss_sales_price) ss_sp
   FROM store_sales
   LEFT JOIN store_returns ON sr_ticket_number=ss_ticket_number
   AND ss_item_sk=sr_item_sk
   JOIN date_dim ON ss_sold_date_sk = d_date_sk
   WHERE sr_ticket_number IS NULL
   GROUP BY d_year,
            ss_item_sk,
            ss_customer_sk )
SELECT ss_sold_year,
       ss_item_sk,
       ss_customer_sk,
       round((ss_qty*1.00)/(coalesce(ws_qty,0)+coalesce(cs_qty,0)),2) ratio,
       ss_qty store_qty,
       ss_wc store_wholesale_cost,
       ss_sp store_sales_price,
       coalesce(ws_qty,0)+coalesce(cs_qty,0) other_chan_qty,
       coalesce(ws_wc,0)+coalesce(cs_wc,0) other_chan_wholesale_cost,
       coalesce(ws_sp,0)+coalesce(cs_sp,0) other_chan_sales_price
FROM ss
LEFT JOIN ws ON (ws_sold_year=ss_sold_year
                 AND ws_item_sk=ss_item_sk
                 AND ws_customer_sk=ss_customer_sk)
LEFT JOIN cs ON (cs_sold_year=ss_sold_year
                 AND cs_item_sk=ss_item_sk
                 AND cs_customer_sk=ss_customer_sk)
WHERE (coalesce(ws_qty,0)>0
       OR coalesce(cs_qty, 0)>0)
  AND ss_sold_year=2000
ORDER BY ss_sold_year,
         ss_item_sk,
         ss_customer_sk,
         ss_qty DESC,
         ss_wc DESC,
         ss_sp DESC,
         other_chan_qty,
         other_chan_wholesale_cost,
         other_chan_sales_price,
         ratio
LIMIT 100;


Question:
SELECT c_last_name,
       c_first_name,
       SUBSTRING(s_city,1,30),
       ss_ticket_number,
       amt,
       profit
FROM
  (SELECT ss_ticket_number ,
          ss_customer_sk ,
          store.s_city ,
          sum(ss_coupon_amt) amt ,
          sum(ss_net_profit) profit
   FROM store_sales,
        date_dim,
        store,
        household_demographics
   WHERE store_sales.ss_sold_date_sk = date_dim.d_date_sk
     AND store_sales.ss_store_sk = store.s_store_sk
     AND store_sales.ss_hdemo_sk = household_demographics.hd_demo_sk
     AND (household_demographics.hd_dep_count = 6
          OR household_demographics.hd_vehicle_count > 2)
     AND date_dim.d_dow = 1
     AND date_dim.d_year IN (1999,
                             1999+1,
                             1999+2)
     AND store.s_number_employees BETWEEN 200 AND 295
   GROUP BY ss_ticket_number,
            ss_customer_sk,
            ss_addr_sk,
            store.s_city) ms,
     customer
WHERE ss_customer_sk = c_customer_sk
ORDER BY c_last_name  NULLS FIRST,
         c_first_name  NULLS FIRST,
         SUBSTRING(s_city,1,30)  NULLS FIRST,
         profit NULLS FIRST,
         ss_ticket_number
LIMIT 100;


Question:
WITH ssr AS
  (SELECT s_store_id AS store_id,
          sum(ss_ext_sales_price) AS sales,
          sum(coalesce(sr_return_amt, 0)) AS returns_,
          sum(ss_net_profit - coalesce(sr_net_loss, 0)) AS profit
   FROM store_sales
   LEFT OUTER JOIN store_returns ON (ss_item_sk = sr_item_sk
                                     AND ss_ticket_number = sr_ticket_number), date_dim,
                                                                               store,
                                                                               item,
                                                                               promotion
   WHERE ss_sold_date_sk = d_date_sk
     AND d_date BETWEEN cast('2000-08-23' AS date) AND cast('2000-09-22' AS date)
     AND ss_store_sk = s_store_sk
     AND ss_item_sk = i_item_sk
     AND i_current_price > 50
     AND ss_promo_sk = p_promo_sk
     AND p_channel_tv = 'N'
   GROUP BY s_store_id) ,
     csr AS
  (SELECT cp_catalog_page_id AS catalog_page_id,
          sum(cs_ext_sales_price) AS sales,
          sum(coalesce(cr_return_amount, 0)) AS returns_,
          sum(cs_net_profit - coalesce(cr_net_loss, 0)) AS profit
   FROM catalog_sales
   LEFT OUTER JOIN catalog_returns ON (cs_item_sk = cr_item_sk
                                       AND cs_order_number = cr_order_number), date_dim,
                                                                               catalog_page,
                                                                               item,
                                                                               promotion
   WHERE cs_sold_date_sk = d_date_sk
     AND d_date BETWEEN cast('2000-08-23' AS date) AND cast('2000-09-22' AS date)
     AND cs_catalog_page_sk = cp_catalog_page_sk
     AND cs_item_sk = i_item_sk
     AND i_current_price > 50
     AND cs_promo_sk = p_promo_sk
     AND p_channel_tv = 'N'
   GROUP BY cp_catalog_page_id) ,
     wsr AS
  (SELECT web_site_id,
          sum(ws_ext_sales_price) AS sales,
          sum(coalesce(wr_return_amt, 0)) AS returns_,
          sum(ws_net_profit - coalesce(wr_net_loss, 0)) AS profit
   FROM web_sales
   LEFT OUTER JOIN web_returns ON (ws_item_sk = wr_item_sk
                                   AND ws_order_number = wr_order_number), date_dim,
                                                                           web_site,
                                                                           item,
                                                                           promotion
   WHERE ws_sold_date_sk = d_date_sk
     AND d_date BETWEEN cast('2000-08-23' AS date) AND cast('2000-09-22' AS date)
     AND ws_web_site_sk = web_site_sk
     AND ws_item_sk = i_item_sk
     AND i_current_price > 50
     AND ws_promo_sk = p_promo_sk
     AND p_channel_tv = 'N'
   GROUP BY web_site_id)
SELECT channel ,
       id ,
       sum(sales) AS sales ,
       sum(returns_) AS returns_ ,
       sum(profit) AS profit
FROM
  (SELECT 'store channel' AS channel ,
          concat('store', store_id) AS id ,
          sales ,
          returns_ ,
          profit
   FROM ssr
   UNION ALL SELECT 'catalog channel' AS channel ,
                    concat('catalog_page', catalog_page_id) AS id ,
                    sales ,
                    returns_ ,
                    profit
   FROM csr
   UNION ALL SELECT 'web channel' AS channel ,
                    concat('web_site', web_site_id) AS id ,
                    sales ,
                    returns_ ,
                    profit
   FROM wsr ) x
GROUP BY ROLLUP (channel,
                 id)
ORDER BY channel NULLS FIRST,
         id NULLS FIRST
LIMIT 100;


Question:
WITH customer_total_return AS
  (SELECT cr_returning_customer_sk AS ctr_customer_sk ,
          ca_state AS ctr_state,
          sum(cr_return_amt_inc_tax) AS ctr_total_return
   FROM catalog_returns ,
        date_dim ,
        customer_address
   WHERE cr_returned_date_sk = d_date_sk
     AND d_year = 2000
     AND cr_returning_addr_sk = ca_address_sk
   GROUP BY cr_returning_customer_sk ,
            ca_state)
SELECT c_customer_id,
       c_salutation,
       c_first_name,
       c_last_name,
       ca_street_number,
       ca_street_name ,
       ca_street_type,
       ca_suite_number,
       ca_city,
       ca_county,
       ca_state,
       ca_zip,
       ca_country,
       ca_gmt_offset ,
       ca_location_type,
       ctr_total_return
FROM customer_total_return ctr1 ,
     customer_address ,
     customer
WHERE ctr1.ctr_total_return >
    (SELECT avg(ctr_total_return)*1.2
     FROM customer_total_return ctr2
     WHERE ctr1.ctr_state = ctr2.ctr_state)
  AND ca_address_sk = c_current_addr_sk
  AND ca_state = 'GA'
  AND ctr1.ctr_customer_sk = c_customer_sk
ORDER BY c_customer_id,
         c_salutation,
         c_first_name,
         c_last_name,
         ca_street_number,
         ca_street_name ,
         ca_street_type,
         ca_suite_number,
         ca_city,
         ca_county,
         ca_state,
         ca_zip,
         ca_country,
         ca_gmt_offset ,
         ca_location_type,
         ctr_total_return
LIMIT 100;


Question:
SELECT i_item_id ,
       i_item_desc ,
       i_current_price
FROM item,
     inventory,
     date_dim,
     store_sales
WHERE i_current_price BETWEEN 62 AND 62+30
  AND inv_item_sk = i_item_sk
  AND d_date_sk=inv_date_sk
  AND d_date BETWEEN cast('2000-05-25' AS date) AND cast('2000-07-24' AS date)
  AND i_manufact_id IN (129,
                        270,
                        821,
                        423)
  AND inv_quantity_on_hand BETWEEN 100 AND 500
  AND ss_item_sk = i_item_sk
GROUP BY i_item_id,
         i_item_desc,
         i_current_price
ORDER BY i_item_id
LIMIT 100;


Question:
WITH sr_items AS
  (SELECT i_item_id item_id,
          sum(sr_return_quantity) sr_item_qty
   FROM store_returns,
        item,
        date_dim
   WHERE sr_item_sk = i_item_sk
     AND d_date IN
       (SELECT d_date
        FROM date_dim
        WHERE d_week_seq IN
            (SELECT d_week_seq
             FROM date_dim
             WHERE d_date IN ('2000-06-30',
                              '2000-09-27',
                              '2000-11-17')))
     AND sr_returned_date_sk = d_date_sk
   GROUP BY i_item_id),
     cr_items AS
  (SELECT i_item_id item_id,
          sum(cr_return_quantity) cr_item_qty
   FROM catalog_returns,
        item,
        date_dim
   WHERE cr_item_sk = i_item_sk
     AND d_date IN
       (SELECT d_date
        FROM date_dim
        WHERE d_week_seq IN
            (SELECT d_week_seq
             FROM date_dim
             WHERE d_date IN ('2000-06-30',
                              '2000-09-27',
                              '2000-11-17')))
     AND cr_returned_date_sk = d_date_sk
   GROUP BY i_item_id),
     wr_items AS
  (SELECT i_item_id item_id,
          sum(wr_return_quantity) wr_item_qty
   FROM web_returns,
        item,
        date_dim
   WHERE wr_item_sk = i_item_sk
     AND d_date IN
       (SELECT d_date
        FROM date_dim
        WHERE d_week_seq IN
            (SELECT d_week_seq
             FROM date_dim
             WHERE d_date IN ('2000-06-30',
                              '2000-09-27',
                              '2000-11-17')))
     AND wr_returned_date_sk = d_date_sk
   GROUP BY i_item_id)
SELECT sr_items.item_id ,
       sr_item_qty ,
       (sr_item_qty*1.0000)/(sr_item_qty+cr_item_qty+wr_item_qty)/3.0000 * 100 sr_dev ,
       cr_item_qty ,
       (cr_item_qty*1.0000)/(sr_item_qty+cr_item_qty+wr_item_qty)/3.0000 * 100 cr_dev ,
       wr_item_qty ,
       (wr_item_qty*1.0000)/(sr_item_qty+cr_item_qty+wr_item_qty)/3.0000 * 100 wr_dev ,
       (sr_item_qty+cr_item_qty+wr_item_qty)/3.0 average
FROM sr_items ,
     cr_items ,
     wr_items
WHERE sr_items.item_id=cr_items.item_id
  AND sr_items.item_id=wr_items.item_id
ORDER BY sr_items.item_id NULLS FIRST,
         sr_item_qty NULLS FIRST
LIMIT 100;


Question:
SELECT c_customer_id AS customer_id ,
       concat(concat(coalesce(c_last_name, '') , ', '), coalesce(c_first_name, '')) AS customername
FROM customer ,
     customer_address ,
     customer_demographics ,
     household_demographics ,
     income_band ,
     store_returns
WHERE ca_city = 'Edgewood'
  AND c_current_addr_sk = ca_address_sk
  AND ib_lower_bound >= 38128
  AND ib_upper_bound <= 38128 + 50000
  AND ib_income_band_sk = hd_income_band_sk
  AND cd_demo_sk = c_current_cdemo_sk
  AND hd_demo_sk = c_current_hdemo_sk
  AND sr_cdemo_sk = cd_demo_sk
ORDER BY c_customer_id NULLS FIRST
LIMIT 100;


Question:
SELECT SUBSTRING(r_reason_desc,1,20) ,
       avg(ws_quantity) avg1,
       avg(wr_refunded_cash) avg2,
       avg(wr_fee)
FROM web_sales,
     web_returns,
     web_page,
     customer_demographics cd1,
     customer_demographics cd2,
     customer_address,
     date_dim,
     reason
WHERE ws_web_page_sk = wp_web_page_sk
  AND ws_item_sk = wr_item_sk
  AND ws_order_number = wr_order_number
  AND ws_sold_date_sk = d_date_sk
  AND d_year = 2000
  AND cd1.cd_demo_sk = wr_refunded_cdemo_sk
  AND cd2.cd_demo_sk = wr_returning_cdemo_sk
  AND ca_address_sk = wr_refunded_addr_sk
  AND r_reason_sk = wr_reason_sk
  AND ( ( cd1.cd_marital_status = 'M'
         AND cd1.cd_marital_status = cd2.cd_marital_status
         AND cd1.cd_education_status = 'Advanced Degree'
         AND cd1.cd_education_status = cd2.cd_education_status
         AND ws_sales_price BETWEEN 100.00 AND 150.00 )
       OR ( cd1.cd_marital_status = 'S'
           AND cd1.cd_marital_status = cd2.cd_marital_status
           AND cd1.cd_education_status = 'College'
           AND cd1.cd_education_status = cd2.cd_education_status
           AND ws_sales_price BETWEEN 50.00 AND 100.00 )
       OR ( cd1.cd_marital_status = 'W'
           AND cd1.cd_marital_status = cd2.cd_marital_status
           AND cd1.cd_education_status = '2 yr Degree'
           AND cd1.cd_education_status = cd2.cd_education_status
           AND ws_sales_price BETWEEN 150.00 AND 200.00 ) )
  AND ( ( ca_country = 'United States'
         AND ca_state IN ('IN',
                          'OH',
                          'NJ')
         AND ws_net_profit BETWEEN 100 AND 200)
       OR ( ca_country = 'United States'
           AND ca_state IN ('WI',
                            'CT',
                            'KY')
           AND ws_net_profit BETWEEN 150 AND 300)
       OR ( ca_country = 'United States'
           AND ca_state IN ('LA',
                            'IA',
                            'AR')
           AND ws_net_profit BETWEEN 50 AND 250) )
GROUP BY r_reason_desc
ORDER BY SUBSTRING(r_reason_desc,1,20) ,
         avg(ws_quantity) ,
         avg(wr_refunded_cash) ,
         avg(wr_fee)
LIMIT 100;


Question:
SELECT sum(ws_net_paid) AS total_sum ,
       i_category ,
       i_class ,
       grouping(i_category)+grouping(i_class) AS lochierarchy ,
       rank() OVER ( PARTITION BY grouping(i_category)+grouping(i_class),
                                  CASE
                                      WHEN grouping(i_class) = 0 THEN i_category
                                  END
                    ORDER BY sum(ws_net_paid) DESC) AS rank_within_parent
FROM web_sales ,
     date_dim d1 ,
     item
WHERE d1.d_month_seq BETWEEN 1200 AND 1200+11
  AND d1.d_date_sk = ws_sold_date_sk
  AND i_item_sk = ws_item_sk
GROUP BY rollup(i_category,i_class)
ORDER BY lochierarchy DESC NULLS FIRST,
         CASE
             WHEN grouping(i_category)+grouping(i_class) = 0 THEN i_category
         END NULLS FIRST,
         rank_within_parent NULLS FIRST
LIMIT 100;


Question:
SELECT count(*)
FROM ((SELECT DISTINCT c_last_name,
                         c_first_name,
                         d_date
         FROM store_sales,
              date_dim,
              customer
         WHERE store_sales.ss_sold_date_sk = date_dim.d_date_sk
           AND store_sales.ss_customer_sk = customer.c_customer_sk
           AND d_month_seq BETWEEN 1200 AND 1200+11)
      EXCEPT
        (SELECT DISTINCT c_last_name,
                         c_first_name,
                         d_date
         FROM catalog_sales,
              date_dim,
              customer
         WHERE catalog_sales.cs_sold_date_sk = date_dim.d_date_sk
           AND catalog_sales.cs_bill_customer_sk = customer.c_customer_sk
           AND d_month_seq BETWEEN 1200 AND 1200+11)
      EXCEPT
        (SELECT DISTINCT c_last_name,
                         c_first_name,
                         d_date
         FROM web_sales,
              date_dim,
              customer
         WHERE web_sales.ws_sold_date_sk = date_dim.d_date_sk
           AND web_sales.ws_bill_customer_sk = customer.c_customer_sk
           AND d_month_seq BETWEEN 1200 AND 1200+11)) cool_cust ;


Question:
SELECT *
FROM
  (SELECT count(*) h8_30_to_9
   FROM store_sales,
        household_demographics,
        time_dim,
        store
   WHERE ss_sold_time_sk = time_dim.t_time_sk
     AND ss_hdemo_sk = household_demographics.hd_demo_sk
     AND ss_store_sk = s_store_sk
     AND time_dim.t_hour = 8
     AND time_dim.t_minute >= 30
     AND ((household_demographics.hd_dep_count = 4
           AND household_demographics.hd_vehicle_count<=4+2)
          OR (household_demographics.hd_dep_count = 2
              AND household_demographics.hd_vehicle_count<=2+2)
          OR (household_demographics.hd_dep_count = 0
              AND household_demographics.hd_vehicle_count<=0+2))
     AND store.s_store_name = 'ese') s1,
  (SELECT count(*) h9_to_9_30
   FROM store_sales,
        household_demographics,
        time_dim,
        store
   WHERE ss_sold_time_sk = time_dim.t_time_sk
     AND ss_hdemo_sk = household_demographics.hd_demo_sk
     AND ss_store_sk = s_store_sk
     AND time_dim.t_hour = 9
     AND time_dim.t_minute < 30
     AND ((household_demographics.hd_dep_count = 4
           AND household_demographics.hd_vehicle_count<=4+2)
          OR (household_demographics.hd_dep_count = 2
              AND household_demographics.hd_vehicle_count<=2+2)
          OR (household_demographics.hd_dep_count = 0
              AND household_demographics.hd_vehicle_count<=0+2))
     AND store.s_store_name = 'ese') s2,
  (SELECT count(*) h9_30_to_10
   FROM store_sales,
        household_demographics,
        time_dim,
        store
   WHERE ss_sold_time_sk = time_dim.t_time_sk
     AND ss_hdemo_sk = household_demographics.hd_demo_sk
     AND ss_store_sk = s_store_sk
     AND time_dim.t_hour = 9
     AND time_dim.t_minute >= 30
     AND ((household_demographics.hd_dep_count = 4
           AND household_demographics.hd_vehicle_count<=4+2)
          OR (household_demographics.hd_dep_count = 2
              AND household_demographics.hd_vehicle_count<=2+2)
          OR (household_demographics.hd_dep_count = 0
              AND household_demographics.hd_vehicle_count<=0+2))
     AND store.s_store_name = 'ese') s3,
  (SELECT count(*) h10_to_10_30
   FROM store_sales,
        household_demographics,
        time_dim,
        store
   WHERE ss_sold_time_sk = time_dim.t_time_sk
     AND ss_hdemo_sk = household_demographics.hd_demo_sk
     AND ss_store_sk = s_store_sk
     AND time_dim.t_hour = 10
     AND time_dim.t_minute < 30
     AND ((household_demographics.hd_dep_count = 4
           AND household_demographics.hd_vehicle_count<=4+2)
          OR (household_demographics.hd_dep_count = 2
              AND household_demographics.hd_vehicle_count<=2+2)
          OR (household_demographics.hd_dep_count = 0
              AND household_demographics.hd_vehicle_count<=0+2))
     AND store.s_store_name = 'ese') s4,
  (SELECT count(*) h10_30_to_11
   FROM store_sales,
        household_demographics,
        time_dim,
        store
   WHERE ss_sold_time_sk = time_dim.t_time_sk
     AND ss_hdemo_sk = household_demographics.hd_demo_sk
     AND ss_store_sk = s_store_sk
     AND time_dim.t_hour = 10
     AND time_dim.t_minute >= 30
     AND ((household_demographics.hd_dep_count = 4
           AND household_demographics.hd_vehicle_count<=4+2)
          OR (household_demographics.hd_dep_count = 2
              AND household_demographics.hd_vehicle_count<=2+2)
          OR (household_demographics.hd_dep_count = 0
              AND household_demographics.hd_vehicle_count<=0+2))
     AND store.s_store_name = 'ese') s5,
  (SELECT count(*) h11_to_11_30
   FROM store_sales,
        household_demographics,
        time_dim,
        store
   WHERE ss_sold_time_sk = time_dim.t_time_sk
     AND ss_hdemo_sk = household_demographics.hd_demo_sk
     AND ss_store_sk = s_store_sk
     AND time_dim.t_hour = 11
     AND time_dim.t_minute < 30
     AND ((household_demographics.hd_dep_count = 4
           AND household_demographics.hd_vehicle_count<=4+2)
          OR (household_demographics.hd_dep_count = 2
              AND household_demographics.hd_vehicle_count<=2+2)
          OR (household_demographics.hd_dep_count = 0
              AND household_demographics.hd_vehicle_count<=0+2))
     AND store.s_store_name = 'ese') s6,
  (SELECT count(*) h11_30_to_12
   FROM store_sales,
        household_demographics,
        time_dim,
        store
   WHERE ss_sold_time_sk = time_dim.t_time_sk
     AND ss_hdemo_sk = household_demographics.hd_demo_sk
     AND ss_store_sk = s_store_sk
     AND time_dim.t_hour = 11
     AND time_dim.t_minute >= 30
     AND ((household_demographics.hd_dep_count = 4
           AND household_demographics.hd_vehicle_count<=4+2)
          OR (household_demographics.hd_dep_count = 2
              AND household_demographics.hd_vehicle_count<=2+2)
          OR (household_demographics.hd_dep_count = 0
              AND household_demographics.hd_vehicle_count<=0+2))
     AND store.s_store_name = 'ese') s7,
  (SELECT count(*) h12_to_12_30
   FROM store_sales,
        household_demographics,
        time_dim,
        store
   WHERE ss_sold_time_sk = time_dim.t_time_sk
     AND ss_hdemo_sk = household_demographics.hd_demo_sk
     AND ss_store_sk = s_store_sk
     AND time_dim.t_hour = 12
     AND time_dim.t_minute < 30
     AND ((household_demographics.hd_dep_count = 4
           AND household_demographics.hd_vehicle_count<=4+2)
          OR (household_demographics.hd_dep_count = 2
              AND household_demographics.hd_vehicle_count<=2+2)
          OR (household_demographics.hd_dep_count = 0
              AND household_demographics.hd_vehicle_count<=0+2))
     AND store.s_store_name = 'ese') s8 ;


Question:
SELECT * from
  (SELECT i_category, i_class, i_brand, s_store_name, s_company_name, d_moy, sum(ss_sales_price) sum_sales, avg(sum(ss_sales_price)) OVER (PARTITION BY i_category, i_brand, s_store_name, s_company_name) avg_monthly_sales
   FROM item, store_sales, date_dim, store
   WHERE ss_item_sk = i_item_sk
     AND ss_sold_date_sk = d_date_sk
     AND ss_store_sk = s_store_sk
     AND d_year = 1999
     AND ((i_category IN ('Books','Electronics','Sports')
           AND i_class IN ('computers','stereo','football') )
          OR (i_category IN ('Men','Jewelry','Women')
              AND i_class IN ('shirts','birdal','dresses')))
   GROUP BY i_category, i_class, i_brand, s_store_name, s_company_name, d_moy) tmp1
WHERE CASE
          WHEN (avg_monthly_sales <> 0) THEN (abs(sum_sales - avg_monthly_sales) / avg_monthly_sales)
          ELSE NULL
      END > 0.1
ORDER BY sum_sales - avg_monthly_sales,
         s_store_name, 1, 2, 3, 5, 6, 7, 8
LIMIT 100;


Question:
SELECT case when pmc=0 then null else cast(amc AS decimal(15,4))/cast(pmc AS decimal(15,4)) end am_pm_ratio
FROM
  (SELECT count(*) amc
   FROM web_sales,
        household_demographics,
        time_dim,
        web_page
   WHERE ws_sold_time_sk = time_dim.t_time_sk
     AND ws_ship_hdemo_sk = household_demographics.hd_demo_sk
     AND ws_web_page_sk = web_page.wp_web_page_sk
     AND time_dim.t_hour BETWEEN 8 AND 8+1
     AND household_demographics.hd_dep_count = 6
     AND web_page.wp_char_count BETWEEN 5000 AND 5200) at_,
  (SELECT count(*) pmc
   FROM web_sales,
        household_demographics,
        time_dim,
        web_page
   WHERE ws_sold_time_sk = time_dim.t_time_sk
     AND ws_ship_hdemo_sk = household_demographics.hd_demo_sk
     AND ws_web_page_sk = web_page.wp_web_page_sk
     AND time_dim.t_hour BETWEEN 19 AND 19+1
     AND household_demographics.hd_dep_count = 6
     AND web_page.wp_char_count BETWEEN 5000 AND 5200) pt
ORDER BY am_pm_ratio
LIMIT 100;


Question:
SELECT cc_call_center_id Call_Center,
       cc_name Call_Center_Name,
       cc_manager Manager,
       sum(cr_net_loss) Returns_Loss
FROM call_center,
     catalog_returns,
     date_dim,
     customer,
     customer_address,
     customer_demographics,
     household_demographics
WHERE cr_call_center_sk = cc_call_center_sk
  AND cr_returned_date_sk = d_date_sk
  AND cr_returning_customer_sk= c_customer_sk
  AND cd_demo_sk = c_current_cdemo_sk
  AND hd_demo_sk = c_current_hdemo_sk
  AND ca_address_sk = c_current_addr_sk
  AND d_year = 1998
  AND d_moy = 11
  AND ((cd_marital_status = 'M'
        AND cd_education_status = 'Unknown') or(cd_marital_status = 'W'
                                                AND cd_education_status = 'Advanced Degree'))
  AND hd_buy_potential LIKE 'Unknown%'
  AND ca_gmt_offset = -7
GROUP BY cc_call_center_id,
         cc_name,
         cc_manager,
         cd_marital_status,
         cd_education_status
ORDER BY sum(cr_net_loss) DESC;


Question:
SELECT sum(ws_ext_discount_amt) AS "Excess Discount Amount"
FROM web_sales,
     item,
     date_dim
WHERE i_manufact_id = 350
  AND i_item_sk = ws_item_sk
  AND d_date BETWEEN '2000-01-27' AND cast('2000-04-26' AS date)
  AND d_date_sk = ws_sold_date_sk
  AND ws_ext_discount_amt >
    (SELECT 1.3 * avg(ws_ext_discount_amt)
     FROM web_sales,
          date_dim
     WHERE ws_item_sk = i_item_sk
       AND d_date BETWEEN '2000-01-27' AND cast('2000-04-26' AS date)
       AND d_date_sk = ws_sold_date_sk )
ORDER BY sum(ws_ext_discount_amt)
LIMIT 100;


Question:
SELECT ss_customer_sk,
       sum(act_sales) sumsales
FROM
  (SELECT ss_item_sk,
          ss_ticket_number,
          ss_customer_sk,
          CASE
              WHEN sr_return_quantity IS NOT NULL THEN (ss_quantity-sr_return_quantity)*ss_sales_price
              ELSE (ss_quantity*ss_sales_price)
          END act_sales
   FROM store_sales
   LEFT OUTER JOIN store_returns ON (sr_item_sk = ss_item_sk
                                     AND sr_ticket_number = ss_ticket_number) ,reason
   WHERE sr_reason_sk = r_reason_sk
     AND r_reason_desc = 'reason 28') t
GROUP BY ss_customer_sk
ORDER BY sumsales NULLS FIRST,
         ss_customer_sk NULLS FIRST
LIMIT 100;


Question:
SELECT count(DISTINCT ws_order_number) AS "order count" ,
       sum(ws_ext_ship_cost) AS "total shipping cost" ,
       sum(ws_net_profit) AS "total net profit"
FROM web_sales ws1 ,
     date_dim ,
     customer_address ,
     web_site
WHERE d_date BETWEEN '1999-02-01' AND cast('1999-04-02' AS date)
  AND ws1.ws_ship_date_sk = d_date_sk
  AND ws1.ws_ship_addr_sk = ca_address_sk
  AND ca_state = 'IL'
  AND ws1.ws_web_site_sk = web_site_sk
  AND web_company_name = 'pri'
  AND EXISTS
    (SELECT *
     FROM web_sales ws2
     WHERE ws1.ws_order_number = ws2.ws_order_number
       AND ws1.ws_warehouse_sk <> ws2.ws_warehouse_sk)
  AND NOT exists
    (SELECT *
     FROM web_returns wr1
     WHERE ws1.ws_order_number = wr1.wr_order_number)
ORDER BY count(DISTINCT ws_order_number)
LIMIT 100;


Question:
WITH ws_wh AS
  (SELECT ws1.ws_order_number,
          ws1.ws_warehouse_sk wh1,
          ws2.ws_warehouse_sk wh2
   FROM web_sales ws1,
        web_sales ws2
   WHERE ws1.ws_order_number = ws2.ws_order_number
     AND ws1.ws_warehouse_sk <> ws2.ws_warehouse_sk)
SELECT count(DISTINCT ws_order_number) AS "order count" ,
       sum(ws_ext_ship_cost) AS "total shipping cost" ,
       sum(ws_net_profit) AS "total net profit"
FROM web_sales ws1 ,
     date_dim ,
     customer_address ,
     web_site
WHERE d_date BETWEEN '1999-02-01' AND cast('1999-04-02' AS date)
  AND ws1.ws_ship_date_sk = d_date_sk
  AND ws1.ws_ship_addr_sk = ca_address_sk
  AND ca_state = 'IL'
  AND ws1.ws_web_site_sk = web_site_sk
  AND web_company_name = 'pri'
  AND ws1.ws_order_number IN
    (SELECT ws_order_number
     FROM ws_wh)
  AND ws1.ws_order_number IN
    (SELECT wr_order_number
     FROM web_returns,
          ws_wh
     WHERE wr_order_number = ws_wh.ws_order_number)
ORDER BY count(DISTINCT ws_order_number)
LIMIT 100;


Question:
SELECT count(*)
FROM store_sales ,
     household_demographics,
     time_dim,
     store
WHERE ss_sold_time_sk = time_dim.t_time_sk
  AND ss_hdemo_sk = household_demographics.hd_demo_sk
  AND ss_store_sk = s_store_sk
  AND time_dim.t_hour = 20
  AND time_dim.t_minute >= 30
  AND household_demographics.hd_dep_count = 7
  AND store.s_store_name = 'ese'
ORDER BY count(*)
LIMIT 100;


Question:
WITH ssci AS
  (SELECT ss_customer_sk customer_sk ,
          ss_item_sk item_sk
   FROM store_sales,
        date_dim
   WHERE ss_sold_date_sk = d_date_sk
     AND d_month_seq BETWEEN 1200 AND 1200 + 11
   GROUP BY ss_customer_sk ,
            ss_item_sk),
     csci as
  ( SELECT cs_bill_customer_sk customer_sk ,cs_item_sk item_sk
   FROM catalog_sales,date_dim
   WHERE cs_sold_date_sk = d_date_sk
     AND d_month_seq BETWEEN 1200 AND 1200 + 11
   GROUP BY cs_bill_customer_sk ,cs_item_sk)
SELECT sum(CASE
               WHEN ssci.customer_sk IS NOT NULL
                    AND csci.customer_sk IS NULL THEN 1
               ELSE 0
           END) store_only ,
       sum(CASE
               WHEN ssci.customer_sk IS NULL
                    AND csci.customer_sk IS NOT NULL THEN 1
               ELSE 0
           END) catalog_only ,
       sum(CASE
               WHEN ssci.customer_sk IS NOT NULL
                    AND csci.customer_sk IS NOT NULL THEN 1
               ELSE 0
           END) store_and_catalog
FROM ssci
FULL OUTER JOIN csci ON (ssci.customer_sk=csci.customer_sk
                         AND ssci.item_sk = csci.item_sk)
LIMIT 100;


Question:
SELECT i_item_id ,
       i_item_desc,
       i_category,
       i_class,
       i_current_price ,
       sum(ss_ext_sales_price) AS itemrevenue,
       sum(ss_ext_sales_price)*100.0000/sum(sum(ss_ext_sales_price)) OVER (PARTITION BY i_class) AS revenueratio
FROM store_sales ,
     item,
     date_dim
WHERE ss_item_sk = i_item_sk
  AND i_category IN ('Sports',
                     'Books',
                     'Home')
  AND ss_sold_date_sk = d_date_sk
  AND d_date BETWEEN cast('1999-02-22' AS date) AND cast('1999-03-24' AS date)
GROUP BY i_item_id ,
         i_item_desc,
         i_category ,
         i_class ,
         i_current_price
ORDER BY i_category  NULLS FIRST,
         i_class  NULLS FIRST,
         i_item_id  NULLS FIRST,
         i_item_desc  NULLS FIRST,
         revenueratio NULLS FIRST;


Question:
SELECT w_substr ,
       sm_type ,
       LOWER(cc_name) cc_name_lower ,
       sum(CASE
               WHEN (cs_ship_date_sk - cs_sold_date_sk <= 30) THEN 1
               ELSE 0
           END) AS "30 days",
       sum(CASE
               WHEN (cs_ship_date_sk - cs_sold_date_sk > 30)
                    AND (cs_ship_date_sk - cs_sold_date_sk <= 60) THEN 1
               ELSE 0
           END) AS "31-60 days",
       sum(CASE
               WHEN (cs_ship_date_sk - cs_sold_date_sk > 60)
                    AND (cs_ship_date_sk - cs_sold_date_sk <= 90) THEN 1
               ELSE 0
           END) AS "61-90 days",
       sum(CASE
               WHEN (cs_ship_date_sk - cs_sold_date_sk > 90)
                    AND (cs_ship_date_sk - cs_sold_date_sk <= 120) THEN 1
               ELSE 0
           END) AS "91-120 days",
       sum(CASE
               WHEN (cs_ship_date_sk - cs_sold_date_sk > 120) THEN 1
               ELSE 0
           END) AS ">120 days"
FROM catalog_sales ,
  (SELECT SUBSTRING(w_warehouse_name,1,20) w_substr, *
   FROM warehouse) AS sq1 ,
     ship_mode ,
     call_center ,
     date_dim
WHERE d_month_seq BETWEEN 1200 AND 1200 + 11
  AND cs_ship_date_sk = d_date_sk
  AND cs_warehouse_sk = w_warehouse_sk
  AND cs_ship_mode_sk = sm_ship_mode_sk
  AND cs_call_center_sk = cc_call_center_sk
GROUP BY w_substr ,
         sm_type ,
         cc_name
ORDER BY w_substr  NULLS FIRST,
         sm_type  NULLS FIRST,
        cc_name_lower NULLS FIRST
LIMIT 100;