【TPC-DS】99条query的SQL(1-15)

-- start query 1 in stream 0 using template query1.tpl
with customer_total_return as
(select sr_customer_sk as ctr_customer_sk
,sr_store_sk as ctr_store_sk
,sum(SR_FEE) as ctr_total_return
from store_returns
,date_dim
where sr_returned_date_sk = d_date_sk
and d_year =2000
group by sr_customer_sk
,sr_store_sk)
select top 100 c_customer_id
from customer_total_return ctr1
,store
,customer
where ctr1.ctr_total_return > (select avg(ctr_total_return)*1.2
from customer_total_return ctr2
where ctr1.ctr_store_sk = ctr2.ctr_store_sk)
and s_store_sk = ctr1.ctr_store_sk
and s_state = 'TN'
and ctr1.ctr_customer_sk = c_customer_sk
order by c_customer_id
;


-- start query 2 in stream 0 using template query2.tpl
with wscs as
(select sold_date_sk
       ,sales_price 
 from (select ws_sold_date_sk sold_date_sk
             ,ws_ext_sales_price sales_price
       from web_sales 
       union all
       select cs_sold_date_sk sold_date_sk
             ,cs_ext_sales_price sales_price
       from catalog_sales)),
wswscs as 
(select d_week_seq,
       sum(case when (d_day_name='Sunday') then sales_price else null end) sun_sales,
       sum(case when (d_day_name='Monday') then sales_price else null end) mon_sales,
       sum(case when (d_day_name='Tuesday') then sales_price else  null end) tue_sales,
       sum(case when (d_day_name='Wednesday') then sales_price else null end) wed_sales,
       sum(case when (d_day_name='Thursday') then sales_price else null end) thu_sales,
       sum(case when (d_day_name='Friday') then sales_price else null end) fri_sales,
       sum(case when (d_day_name='Saturday') then sales_price else null end) sat_sales
from wscs
    ,date_dim
where d_date_sk = sold_date_sk
group by d_week_seq)
select d_week_seq1
      ,round(sun_sales1/sun_sales2,2)
      ,round(mon_sales1/mon_sales2,2)
      ,round(tue_sales1/tue_sales2,2)
      ,round(wed_sales1/wed_sales2,2)
      ,round(thu_sales1/thu_sales2,2)
      ,round(fri_sales1/fri_sales2,2)
      ,round(sat_sales1/sat_sales2,2)
from
(select wswscs.d_week_seq d_week_seq1
       ,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 wswscs,date_dim 
 where date_dim.d_week_seq = wswscs.d_week_seq and
       d_year = 1998) y,
(select wswscs.d_week_seq d_week_seq2
       ,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 wswscs
     ,date_dim 
 where date_dim.d_week_seq = wswscs.d_week_seq and
       d_year = 1998+1) z
where d_week_seq1=d_week_seq2-53
order by d_week_seq1;


-- start query 3 in stream 0 using template query3.tpl
select top 100 dt.d_year 
      ,item.i_brand_id brand_id 
      ,item.i_brand brand
      ,sum(ss_sales_price) sum_agg
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_manufact_id = 816
  and dt.d_moy=11
group by dt.d_year
     ,item.i_brand
     ,item.i_brand_id
order by dt.d_year
        ,sum_agg desc
        ,brand_id
;


