aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/tests/sql/suites/pg-tpcds/q02.sql
blob: 0b1e03a631ca83fa039e000600409a3c9596b961 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
--!syntax_pg
--TPC-DS Q2

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

-- end query 1 in stream 0 using template ../query_templates/query2.tpl