-- start query 4 in stream 0 using template query4.tpl
with year_total as (
select c_customer_id customer_id
      ,c_first_name customer_first_name
      ,c_last_name customer_last_name
      ,c_preferred_cust_flag customer_preferred_cust_flag
      ,c_birth_country customer_birth_country
      ,c_login customer_login
      ,c_email_address customer_email_address
      ,d_year dyear
      ,sum(((ss_ext_list_price-ss_ext_wholesale_cost-ss_ext_discount_amt)+ss_ext_sales_price)/2) 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
group by c_customer_id
        ,c_first_name
        ,c_last_name
        ,c_preferred_cust_flag
        ,c_birth_country
        ,c_login
        ,c_email_address
        ,d_year
union all
select c_customer_id customer_id
      ,c_first_name customer_first_name
      ,c_last_name customer_last_name
      ,c_preferred_cust_flag customer_preferred_cust_flag
      ,c_birth_country customer_birth_country
      ,c_login customer_login
      ,c_email_address customer_email_address
      ,d_year dyear
      ,sum((((cs_ext_list_price-cs_ext_wholesale_cost-cs_ext_discount_amt)+cs_ext_sales_price)/2) ) year_total
      ,'c' sale_type
from customer
    ,catalog_sales
    ,date_dim
where c_customer_sk = cs_bill_customer_sk
  and cs_sold_date_sk = d_date_sk
group by c_customer_id
        ,c_first_name
        ,c_last_name
        ,c_preferred_cust_flag
        ,c_birth_country
        ,c_login
        ,c_email_address
        ,d_year
union all
select c_customer_id customer_id
      ,c_first_name customer_first_name
      ,c_last_name customer_last_name
      ,c_preferred_cust_flag customer_preferred_cust_flag
      ,c_birth_country customer_birth_country
      ,c_login customer_login
      ,c_email_address customer_email_address
      ,d_year dyear
      ,sum((((ws_ext_list_price-ws_ext_wholesale_cost-ws_ext_discount_amt)+ws_ext_sales_price)/2) ) 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
group by c_customer_id
        ,c_first_name
        ,c_last_name
        ,c_preferred_cust_flag
        ,c_birth_country
        ,c_login
        ,c_email_address
        ,d_year
        )
 select top 100 
                 t_s_secyear.customer_id
                ,t_s_secyear.customer_first_name
                ,t_s_secyear.customer_last_name
                ,t_s_secyear.customer_birth_country
from year_total t_s_firstyear
    ,year_total t_s_secyear
    ,year_total t_c_firstyear
    ,year_total t_c_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_c_secyear.customer_id
  and t_s_firstyear.customer_id = t_c_firstyear.customer_id
  and t_s_firstyear.customer_id = t_w_firstyear.customer_id
  and t_s_firstyear.customer_id = t_w_secyear.customer_id
  and t_s_firstyear.sale_type = 's'
  and t_c_firstyear.sale_type = 'c'
  and t_w_firstyear.sale_type = 'w'
  and t_s_secyear.sale_type = 's'
  and t_c_secyear.sale_type = 'c'
  and t_w_secyear.sale_type = 'w'
  and t_s_firstyear.dyear =  1999
  and t_s_secyear.dyear = 1999+1
  and t_c_firstyear.dyear =  1999
  and t_c_secyear.dyear =  1999+1
  and t_w_firstyear.dyear = 1999
  and t_w_secyear.dyear = 1999+1
  and t_s_firstyear.year_total > 0
  and t_c_firstyear.year_total > 0
  and t_w_firstyear.year_total > 0
  and case when t_c_firstyear.year_total > 0 then t_c_secyear.year_total / t_c_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
  and case when t_c_firstyear.year_total > 0 then t_c_secyear.year_total / t_c_firstyear.year_total else null end
          > case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else null end
order by t_s_secyear.customer_id
        ,t_s_secyear.customer_first_name
        ,t_s_secyear.customer_last_name
        ,t_s_secyear.customer_birth_country
;


-- start query 5 in stream 0 using template query5.tpl
with ssr as
(select s_store_id,
       sum(sales_price) as sales,
       sum(profit) as profit,
       sum(return_amt) as returns,
       sum(net_loss) as profit_loss
from
 ( select  ss_store_sk as store_sk,
           ss_sold_date_sk  as date_sk,
           ss_ext_sales_price as sales_price,
           ss_net_profit as profit,
           cast(0 as decimal(7,2)) as return_amt,
           cast(0 as decimal(7,2)) as net_loss
   from store_sales
   union all
   select sr_store_sk as store_sk,
          sr_returned_date_sk as date_sk,
          cast(0 as decimal(7,2)) as sales_price,
          cast(0 as decimal(7,2)) as profit,
          sr_return_amt as return_amt,
          sr_net_loss as net_loss
   from store_returns
  ) salesreturns,
    date_dim,
    store
where date_sk = d_date_sk
      and d_date between cast('2000-08-19' as date) 
                 and (date_add(cast('2000-08-19' as date),interval 14 day))
      and store_sk = s_store_sk
group by s_store_id)
,
csr as
(select cp_catalog_page_id,
       sum(sales_price) as sales,
       sum(profit) as profit,
       sum(return_amt) as returns,
       sum(net_loss) as profit_loss
from
 ( select  cs_catalog_page_sk as page_sk,
           cs_sold_date_sk  as date_sk,
           cs_ext_sales_price as sales_price,
           cs_net_profit as profit,
           cast(0 as decimal(7,2)) as return_amt,
           cast(0 as decimal(7,2)) as net_loss
   from catalog_sales
   union all
   select cr_catalog_page_sk as page_sk,
          cr_returned_date_sk as date_sk,
          cast(0 as decimal(7,2)) as sales_price,
          cast(0 as decimal(7,2)) as profit,
          cr_return_amount as return_amt,
          cr_net_loss as net_loss
   from catalog_returns
  ) salesreturns,
    date_dim,
    catalog_page
where date_sk = d_date_sk
      and d_date between cast('2000-08-19' as date)
                 and (date_add(cast('2000-08-19' as date),interval 14 day))
      and page_sk = cp_catalog_page_sk
group by cp_catalog_page_id)
,
wsr as
(select web_site_id,
       sum(sales_price) as sales,
       sum(profit) as profit,
       sum(return_amt) as returns,
       sum(net_loss) as profit_loss
from
 ( select  ws_web_site_sk as wsr_web_site_sk,
           ws_sold_date_sk  as date_sk,
           ws_ext_sales_price as sales_price,
           ws_net_profit as profit,
           cast(0 as decimal(7,2)) as return_amt,
           cast(0 as decimal(7,2)) as net_loss
   from web_sales
   union all
   select ws_web_site_sk as wsr_web_site_sk,
          wr_returned_date_sk as date_sk,
          cast(0 as decimal(7,2)) as sales_price,
          cast(0 as decimal(7,2)) as profit,
          wr_return_amt as return_amt,
          wr_net_loss as net_loss
   from web_returns left outer join web_sales on
        ( wr_item_sk = ws_item_sk
          and wr_order_number = ws_order_number)
  ) salesreturns,
    date_dim,
    web_site
where date_sk = d_date_sk
      and d_date between cast('2000-08-19' as date)
                 and (date_add(cast('2000-08-19' as date),interval 14 day))
      and wsr_web_site_sk = web_site_sk
group by web_site_id)
 select top 100 channel
       , id
       , sum(sales) as sales
       , sum(returns) as returns
       , sum(profit) as profit
from 
(select 'store channel' as channel
       , 'store' || s_store_id as id
       , sales
       , returns
       , (profit - profit_loss) as profit
from   ssr
union all
select 'catalog channel' as channel
       , 'catalog_page' || cp_catalog_page_id as id
       , sales
       , returns
       , (profit - profit_loss) as profit
from  csr
union all
select 'web channel' as channel
       , 'web_site' || web_site_id as id
       , sales
       , returns
       , (profit - profit_loss) as profit
from   wsr
) x
group by rollup (channel, id)
order by channel
        ,id
;
-- start query 6 in stream 0 using template query6.tpl
select top 100 a.ca_state state, count(*) cnt
from customer_address a
    ,customer c
    ,store_sales s
    ,date_dim d
    ,item i
where       a.ca_address_sk = c.c_current_addr_sk
   and c.c_customer_sk = s.ss_customer_sk
   and s.ss_sold_date_sk = d.d_date_sk
   and s.ss_item_sk = i.i_item_sk
   and d.d_month_seq = 
        (select distinct (d_month_seq)
         from date_dim
              where d_year = 2002
           and d_moy = 3 )
   and i.i_current_price > 1.2 * 
            (select avg(j.i_current_price) 
        from item j 
        where j.i_category = i.i_category)
group by a.ca_state
having count(*) >= 10
order by cnt, a.ca_state 
;


-- start query 7 in stream 0 using template query7.tpl
select top 100 i_item_id, 
       avg(ss_quantity) agg1,
       avg(ss_list_price) agg2,
       avg(ss_coupon_amt) agg3,
       avg(ss_sales_price) agg4 
from store_sales, customer_demographics, date_dim, item, promotion
where ss_sold_date_sk = d_date_sk and
      ss_item_sk = i_item_sk and
      ss_cdemo_sk = cd_demo_sk and
      ss_promo_sk = p_promo_sk and
      cd_gender = 'F' and 
      cd_marital_status = 'W' and
      cd_education_status = 'College' and
      (p_channel_email = 'N' or p_channel_event = 'N') and
      d_year = 2001 
group by i_item_id
order by i_item_id
;


-- start query 8 in stream 0 using template query8.tpl
select top 100 s_store_name
     ,sum(ss_net_profit)
from store_sales
    ,date_dim
    ,store,
    (select ca_zip
    from (
     SELECT substr(ca_zip,1,5) ca_zip
     FROM customer_address
     WHERE substr(ca_zip,1,5) IN (
                         '47602','16704','35863','28577','83910','36201',
                         '58412','48162','28055','41419','80332',
                         '38607','77817','24891','16226','18410',
                         '21231','59345','13918','51089','20317',
                         '17167','54585','67881','78366','47770',
                         '18360','51717','73108','14440','21800',
                         '89338','45859','65501','34948','25973',
                         '73219','25333','17291','10374','18829',
                         '60736','82620','41351','52094','19326',
                         '25214','54207','40936','21814','79077',
                         '25178','75742','77454','30621','89193',
                         '27369','41232','48567','83041','71948',
                         '37119','68341','14073','16891','62878',
                         '49130','19833','24286','27700','40979',
                         '50412','81504','94835','84844','71954',
                         '39503','57649','18434','24987','12350',
                         '86379','27413','44529','98569','16515',
                         '27287','24255','21094','16005','56436',
                         '91110','68293','56455','54558','10298',
                         '83647','32754','27052','51766','19444',
                         '13869','45645','94791','57631','20712',
                         '37788','41807','46507','21727','71836',
                         '81070','50632','88086','63991','20244',
                         '31655','51782','29818','63792','68605',
                         '94898','36430','57025','20601','82080',
                         '33869','22728','35834','29086','92645',
                         '98584','98072','11652','78093','57553',
                         '43830','71144','53565','18700','90209',
                         '71256','38353','54364','28571','96560',
                         '57839','56355','50679','45266','84680',
                         '34306','34972','48530','30106','15371',
                         '92380','84247','92292','68852','13338',
                         '34594','82602','70073','98069','85066',
                         '47289','11686','98862','26217','47529',
                         '63294','51793','35926','24227','14196',
                         '24594','32489','99060','49472','43432',
                         '49211','14312','88137','47369','56877',
                         '20534','81755','15794','12318','21060',
                         '73134','41255','63073','81003','73873',
                         '66057','51184','51195','45676','92696',
                         '70450','90669','98338','25264','38919',
                         '59226','58581','60298','17895','19489',
                         '52301','80846','95464','68770','51634',
                         '19988','18367','18421','11618','67975',
                         '25494','41352','95430','15734','62585',
                         '97173','33773','10425','75675','53535',
                         '17879','41967','12197','67998','79658',
                         '59130','72592','14851','43933','68101',
                         '50636','25717','71286','24660','58058',
                         '72991','95042','15543','33122','69280',
                         '11912','59386','27642','65177','17672',
                         '33467','64592','36335','54010','18767',
                         '63193','42361','49254','33113','33159',
                         '36479','59080','11855','81963','31016',
                         '49140','29392','41836','32958','53163',
                         '13844','73146','23952','65148','93498',
                         '14530','46131','58454','13376','13378',
                         '83986','12320','17193','59852','46081',
                         '98533','52389','13086','68843','31013',
                         '13261','60560','13443','45533','83583',
                         '11489','58218','19753','22911','25115',
                         '86709','27156','32669','13123','51933',
                         '39214','41331','66943','14155','69998',
                         '49101','70070','35076','14242','73021',
                         '59494','15782','29752','37914','74686',
                         '83086','34473','15751','81084','49230',
                         '91894','60624','17819','28810','63180',
                         '56224','39459','55233','75752','43639',
                         '55349','86057','62361','50788','31830',
                         '58062','18218','85761','60083','45484',
                         '21204','90229','70041','41162','35390',
                         '16364','39500','68908','26689','52868',
                         '81335','40146','11340','61527','61794',
                         '71997','30415','59004','29450','58117',
                         '69952','33562','83833','27385','61860',
                         '96435','48333','23065','32961','84919',
                         '61997','99132','22815','56600','68730',
                         '48017','95694','32919','88217','27116',
                         '28239','58032','18884','16791','21343',
                         '97462','18569','75660','15475')
    intersect
     select ca_zip
     from (SELECT substr(ca_zip,1,5) ca_zip,count(*) cnt
           FROM customer_address, customer
           WHERE ca_address_sk = c_current_addr_sk and
                 c_preferred_cust_flag='Y'
           group by ca_zip
           having count(*) > 10)A1)A2) V1
where ss_store_sk = s_store_sk
 and ss_sold_date_sk = d_date_sk
 and d_qoy = 2 and d_year = 1998
 and (substr(s_zip,1,2) = substr(V1.ca_zip,1,2))
group by s_store_name
order by s_store_name
;


-- start query 9 in stream 0 using template query9.tpl
select case when (select count(*) 
                 from store_sales 
                 where ss_quantity between 1 and 20) > 1071
           then (select avg(ss_ext_tax) 
                 from store_sales 
                 where ss_quantity between 1 and 20) 
           else (select avg(ss_net_paid_inc_tax)
                 from store_sales
                 where ss_quantity between 1 and 20) end bucket1 ,
      case when (select count(*)
                 from store_sales
                 where ss_quantity between 21 and 40) > 39161
           then (select avg(ss_ext_tax)
                 from store_sales
                 where ss_quantity between 21 and 40) 
           else (select avg(ss_net_paid_inc_tax)
                 from store_sales
                 where ss_quantity between 21 and 40) end bucket2,
      case when (select count(*)
                 from store_sales
                 where ss_quantity between 41 and 60) > 29434
           then (select avg(ss_ext_tax)
                 from store_sales
                 where ss_quantity between 41 and 60)
           else (select avg(ss_net_paid_inc_tax)
                 from store_sales
                 where ss_quantity between 41 and 60) end bucket3,
      case when (select count(*)
                 from store_sales
                 where ss_quantity between 61 and 80) > 6568
           then (select avg(ss_ext_tax)
                 from store_sales
                 where ss_quantity between 61 and 80)
           else (select avg(ss_net_paid_inc_tax)
                 from store_sales
                 where ss_quantity between 61 and 80) end bucket4,
      case when (select count(*)
                 from store_sales
                 where ss_quantity between 81 and 100) > 21216
           then (select avg(ss_ext_tax)
                 from store_sales
                 where ss_quantity between 81 and 100)
           else (select avg(ss_net_paid_inc_tax)
                 from store_sales
                 where ss_quantity between 81 and 100) end bucket5
from reason
where r_reason_sk = 1
;


-- start query 10 in stream 0 using template query10.tpl
select top 100 
 cd_gender,
 cd_marital_status,
 cd_education_status,
 count(*) cnt1,
 cd_purchase_estimate,
 count(*) cnt2,
 cd_credit_rating,
 count(*) cnt3,
 cd_dep_count,
 count(*) cnt4,
 cd_dep_employed_count,
 count(*) cnt5,
 cd_dep_college_count,
 count(*) cnt6
from
 customer c,customer_address ca,customer_demographics
where
 c.c_current_addr_sk = ca.ca_address_sk and
 ca_county in ('Fairfield County','Campbell County','Washtenaw County','Escambia County','Cleburne County') 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 3 and 3+3) 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 = 2001 and
                 d_moy between 3 ANd 3+3) 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 = 2001 and
                 d_moy between 3 and 3+3))
group by cd_gender,
         cd_marital_status,
         cd_education_status,
         cd_purchase_estimate,
         cd_credit_rating,
         cd_dep_count,
         cd_dep_employed_count,
         cd_dep_college_count
order by cd_gender,
         cd_marital_status,
         cd_education_status,
         cd_purchase_estimate,
         cd_credit_rating,
         cd_dep_count,
         cd_dep_employed_count,
         cd_dep_college_count
;
-- start query 11 in stream 0 using template query11.tpl
with year_total as (
select c_customer_id customer_id
      ,c_first_name customer_first_name
      ,c_last_name customer_last_name
      ,c_preferred_cust_flag customer_preferred_cust_flag
      ,c_birth_country customer_birth_country
      ,c_login customer_login
      ,c_email_address customer_email_address
      ,d_year dyear
      ,sum(ss_ext_list_price-ss_ext_discount_amt) 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
group by c_customer_id
        ,c_first_name
        ,c_last_name
        ,c_preferred_cust_flag 
        ,c_birth_country
        ,c_login
        ,c_email_address
        ,d_year 
union all
select c_customer_id customer_id
      ,c_first_name customer_first_name
      ,c_last_name customer_last_name
      ,c_preferred_cust_flag customer_preferred_cust_flag
      ,c_birth_country customer_birth_country
      ,c_login customer_login
      ,c_email_address customer_email_address
      ,d_year dyear
      ,sum(ws_ext_list_price-ws_ext_discount_amt) 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
group by c_customer_id
        ,c_first_name
        ,c_last_name
        ,c_preferred_cust_flag 
        ,c_birth_country
        ,c_login
        ,c_email_address
        ,d_year
        )
 select top 100 
                 t_s_secyear.customer_id
                ,t_s_secyear.customer_first_name
                ,t_s_secyear.customer_last_name
                ,t_s_secyear.customer_email_address
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.dyear = 1998
        and t_s_secyear.dyear = 1998+1
        and t_w_firstyear.dyear = 1998
        and t_w_secyear.dyear = 1998+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 0.0 end
            > case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else 0.0 end
order by t_s_secyear.customer_id
        ,t_s_secyear.customer_first_name
        ,t_s_secyear.customer_last_name
        ,t_s_secyear.customer_email_address
;


-- start query 12 in stream 0 using template query12.tpl
select top 100 i_item_id
     ,i_item_desc 
     ,i_category 
     ,i_class 
     ,i_current_price
     ,sum(ws_ext_sales_price) as itemrevenue 
     ,sum(ws_ext_sales_price)*100/sum(sum(ws_ext_sales_price)) over
         (partition by i_class) as revenueratio
from    
   web_sales
       ,item 
       ,date_dim
where 
   ws_item_sk = i_item_sk 
   and i_category in ('Men', 'Books', 'Electronics')
   and ws_sold_date_sk = d_date_sk
   and d_date between cast('2001-06-15' as date) 
               and (date_add(cast('2001-06-15' as date),interval 30 day))
group by 
   i_item_id
       ,i_item_desc 
       ,i_category
       ,i_class
       ,i_current_price
order by 
   i_category
       ,i_class
       ,i_item_id
       ,i_item_desc
       ,revenueratio
;

-- start query 13 in stream 0 using template query13.tpl 
select avg(ss_quantity)
      ,avg(ss_ext_sales_price)
      ,avg(ss_ext_wholesale_cost)
      ,sum(ss_ext_wholesale_cost)
from store_sales
    ,store
    ,customer_demographics
    ,household_demographics
    ,customer_address
    ,date_dim
where s_store_sk = ss_store_sk
and  ss_sold_date_sk = d_date_sk and d_year = 2001
and((ss_hdemo_sk=hd_demo_sk
 and cd_demo_sk = ss_cdemo_sk
 and cd_marital_status = 'M'
 and cd_education_status = 'College'
 and ss_sales_price between 100.00 and 150.00
 and hd_dep_count = 3   
    )or
    (ss_hdemo_sk=hd_demo_sk
 and cd_demo_sk = ss_cdemo_sk
 and cd_marital_status = 'D'
 and cd_education_status = 'Primary'
 and ss_sales_price between 50.00 and 100.00   
 and hd_dep_count = 1
    ) or 
    (ss_hdemo_sk=hd_demo_sk
 and cd_demo_sk = ss_cdemo_sk
 and cd_marital_status = 'W'
 and cd_education_status = '2 yr Degree'
 and ss_sales_price between 150.00 and 200.00 
 and hd_dep_count = 1  
    ))
and((ss_addr_sk = ca_address_sk
 and ca_country = 'United States'
 and ca_state in ('IL', 'TN', 'TX')
 and ss_net_profit between 100 and 200  
    ) or
    (ss_addr_sk = ca_address_sk
 and ca_country = 'United States'
 and ca_state in ('WY', 'OH', 'ID')
 and ss_net_profit between 150 and 300  
    ) or
    (ss_addr_sk = ca_address_sk
 and ca_country = 'United States'
 and ca_state in ('MS', 'SC', 'IA')
 and ss_net_profit between 50 and 250  
    ))
;

-- start query 14 in stream 0 using template query14.tpl
with  cross_items as
(select i_item_sk ss_item_sk
from item,
(select iss.i_brand_id brand_id
    ,iss.i_class_id class_id
    ,iss.i_category_id category_id
from store_sales
    ,item iss
    ,date_dim d1
where ss_item_sk = iss.i_item_sk
  and ss_sold_date_sk = d1.d_date_sk
  and d1.d_year between 1999 AND 1999 + 2
intersect 
select ics.i_brand_id
    ,ics.i_class_id
    ,ics.i_category_id
from catalog_sales
    ,item ics
    ,date_dim d2
where cs_item_sk = ics.i_item_sk
  and cs_sold_date_sk = d2.d_date_sk
  and d2.d_year between 1999 AND 1999 + 2
intersect
select iws.i_brand_id
    ,iws.i_class_id
    ,iws.i_category_id
from web_sales
    ,item iws
    ,date_dim d3
where ws_item_sk = iws.i_item_sk
  and ws_sold_date_sk = d3.d_date_sk
  and d3.d_year between 1999 AND 1999 + 2)
where i_brand_id = brand_id
     and i_class_id = class_id
     and i_category_id = category_id
),
avg_sales as
(select avg(quantity*list_price) average_sales
 from (select ss_quantity quantity
            ,ss_list_price list_price
      from store_sales
          ,date_dim
      where ss_sold_date_sk = d_date_sk
        and d_year between 1999 and 1999 + 2
      union all 
      select cs_quantity quantity 
            ,cs_list_price list_price
      from catalog_sales
          ,date_dim
      where cs_sold_date_sk = d_date_sk
        and d_year between 1999 and 1999 + 2 
      union all
      select ws_quantity quantity
            ,ws_list_price list_price
      from web_sales
          ,date_dim
      where ws_sold_date_sk = d_date_sk
        and d_year between 1999 and 1999 + 2) x)
 select top 100 channel, i_brand_id,i_class_id,i_category_id,sum(sales), sum(number_sales)
from(
      select 'store' channel, i_brand_id,i_class_id
            ,i_category_id,sum(ss_quantity*ss_list_price) sales
            , count(*) number_sales
      from store_sales
          ,item
          ,date_dim
      where ss_item_sk in (select ss_item_sk from cross_items)
        and ss_item_sk = i_item_sk
        and ss_sold_date_sk = d_date_sk
        and d_year = 1999+2 
        and d_moy = 11
      group by i_brand_id,i_class_id,i_category_id
      having sum(ss_quantity*ss_list_price) > (select average_sales from avg_sales)
      union all
      select 'catalog' channel, i_brand_id,i_class_id,i_category_id, sum(cs_quantity*cs_list_price) sales, count(*) number_sales
      from catalog_sales
          ,item
          ,date_dim
      where cs_item_sk in (select ss_item_sk from cross_items)
        and cs_item_sk = i_item_sk
        and cs_sold_date_sk = d_date_sk
        and d_year = 1999+2 
        and d_moy = 11
      group by i_brand_id,i_class_id,i_category_id
      having sum(cs_quantity*cs_list_price) > (select average_sales from avg_sales)
      union all
      select 'web' channel, i_brand_id,i_class_id,i_category_id, sum(ws_quantity*ws_list_price) sales , count(*) number_sales
      from web_sales
          ,item
          ,date_dim
      where ws_item_sk in (select ss_item_sk from cross_items)
        and ws_item_sk = i_item_sk
        and ws_sold_date_sk = d_date_sk
        and d_year = 1999+2
        and d_moy = 11
      group by i_brand_id,i_class_id,i_category_id
      having sum(ws_quantity*ws_list_price) > (select average_sales from avg_sales)
) y
group by rollup (channel, i_brand_id,i_class_id,i_category_id)
order by channel,i_brand_id,i_class_id,i_category_id
;

-- start query 15 in stream 0 using template query15.tpl
select top 100 ca_zip
      ,sum(cs_sales_price)
from catalog_sales
    ,customer
    ,customer_address
    ,date_dim
where cs_bill_customer_sk = c_customer_sk
   and c_current_addr_sk = ca_address_sk 
   and ( substr(ca_zip,1,5) in ('85669', '86197','88274','83405','86475',
                                  '85392', '85460', '80348', '81792')
         or ca_state in ('CA','WA','GA')
         or cs_sales_price > 500)
   and cs_sold_date_sk = d_date_sk
   and d_qoy = 2 and d_year = 2001
group by ca_zip
order by ca_zip
;
   ```

你可能感兴趣的:(【TPC-DS】99条query的SQL(1-15))