github.com/matrixorigin/matrixone@v1.2.0/test/distributed/cases/benchmark/tpch/04_CLEANUP/03_Snapshot_tpch.sql (about)

     1  use tpch;
     2  -- tpch q1 --
     3  select
     4  	l_returnflag,
     5  	l_linestatus,
     6  	sum(l_quantity) as sum_qty,
     7  	sum(l_extendedprice) as sum_base_price,
     8  	sum(l_extendedprice * (1 - l_discount)) as sum_disc_price,
     9  	sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) as sum_charge,
    10  	avg(l_quantity) as avg_qty,
    11  	avg(l_extendedprice) as avg_price,
    12  	avg(l_discount) as avg_disc,
    13  	count(*) as count_order
    14  from
    15  	lineitem {snapshot = 'tpch_snapshot'}
    16  where
    17  	l_shipdate <= date '1998-12-01' - interval '112' day
    18  group by
    19  	l_returnflag,
    20  	l_linestatus
    21  order by
    22  	l_returnflag,
    23  	l_linestatus
    24  ;
    25  
    26  select
    27  	l_returnflag,
    28  	l_linestatus,
    29  	sum(l_quantity) as sum_qty,
    30  	sum(l_extendedprice) as sum_base_price,
    31  	sum(l_extendedprice * (1 - l_discount)) as sum_disc_price,
    32  	sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) as sum_charge,
    33  	avg(l_quantity) as avg_qty,
    34  	avg(l_extendedprice) as avg_price,
    35  	avg(l_discount) as avg_disc,
    36  	count(*) as count_order
    37  from
    38  	lineitem {snapshot = 'tpch_snapshot'}
    39  where
    40  	l_shipdate <= date '1998-12-01' - interval '112' day
    41  group by
    42  	l_returnflag,
    43  	l_linestatus
    44  order by
    45  	l_returnflag,
    46  	l_linestatus
    47  ;
    48  
    49  select
    50  	l_returnflag,
    51  	l_linestatus,
    52  	sum(l_quantity) as sum_qty,
    53  	sum(l_extendedprice) as sum_base_price,
    54  	sum(l_extendedprice * (1 - l_discount)) as sum_disc_price,
    55  	sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) as sum_charge,
    56  	avg(l_quantity) as avg_qty,
    57  	avg(l_extendedprice) as avg_price,
    58  	avg(l_discount) as avg_disc,
    59  	count(*) as count_order
    60  from
    61  	lineitem {snapshot = 'tpch_snapshot'}
    62  where
    63  	l_shipdate <= date '1998-12-01' - interval '112' day
    64  group by
    65  	l_returnflag,
    66  	l_linestatus
    67  order by
    68  	l_returnflag,
    69  	l_linestatus
    70  ;
    71  
    72  -- tpch q2 --
    73  select
    74  	s_acctbal,
    75  	s_name,
    76  	n_name,
    77  	p_partkey,
    78  	p_mfgr,
    79  	s_address,
    80  	s_phone,
    81  	s_comment
    82  from
    83  	part {snapshot = 'tpch_snapshot'},
    84  	supplier {snapshot = 'tpch_snapshot'},
    85  	partsupp {snapshot = 'tpch_snapshot'},
    86  	nation {snapshot = 'tpch_snapshot'},
    87  	region {snapshot = 'tpch_snapshot'}
    88  where
    89  	p_partkey = ps_partkey
    90  	and s_suppkey = ps_suppkey
    91  	and p_size = 48
    92  	and p_type like '%TIN'
    93  	and s_nationkey = n_nationkey
    94  	and n_regionkey = r_regionkey
    95  	and r_name = 'MIDDLE EAST'
    96  	and ps_supplycost = (
    97  		select
    98  			min(ps_supplycost)
    99  		from
   100  			partsupp {snapshot = 'tpch_snapshot'},
   101  			supplier {snapshot = 'tpch_snapshot'},
   102  			nation {snapshot = 'tpch_snapshot'},
   103  			region {snapshot = 'tpch_snapshot'}
   104  		where
   105  			p_partkey = ps_partkey
   106  			and s_suppkey = ps_suppkey
   107  			and s_nationkey = n_nationkey
   108  			and n_regionkey = r_regionkey
   109  			and r_name = 'MIDDLE EAST'
   110  	)
   111  order by
   112  	s_acctbal desc,
   113  	n_name,
   114  	s_name,
   115  	p_partkey
   116  limit 100
   117  ;
   118  
   119  select
   120  	s_acctbal,
   121  	s_name,
   122  	n_name,
   123  	p_partkey,
   124  	p_mfgr,
   125  	s_address,
   126  	s_phone,
   127  	s_comment
   128  from
   129  	part {snapshot = 'tpch_snapshot'},
   130  	supplier {snapshot = 'tpch_snapshot'},
   131  	partsupp {snapshot = 'tpch_snapshot'},
   132  	nation {snapshot = 'tpch_snapshot'},
   133  	region {snapshot = 'tpch_snapshot'}
   134  where
   135  	p_partkey = ps_partkey
   136  	and s_suppkey = ps_suppkey
   137  	and p_size = 48
   138  	and p_type like '%TIN'
   139  	and s_nationkey = n_nationkey
   140  	and n_regionkey = r_regionkey
   141  	and r_name = 'MIDDLE EAST'
   142  	and ps_supplycost = (
   143  		select
   144  			min(ps_supplycost)
   145  		from
   146  			partsupp {snapshot = 'tpch_snapshot'},
   147  			supplier {snapshot = 'tpch_snapshot'},
   148  			nation {snapshot = 'tpch_snapshot'},
   149  			region {snapshot = 'tpch_snapshot'}
   150  		where
   151  			p_partkey = ps_partkey
   152  			and s_suppkey = ps_suppkey
   153  			and s_nationkey = n_nationkey
   154  			and n_regionkey = r_regionkey
   155  			and r_name = 'MIDDLE EAST'
   156  	)
   157  order by
   158  	s_acctbal desc,
   159  	n_name,
   160  	s_name,
   161  	p_partkey
   162  limit 100
   163  ;
   164  
   165  select
   166  	s_acctbal,
   167  	s_name,
   168  	n_name,
   169  	p_partkey,
   170  	p_mfgr,
   171  	s_address,
   172  	s_phone,
   173  	s_comment
   174  from
   175  	part {snapshot = 'tpch_snapshot'},
   176  	supplier {snapshot = 'tpch_snapshot'},
   177  	partsupp {snapshot = 'tpch_snapshot'},
   178  	nation {snapshot = 'tpch_snapshot'},
   179  	region {snapshot = 'tpch_snapshot'}
   180  where
   181  	p_partkey = ps_partkey
   182  	and s_suppkey = ps_suppkey
   183  	and p_size = 48
   184  	and p_type like '%TIN'
   185  	and s_nationkey = n_nationkey
   186  	and n_regionkey = r_regionkey
   187  	and r_name = 'MIDDLE EAST'
   188  	and ps_supplycost = (
   189  		select
   190  			min(ps_supplycost)
   191  		from
   192  			partsupp {snapshot = 'tpch_snapshot'},
   193  			supplier {snapshot = 'tpch_snapshot'},
   194  			nation {snapshot = 'tpch_snapshot'},
   195  			region {snapshot = 'tpch_snapshot'}
   196  		where
   197  			p_partkey = ps_partkey
   198  			and s_suppkey = ps_suppkey
   199  			and s_nationkey = n_nationkey
   200  			and n_regionkey = r_regionkey
   201  			and r_name = 'MIDDLE EAST'
   202  	)
   203  order by
   204  	s_acctbal desc,
   205  	n_name,
   206  	s_name,
   207  	p_partkey
   208  limit 100
   209  ;
   210  
   211  -- tpch q3 --
   212  select
   213  	l_orderkey,
   214  	sum(l_extendedprice * (1 - l_discount)) as revenue,
   215  	o_orderdate,
   216  	o_shippriority
   217  from
   218  	customer {snapshot = 'tpch_snapshot'},
   219  	orders {snapshot = 'tpch_snapshot'},
   220  	lineitem {snapshot = 'tpch_snapshot'}
   221  where
   222  	c_mktsegment = 'HOUSEHOLD'
   223  	and c_custkey = o_custkey
   224  	and l_orderkey = o_orderkey
   225  	and o_orderdate < date '1995-03-29'
   226  	and l_shipdate > date '1995-03-29'
   227  group by
   228  	l_orderkey,
   229  	o_orderdate,
   230  	o_shippriority
   231  order by
   232  	revenue desc,
   233  	o_orderdate
   234  limit 10
   235  ;
   236  
   237  select
   238  	l_orderkey,
   239  	sum(l_extendedprice * (1 - l_discount)) as revenue,
   240  	o_orderdate,
   241  	o_shippriority
   242  from
   243  	customer {snapshot = 'tpch_snapshot'},
   244  	orders {snapshot = 'tpch_snapshot'},
   245  	lineitem {snapshot = 'tpch_snapshot'}
   246  where
   247  	c_mktsegment = 'HOUSEHOLD'
   248  	and c_custkey = o_custkey
   249  	and l_orderkey = o_orderkey
   250  	and o_orderdate < date '1995-03-29'
   251  	and l_shipdate > date '1995-03-29'
   252  group by
   253  	l_orderkey,
   254  	o_orderdate,
   255  	o_shippriority
   256  order by
   257  	revenue desc,
   258  	o_orderdate
   259  limit 10
   260  ;
   261  
   262  select
   263  	l_orderkey,
   264  	sum(l_extendedprice * (1 - l_discount)) as revenue,
   265  	o_orderdate,
   266  	o_shippriority
   267  from
   268  	customer {snapshot = 'tpch_snapshot'},
   269  	orders {snapshot = 'tpch_snapshot'},
   270  	lineitem {snapshot = 'tpch_snapshot'}
   271  where
   272  	c_mktsegment = 'HOUSEHOLD'
   273  	and c_custkey = o_custkey
   274  	and l_orderkey = o_orderkey
   275  	and o_orderdate < date '1995-03-29'
   276  	and l_shipdate > date '1995-03-29'
   277  group by
   278  	l_orderkey,
   279  	o_orderdate,
   280  	o_shippriority
   281  order by
   282  	revenue desc,
   283  	o_orderdate
   284  limit 10
   285  ;
   286  
   287  -- tpch q4 --
   288  select
   289      o_orderpriority,
   290      count(*) as order_count
   291  from
   292      orders {snapshot = 'tpch_snapshot'}
   293  where
   294      o_orderdate >= date '1997-07-01'
   295      and o_orderdate < date '1997-07-01' + interval '3' month
   296      and exists (
   297          select
   298              *
   299          from
   300              lineitem {snapshot = 'tpch_snapshot'}
   301          where
   302              l_orderkey = o_orderkey
   303              and l_commitdate < l_receiptdate
   304      )
   305  group by
   306      o_orderpriority
   307  order by
   308      o_orderpriority
   309  ;
   310  
   311  select
   312      o_orderpriority,
   313      count(*) as order_count
   314  from
   315      orders {snapshot = 'tpch_snapshot'}
   316  where
   317      o_orderdate >= date '1997-07-01'
   318      and o_orderdate < date '1997-07-01' + interval '3' month
   319      and exists (
   320          select
   321              *
   322          from
   323              lineitem {snapshot = 'tpch_snapshot'}
   324          where
   325              l_orderkey = o_orderkey
   326              and l_commitdate < l_receiptdate
   327      )
   328  group by
   329      o_orderpriority
   330  order by
   331      o_orderpriority
   332  ;
   333  
   334  select
   335      o_orderpriority,
   336      count(*) as order_count
   337  from
   338      orders {snapshot = 'tpch_snapshot'}
   339  where
   340      o_orderdate >= date '1997-07-01'
   341      and o_orderdate < date '1997-07-01' + interval '3' month
   342      and exists (
   343          select
   344              *
   345          from
   346              lineitem {snapshot = 'tpch_snapshot'}
   347          where
   348              l_orderkey = o_orderkey
   349              and l_commitdate < l_receiptdate
   350      )
   351  group by
   352      o_orderpriority
   353  order by
   354      o_orderpriority
   355  ;
   356  
   357  -- tpch q5 --
   358  select
   359  	n_name,
   360  	sum(l_extendedprice * (1 - l_discount)) as revenue
   361  from
   362  	customer {snapshot = 'tpch_snapshot'},
   363  	orders {snapshot = 'tpch_snapshot'},
   364  	lineitem {snapshot = 'tpch_snapshot'},
   365  	supplier {snapshot = 'tpch_snapshot'},
   366  	nation {snapshot = 'tpch_snapshot'},
   367  	region {snapshot = 'tpch_snapshot'}
   368  where
   369  	c_custkey = o_custkey
   370  	and l_orderkey = o_orderkey
   371  	and l_suppkey = s_suppkey
   372  	and c_nationkey = s_nationkey
   373  	and s_nationkey = n_nationkey
   374  	and n_regionkey = r_regionkey
   375  	and r_name = 'AMERICA'
   376  	and o_orderdate >= date '1994-01-01'
   377  	and o_orderdate < date '1994-01-01' + interval '1' year
   378  group by
   379  	n_name
   380  order by
   381  	revenue desc
   382  ;
   383  
   384  select
   385  	n_name,
   386  	sum(l_extendedprice * (1 - l_discount)) as revenue
   387  from
   388  	customer {snapshot = 'tpch_snapshot'},
   389  	orders {snapshot = 'tpch_snapshot'},
   390  	lineitem {snapshot = 'tpch_snapshot'},
   391  	supplier {snapshot = 'tpch_snapshot'},
   392  	nation {snapshot = 'tpch_snapshot'},
   393  	region {snapshot = 'tpch_snapshot'}
   394  where
   395  	c_custkey = o_custkey
   396  	and l_orderkey = o_orderkey
   397  	and l_suppkey = s_suppkey
   398  	and c_nationkey = s_nationkey
   399  	and s_nationkey = n_nationkey
   400  	and n_regionkey = r_regionkey
   401  	and r_name = 'AMERICA'
   402  	and o_orderdate >= date '1994-01-01'
   403  	and o_orderdate < date '1994-01-01' + interval '1' year
   404  group by
   405  	n_name
   406  order by
   407  	revenue desc
   408  ;
   409  
   410  select
   411  	n_name,
   412  	sum(l_extendedprice * (1 - l_discount)) as revenue
   413  from
   414  	customer {snapshot = 'tpch_snapshot'},
   415  	orders {snapshot = 'tpch_snapshot'},
   416  	lineitem {snapshot = 'tpch_snapshot'},
   417  	supplier {snapshot = 'tpch_snapshot'},
   418  	nation {snapshot = 'tpch_snapshot'},
   419  	region {snapshot = 'tpch_snapshot'}
   420  where
   421  	c_custkey = o_custkey
   422  	and l_orderkey = o_orderkey
   423  	and l_suppkey = s_suppkey
   424  	and c_nationkey = s_nationkey
   425  	and s_nationkey = n_nationkey
   426  	and n_regionkey = r_regionkey
   427  	and r_name = 'AMERICA'
   428  	and o_orderdate >= date '1994-01-01'
   429  	and o_orderdate < date '1994-01-01' + interval '1' year
   430  group by
   431  	n_name
   432  order by
   433  	revenue desc
   434  ;
   435  
   436  -- tpch q6 --
   437  select
   438  	sum(l_extendedprice * l_discount) as revenue
   439  from
   440  	lineitem {snapshot = 'tpch_snapshot'}
   441  where
   442  	l_shipdate >= date '1994-01-01'
   443  	and l_shipdate < date '1994-01-01' + interval '1' year
   444  	and l_discount between 0.03 - 0.01 and 0.03 + 0.01
   445  	and l_quantity < 24;
   446  
   447  select
   448  	sum(l_extendedprice * l_discount) as revenue
   449  from
   450  	lineitem {snapshot = 'tpch_snapshot'}
   451  where
   452  	l_shipdate >= date '1994-01-01'
   453  	and l_shipdate < date '1994-01-01' + interval '1' year
   454  	and l_discount between 0.03 - 0.01 and 0.03 + 0.01
   455  	and l_quantity < 24;
   456  
   457  select
   458  	sum(l_extendedprice * l_discount) as revenue
   459  from
   460  	lineitem {snapshot = 'tpch_snapshot'}
   461  where
   462  	l_shipdate >= date '1994-01-01'
   463  	and l_shipdate < date '1994-01-01' + interval '1' year
   464  	and l_discount between 0.03 - 0.01 and 0.03 + 0.01
   465  	and l_quantity < 24;
   466  
   467  -- tpch q7 --
   468  select
   469      supp_nation,
   470      cust_nation,
   471      l_year,
   472      sum(volume) as revenue
   473  from
   474      (
   475          select
   476              n1.n_name as supp_nation,
   477              n2.n_name as cust_nation,
   478              extract(year from l_shipdate) as l_year,
   479              l_extendedprice * (1 - l_discount) as volume
   480          from
   481              supplier {snapshot = 'tpch_snapshot'},
   482              lineitem {snapshot = 'tpch_snapshot'},
   483              orders {snapshot = 'tpch_snapshot'},
   484              customer {snapshot = 'tpch_snapshot'},
   485              nation {snapshot = 'tpch_snapshot'} n1 ,
   486              nation {snapshot = 'tpch_snapshot'} n2 
   487          where
   488              s_suppkey = l_suppkey
   489              and o_orderkey = l_orderkey
   490              and c_custkey = o_custkey
   491              and s_nationkey = n1.n_nationkey
   492              and c_nationkey = n2.n_nationkey
   493              and (
   494                  (n1.n_name = 'FRANCE' and n2.n_name = 'ARGENTINA')
   495                  or (n1.n_name = 'ARGENTINA' and n2.n_name = 'FRANCE')
   496              )
   497              and l_shipdate between date '1995-01-01' and date '1996-12-31'
   498      ) as shipping
   499  group by
   500      supp_nation,
   501      cust_nation,
   502      l_year
   503  order by
   504      supp_nation,
   505      cust_nation,
   506      l_year
   507  ;
   508  
   509  select
   510      supp_nation,
   511      cust_nation,
   512      l_year,
   513      sum(volume) as revenue
   514  from
   515      (
   516          select
   517              n1.n_name as supp_nation,
   518              n2.n_name as cust_nation,
   519              extract(year from l_shipdate) as l_year,
   520              l_extendedprice * (1 - l_discount) as volume
   521          from
   522              supplier {snapshot = 'tpch_snapshot'},
   523              lineitem {snapshot = 'tpch_snapshot'},
   524              orders {snapshot = 'tpch_snapshot'},
   525              customer {snapshot = 'tpch_snapshot'},
   526              nation {snapshot = 'tpch_snapshot'} n1 ,
   527              nation {snapshot = 'tpch_snapshot'} n2 
   528          where
   529              s_suppkey = l_suppkey
   530              and o_orderkey = l_orderkey
   531              and c_custkey = o_custkey
   532              and s_nationkey = n1.n_nationkey
   533              and c_nationkey = n2.n_nationkey
   534              and (
   535                  (n1.n_name = 'FRANCE' and n2.n_name = 'ARGENTINA')
   536                  or (n1.n_name = 'ARGENTINA' and n2.n_name = 'FRANCE')
   537              )
   538              and l_shipdate between date '1995-01-01' and date '1996-12-31'
   539      ) as shipping
   540  group by
   541      supp_nation,
   542      cust_nation,
   543      l_year
   544  order by
   545      supp_nation,
   546      cust_nation,
   547      l_year
   548  ;
   549  
   550  select
   551      supp_nation,
   552      cust_nation,
   553      l_year,
   554      sum(volume) as revenue
   555  from
   556      (
   557          select
   558              n1.n_name as supp_nation,
   559              n2.n_name as cust_nation,
   560              extract(year from l_shipdate) as l_year,
   561              l_extendedprice * (1 - l_discount) as volume
   562          from
   563              supplier {snapshot = 'tpch_snapshot'},
   564              lineitem {snapshot = 'tpch_snapshot'},
   565              orders {snapshot = 'tpch_snapshot'},
   566              customer {snapshot = 'tpch_snapshot'},
   567              nation {snapshot = 'tpch_snapshot'} n1 ,
   568              nation {snapshot = 'tpch_snapshot'} n2 
   569          where
   570              s_suppkey = l_suppkey
   571              and o_orderkey = l_orderkey
   572              and c_custkey = o_custkey
   573              and s_nationkey = n1.n_nationkey
   574              and c_nationkey = n2.n_nationkey
   575              and (
   576                  (n1.n_name = 'FRANCE' and n2.n_name = 'ARGENTINA')
   577                  or (n1.n_name = 'ARGENTINA' and n2.n_name = 'FRANCE')
   578              )
   579              and l_shipdate between date '1995-01-01' and date '1996-12-31'
   580      ) as shipping
   581  group by
   582      supp_nation,
   583      cust_nation,
   584      l_year
   585  order by
   586      supp_nation,
   587      cust_nation,
   588      l_year
   589  ;
   590  -- tpch q8 --
   591  select
   592      o_year,
   593      (sum(case
   594          when nation = 'ARGENTINA' then volume
   595          else 0
   596      end) / sum(volume)) as mkt_share
   597  from
   598      (
   599          select
   600              extract(year from o_orderdate) as o_year,
   601              l_extendedprice * (1 - l_discount) as volume,
   602              n2.n_name as nation
   603          from
   604              part {snapshot = 'tpch_snapshot'},
   605              supplier {snapshot = 'tpch_snapshot'},
   606              lineitem {snapshot = 'tpch_snapshot'},
   607              orders {snapshot = 'tpch_snapshot'},
   608              customer {snapshot = 'tpch_snapshot'},
   609              nation {snapshot = 'tpch_snapshot'} n1,
   610              nation {snapshot = 'tpch_snapshot'} n2,
   611              region {snapshot = 'tpch_snapshot'}
   612          where
   613              p_partkey = l_partkey
   614              and s_suppkey = l_suppkey
   615              and l_orderkey = o_orderkey
   616              and o_custkey = c_custkey
   617              and c_nationkey = n1.n_nationkey
   618              and n1.n_regionkey = r_regionkey
   619              and r_name = 'AMERICA'
   620              and s_nationkey = n2.n_nationkey
   621              and o_orderdate between date '1995-01-01' and date '1996-12-31'
   622              and p_type = 'ECONOMY BURNISHED TIN'
   623      ) as all_nations
   624  group by
   625      o_year
   626  order by
   627      o_year
   628  ;
   629  
   630  select
   631      o_year,
   632      (sum(case
   633          when nation = 'ARGENTINA' then volume
   634          else 0
   635      end) / sum(volume)) as mkt_share
   636  from
   637      (
   638          select
   639              extract(year from o_orderdate) as o_year,
   640              l_extendedprice * (1 - l_discount) as volume,
   641              n2.n_name as nation
   642          from
   643              part {snapshot = 'tpch_snapshot'},
   644              supplier {snapshot = 'tpch_snapshot'},
   645              lineitem {snapshot = 'tpch_snapshot'},
   646              orders {snapshot = 'tpch_snapshot'},
   647              customer {snapshot = 'tpch_snapshot'},
   648              nation {snapshot = 'tpch_snapshot'} n1,
   649              nation {snapshot = 'tpch_snapshot'} n2,
   650              region {snapshot = 'tpch_snapshot'}
   651          where
   652              p_partkey = l_partkey
   653              and s_suppkey = l_suppkey
   654              and l_orderkey = o_orderkey
   655              and o_custkey = c_custkey
   656              and c_nationkey = n1.n_nationkey
   657              and n1.n_regionkey = r_regionkey
   658              and r_name = 'AMERICA'
   659              and s_nationkey = n2.n_nationkey
   660              and o_orderdate between date '1995-01-01' and date '1996-12-31'
   661              and p_type = 'ECONOMY BURNISHED TIN'
   662      ) as all_nations
   663  group by
   664      o_year
   665  order by
   666      o_year
   667  ;
   668  
   669  select
   670      o_year,
   671      (sum(case
   672          when nation = 'ARGENTINA' then volume
   673          else 0
   674      end) / sum(volume)) as mkt_share
   675  from
   676      (
   677          select
   678              extract(year from o_orderdate) as o_year,
   679              l_extendedprice * (1 - l_discount) as volume,
   680              n2.n_name as nation
   681          from
   682              part {snapshot = 'tpch_snapshot'},
   683              supplier {snapshot = 'tpch_snapshot'},
   684              lineitem {snapshot = 'tpch_snapshot'},
   685              orders {snapshot = 'tpch_snapshot'},
   686              customer {snapshot = 'tpch_snapshot'},
   687              nation {snapshot = 'tpch_snapshot'} n1,
   688              nation {snapshot = 'tpch_snapshot'} n2,
   689              region {snapshot = 'tpch_snapshot'}
   690          where
   691              p_partkey = l_partkey
   692              and s_suppkey = l_suppkey
   693              and l_orderkey = o_orderkey
   694              and o_custkey = c_custkey
   695              and c_nationkey = n1.n_nationkey
   696              and n1.n_regionkey = r_regionkey
   697              and r_name = 'AMERICA'
   698              and s_nationkey = n2.n_nationkey
   699              and o_orderdate between date '1995-01-01' and date '1996-12-31'
   700              and p_type = 'ECONOMY BURNISHED TIN'
   701      ) as all_nations
   702  group by
   703      o_year
   704  order by
   705      o_year
   706  ;
   707  
   708  -- tpch q9 --
   709  select
   710      nation,
   711      o_year,
   712      sum(amount) as sum_profit
   713  from
   714      (
   715          select
   716              n_name as nation,
   717              extract(year from o_orderdate) as o_year,
   718              l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
   719          from
   720              part {snapshot = 'tpch_snapshot'},
   721              supplier {snapshot = 'tpch_snapshot'},
   722              lineitem {snapshot = 'tpch_snapshot'},
   723              partsupp {snapshot = 'tpch_snapshot'},
   724              orders {snapshot = 'tpch_snapshot'},
   725              nation {snapshot = 'tpch_snapshot'}
   726          where
   727              s_suppkey = l_suppkey
   728              and ps_suppkey = l_suppkey
   729              and ps_partkey = l_partkey
   730              and p_partkey = l_partkey
   731              and o_orderkey = l_orderkey
   732              and s_nationkey = n_nationkey
   733              and p_name like '%pink%'
   734      ) as profit
   735  group by
   736      nation,
   737      o_year
   738  order by
   739      nation,
   740      o_year desc
   741  ;
   742  
   743  select
   744      nation,
   745      o_year,
   746      sum(amount) as sum_profit
   747  from
   748      (
   749          select
   750              n_name as nation,
   751              extract(year from o_orderdate) as o_year,
   752              l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
   753          from
   754              part {snapshot = 'tpch_snapshot'},
   755              supplier {snapshot = 'tpch_snapshot'},
   756              lineitem {snapshot = 'tpch_snapshot'},
   757              partsupp {snapshot = 'tpch_snapshot'},
   758              orders {snapshot = 'tpch_snapshot'},
   759              nation {snapshot = 'tpch_snapshot'}
   760          where
   761              s_suppkey = l_suppkey
   762              and ps_suppkey = l_suppkey
   763              and ps_partkey = l_partkey
   764              and p_partkey = l_partkey
   765              and o_orderkey = l_orderkey
   766              and s_nationkey = n_nationkey
   767              and p_name like '%pink%'
   768      ) as profit
   769  group by
   770      nation,
   771      o_year
   772  order by
   773      nation,
   774      o_year desc
   775  ;
   776  
   777  select
   778      nation,
   779      o_year,
   780      sum(amount) as sum_profit
   781  from
   782      (
   783          select
   784              n_name as nation,
   785              extract(year from o_orderdate) as o_year,
   786              l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity as amount
   787          from
   788              part {snapshot = 'tpch_snapshot'},
   789              supplier {snapshot = 'tpch_snapshot'},
   790              lineitem {snapshot = 'tpch_snapshot'},
   791              partsupp {snapshot = 'tpch_snapshot'},
   792              orders {snapshot = 'tpch_snapshot'},
   793              nation {snapshot = 'tpch_snapshot'}
   794          where
   795              s_suppkey = l_suppkey
   796              and ps_suppkey = l_suppkey
   797              and ps_partkey = l_partkey
   798              and p_partkey = l_partkey
   799              and o_orderkey = l_orderkey
   800              and s_nationkey = n_nationkey
   801              and p_name like '%pink%'
   802      ) as profit
   803  group by
   804      nation,
   805      o_year
   806  order by
   807      nation,
   808      o_year desc
   809  ;
   810  
   811  -- tpch q10 --
   812  select
   813  	c_custkey,
   814  	c_name,
   815  	sum(l_extendedprice * (1 - l_discount)) as revenue,
   816  	c_acctbal,
   817  	n_name,
   818  	c_address,
   819  	c_phone,
   820  	c_comment
   821  from
   822  	customer {snapshot = 'tpch_snapshot'},
   823  	orders {snapshot = 'tpch_snapshot'},
   824  	lineitem {snapshot = 'tpch_snapshot'},
   825  	nation {snapshot = 'tpch_snapshot'}
   826  where
   827  	c_custkey = o_custkey
   828  	and l_orderkey = o_orderkey
   829  	and o_orderdate >= date '1993-03-01'
   830  	and o_orderdate < date '1993-03-01' + interval '3' month
   831  	and l_returnflag = 'R'
   832  	and c_nationkey = n_nationkey
   833  group by
   834  	c_custkey,
   835  	c_name,
   836  	c_acctbal,
   837  	c_phone,
   838  	n_name,
   839  	c_address,
   840  	c_comment
   841  order by
   842  	revenue desc
   843  limit 20
   844  ;
   845  
   846  select
   847  	c_custkey,
   848  	c_name,
   849  	sum(l_extendedprice * (1 - l_discount)) as revenue,
   850  	c_acctbal,
   851  	n_name,
   852  	c_address,
   853  	c_phone,
   854  	c_comment
   855  from
   856  	customer {snapshot = 'tpch_snapshot'},
   857  	orders {snapshot = 'tpch_snapshot'},
   858  	lineitem {snapshot = 'tpch_snapshot'},
   859  	nation {snapshot = 'tpch_snapshot'}
   860  where
   861  	c_custkey = o_custkey
   862  	and l_orderkey = o_orderkey
   863  	and o_orderdate >= date '1993-03-01'
   864  	and o_orderdate < date '1993-03-01' + interval '3' month
   865  	and l_returnflag = 'R'
   866  	and c_nationkey = n_nationkey
   867  group by
   868  	c_custkey,
   869  	c_name,
   870  	c_acctbal,
   871  	c_phone,
   872  	n_name,
   873  	c_address,
   874  	c_comment
   875  order by
   876  	revenue desc
   877  limit 20
   878  ;
   879  
   880  
   881  select
   882  	c_custkey,
   883  	c_name,
   884  	sum(l_extendedprice * (1 - l_discount)) as revenue,
   885  	c_acctbal,
   886  	n_name,
   887  	c_address,
   888  	c_phone,
   889  	c_comment
   890  from
   891  	customer {snapshot = 'tpch_snapshot'},
   892  	orders {snapshot = 'tpch_snapshot'},
   893  	lineitem {snapshot = 'tpch_snapshot'},
   894  	nation {snapshot = 'tpch_snapshot'}
   895  where
   896  	c_custkey = o_custkey
   897  	and l_orderkey = o_orderkey
   898  	and o_orderdate >= date '1993-03-01'
   899  	and o_orderdate < date '1993-03-01' + interval '3' month
   900  	and l_returnflag = 'R'
   901  	and c_nationkey = n_nationkey
   902  group by
   903  	c_custkey,
   904  	c_name,
   905  	c_acctbal,
   906  	c_phone,
   907  	n_name,
   908  	c_address,
   909  	c_comment
   910  order by
   911  	revenue desc
   912  limit 20
   913  ;
   914  
   915  -- tpch q11 --
   916  select
   917      ps_partkey,
   918      sum(ps_supplycost * ps_availqty) as value
   919  from
   920      partsupp {snapshot = 'tpch_snapshot'},
   921      supplier {snapshot = 'tpch_snapshot'},
   922      nation {snapshot = 'tpch_snapshot'}
   923  where
   924      ps_suppkey = s_suppkey
   925      and s_nationkey = n_nationkey
   926      and n_name = 'JAPAN'
   927  group by
   928      ps_partkey having
   929          sum(ps_supplycost * ps_availqty) > (
   930          select
   931              sum(ps_supplycost * ps_availqty) * 0.0001000000
   932                  from
   933                  partsupp {snapshot = 'tpch_snapshot'},
   934                  supplier {snapshot = 'tpch_snapshot'},
   935                  nation {snapshot = 'tpch_snapshot'}
   936              where
   937                  ps_suppkey = s_suppkey
   938                  and s_nationkey = n_nationkey
   939                  and n_name = 'JAPAN'
   940          )
   941  order by
   942      value desc
   943  ;
   944  
   945  select
   946      ps_partkey,
   947      sum(ps_supplycost * ps_availqty) as value
   948  from
   949      partsupp {snapshot = 'tpch_snapshot'},
   950      supplier {snapshot = 'tpch_snapshot'},
   951      nation {snapshot = 'tpch_snapshot'}
   952  where
   953      ps_suppkey = s_suppkey
   954      and s_nationkey = n_nationkey
   955      and n_name = 'JAPAN'
   956  group by
   957      ps_partkey having
   958          sum(ps_supplycost * ps_availqty) > (
   959          select
   960              sum(ps_supplycost * ps_availqty) * 0.0001000000
   961                  from
   962                  partsupp {snapshot = 'tpch_snapshot'},
   963                  supplier {snapshot = 'tpch_snapshot'},
   964                  nation {snapshot = 'tpch_snapshot'}
   965              where
   966                  ps_suppkey = s_suppkey
   967                  and s_nationkey = n_nationkey
   968                  and n_name = 'JAPAN'
   969          )
   970  order by
   971      value desc
   972  ;
   973  
   974  select
   975      ps_partkey,
   976      sum(ps_supplycost * ps_availqty) as value
   977  from
   978      partsupp {snapshot = 'tpch_snapshot'},
   979      supplier {snapshot = 'tpch_snapshot'},
   980      nation {snapshot = 'tpch_snapshot'}
   981  where
   982      ps_suppkey = s_suppkey
   983      and s_nationkey = n_nationkey
   984      and n_name = 'JAPAN'
   985  group by
   986      ps_partkey having
   987          sum(ps_supplycost * ps_availqty) > (
   988          select
   989              sum(ps_supplycost * ps_availqty) * 0.0001000000
   990                  from
   991                  partsupp {snapshot = 'tpch_snapshot'},
   992                  supplier {snapshot = 'tpch_snapshot'},
   993                  nation {snapshot = 'tpch_snapshot'}
   994              where
   995                  ps_suppkey = s_suppkey
   996                  and s_nationkey = n_nationkey
   997                  and n_name = 'JAPAN'
   998          )
   999  order by
  1000      value desc
  1001  ;
  1002  
  1003  -- tpch q12 --
  1004  select
  1005          l_shipmode,
  1006          sum(case
  1007                  when o_orderpriority = '1-URGENT'
  1008                          or o_orderpriority = '2-HIGH'
  1009                          then 1
  1010                  else 0
  1011          end) as high_line_count,
  1012          sum(case
  1013                  when o_orderpriority <> '1-URGENT'
  1014                          and o_orderpriority <> '2-HIGH'
  1015                          then 1
  1016                  else 0
  1017          end) as low_line_count
  1018  from
  1019          orders {snapshot = 'tpch_snapshot'},
  1020          lineitem {snapshot = 'tpch_snapshot'}
  1021  where
  1022          o_orderkey = l_orderkey
  1023          and l_shipmode in ('FOB', 'TRUCK')
  1024          and l_commitdate < l_receiptdate
  1025          and l_shipdate < l_commitdate
  1026          and l_receiptdate >= date '1996-01-01'
  1027          and l_receiptdate < date '1996-01-01' + interval '1' year
  1028  group by
  1029          l_shipmode
  1030  order by
  1031          l_shipmode
  1032  ;
  1033  
  1034  select
  1035          l_shipmode,
  1036          sum(case
  1037                  when o_orderpriority = '1-URGENT'
  1038                          or o_orderpriority = '2-HIGH'
  1039                          then 1
  1040                  else 0
  1041          end) as high_line_count,
  1042          sum(case
  1043                  when o_orderpriority <> '1-URGENT'
  1044                          and o_orderpriority <> '2-HIGH'
  1045                          then 1
  1046                  else 0
  1047          end) as low_line_count
  1048  from
  1049          orders {snapshot = 'tpch_snapshot'},
  1050          lineitem {snapshot = 'tpch_snapshot'}
  1051  where
  1052          o_orderkey = l_orderkey
  1053          and l_shipmode in ('FOB', 'TRUCK')
  1054          and l_commitdate < l_receiptdate
  1055          and l_shipdate < l_commitdate
  1056          and l_receiptdate >= date '1996-01-01'
  1057          and l_receiptdate < date '1996-01-01' + interval '1' year
  1058  group by
  1059          l_shipmode
  1060  order by
  1061          l_shipmode
  1062  ;
  1063  
  1064  select
  1065          l_shipmode,
  1066          sum(case
  1067                  when o_orderpriority = '1-URGENT'
  1068                          or o_orderpriority = '2-HIGH'
  1069                          then 1
  1070                  else 0
  1071          end) as high_line_count,
  1072          sum(case
  1073                  when o_orderpriority <> '1-URGENT'
  1074                          and o_orderpriority <> '2-HIGH'
  1075                          then 1
  1076                  else 0
  1077          end) as low_line_count
  1078  from
  1079          orders {snapshot = 'tpch_snapshot'},
  1080          lineitem {snapshot = 'tpch_snapshot'}
  1081  where
  1082          o_orderkey = l_orderkey
  1083          and l_shipmode in ('FOB', 'TRUCK')
  1084          and l_commitdate < l_receiptdate
  1085          and l_shipdate < l_commitdate
  1086          and l_receiptdate >= date '1996-01-01'
  1087          and l_receiptdate < date '1996-01-01' + interval '1' year
  1088  group by
  1089          l_shipmode
  1090  order by
  1091          l_shipmode
  1092  ;
  1093  
  1094  -- tpch q13 --
  1095  select
  1096  	c_count,
  1097  	count(*) as custdist
  1098  from
  1099  	(
  1100  		select
  1101  			c_custkey,
  1102  			count(o_orderkey)
  1103  		from
  1104  			customer {snapshot = 'tpch_snapshot'} left outer join orders {snapshot = 'tpch_snapshot'} on
  1105  				c_custkey = o_custkey
  1106  				and o_comment not like '%pending%accounts%'
  1107  		group by
  1108  			c_custkey
  1109  	) as c_orders (c_custkey, c_count)
  1110  group by
  1111  	c_count
  1112  order by
  1113  	custdist desc,
  1114  	c_count desc
  1115  ;
  1116  
  1117  select
  1118  	c_count,
  1119  	count(*) as custdist
  1120  from
  1121  	(
  1122  		select
  1123  			c_custkey,
  1124  			count(o_orderkey)
  1125  		from
  1126  			customer {snapshot = 'tpch_snapshot'} left outer join orders {snapshot = 'tpch_snapshot'} on
  1127  				c_custkey = o_custkey
  1128  				and o_comment not like '%pending%accounts%'
  1129  		group by
  1130  			c_custkey
  1131  	) as c_orders (c_custkey, c_count)
  1132  group by
  1133  	c_count
  1134  order by
  1135  	custdist desc,
  1136  	c_count desc
  1137  ;
  1138  
  1139  select
  1140  	c_count,
  1141  	count(*) as custdist
  1142  from
  1143  	(
  1144  		select
  1145  			c_custkey,
  1146  			count(o_orderkey)
  1147  		from
  1148  			customer {snapshot = 'tpch_snapshot'} left outer join orders {snapshot = 'tpch_snapshot'} on
  1149  				c_custkey = o_custkey
  1150  				and o_comment not like '%pending%accounts%'
  1151  		group by
  1152  			c_custkey
  1153  	) as c_orders (c_custkey, c_count)
  1154  group by
  1155  	c_count
  1156  order by
  1157  	custdist desc,
  1158  	c_count desc
  1159  ;
  1160  
  1161  -- tpch q14 --
  1162  select
  1163  	100.00 * sum(case
  1164  		when p_type like 'PROMO%'
  1165  			then l_extendedprice * (1 - l_discount)
  1166  		else 0
  1167  	end) / sum(l_extendedprice * (1 - l_discount)) as promo_revenue
  1168  from
  1169  	lineitem {snapshot = 'tpch_snapshot'},
  1170  	part {snapshot = 'tpch_snapshot'}
  1171  where
  1172  	l_partkey = p_partkey
  1173  	and l_shipdate >= date '1996-04-01'
  1174  	and l_shipdate < date '1996-04-01' + interval '1' month;
  1175  
  1176  select
  1177  	100.00 * sum(case
  1178  		when p_type like 'PROMO%'
  1179  			then l_extendedprice * (1 - l_discount)
  1180  		else 0
  1181  	end) / sum(l_extendedprice * (1 - l_discount)) as promo_revenue
  1182  from
  1183  	lineitem {snapshot = 'tpch_snapshot'},
  1184  	part {snapshot = 'tpch_snapshot'}
  1185  where
  1186  	l_partkey = p_partkey
  1187  	and l_shipdate >= date '1996-04-01'
  1188  	and l_shipdate < date '1996-04-01' + interval '1' month;
  1189  
  1190  select
  1191  	100.00 * sum(case
  1192  		when p_type like 'PROMO%'
  1193  			then l_extendedprice * (1 - l_discount)
  1194  		else 0
  1195  	end) / sum(l_extendedprice * (1 - l_discount)) as promo_revenue
  1196  from
  1197  	lineitem {snapshot = 'tpch_snapshot'},
  1198  	part {snapshot = 'tpch_snapshot'}
  1199  where
  1200  	l_partkey = p_partkey
  1201  	and l_shipdate >= date '1996-04-01'
  1202  	and l_shipdate < date '1996-04-01' + interval '1' month;
  1203  
  1204  -- tpch q15 --
  1205  with q15_revenue0 as (
  1206      select
  1207          l_suppkey as supplier_no,
  1208          sum(l_extendedprice * (1 - l_discount)) as total_revenue
  1209      from
  1210          lineitem {snapshot = 'tpch_snapshot'}
  1211      where
  1212          l_shipdate >= date '1995-12-01'
  1213          and l_shipdate < date '1995-12-01' + interval '3' month
  1214      group by
  1215          l_suppkey
  1216      )
  1217  select
  1218      s_suppkey,
  1219      s_name,
  1220      s_address,
  1221      s_phone,
  1222      total_revenue
  1223  from
  1224      supplier {snapshot = 'tpch_snapshot'},
  1225      q15_revenue0 {snapshot = 'tpch_snapshot'}
  1226  where
  1227      s_suppkey = supplier_no
  1228      and total_revenue = (
  1229          select
  1230              max(total_revenue)
  1231          from
  1232              q15_revenue0 {snapshot = 'tpch_snapshot'}
  1233      )
  1234  order by
  1235      s_suppkey
  1236  ;
  1237  
  1238  with q15_revenue0 as (
  1239      select
  1240          l_suppkey as supplier_no,
  1241          sum(l_extendedprice * (1 - l_discount)) as total_revenue
  1242      from
  1243          lineitem {snapshot = 'tpch_snapshot'}
  1244      where
  1245          l_shipdate >= date '1995-12-01'
  1246          and l_shipdate < date '1995-12-01' + interval '3' month
  1247      group by
  1248          l_suppkey
  1249      )
  1250  select
  1251      s_suppkey,
  1252      s_name,
  1253      s_address,
  1254      s_phone,
  1255      total_revenue
  1256  from
  1257      supplier {snapshot = 'tpch_snapshot'},
  1258      q15_revenue0 {snapshot = 'tpch_snapshot'}
  1259  where
  1260      s_suppkey = supplier_no
  1261      and total_revenue = (
  1262          select
  1263              max(total_revenue)
  1264          from
  1265              q15_revenue0 {snapshot = 'tpch_snapshot'}
  1266      )
  1267  order by
  1268      s_suppkey
  1269  ;
  1270  
  1271  with q15_revenue0 as (
  1272      select
  1273          l_suppkey as supplier_no,
  1274          sum(l_extendedprice * (1 - l_discount)) as total_revenue
  1275      from
  1276          lineitem {snapshot = 'tpch_snapshot'}
  1277      where
  1278          l_shipdate >= date '1995-12-01'
  1279          and l_shipdate < date '1995-12-01' + interval '3' month
  1280      group by
  1281          l_suppkey
  1282      )
  1283  select
  1284      s_suppkey,
  1285      s_name,
  1286      s_address,
  1287      s_phone,
  1288      total_revenue
  1289  from
  1290      supplier {snapshot = 'tpch_snapshot'},
  1291      q15_revenue0 {snapshot = 'tpch_snapshot'}
  1292  where
  1293      s_suppkey = supplier_no
  1294      and total_revenue = (
  1295          select
  1296              max(total_revenue)
  1297          from
  1298              q15_revenue0 {snapshot = 'tpch_snapshot'}
  1299      )
  1300  order by
  1301      s_suppkey
  1302  ;
  1303  
  1304  -- tpch q16 --
  1305  select
  1306      p_brand,
  1307      p_type,
  1308      p_size,
  1309      count(distinct ps_suppkey) as supplier_cnt
  1310  from
  1311      partsupp {snapshot = 'tpch_snapshot'},
  1312      part {snapshot = 'tpch_snapshot'}
  1313  where
  1314      p_partkey = ps_partkey
  1315      and p_brand <> 'Brand#35'
  1316      and p_type not like 'ECONOMY BURNISHED%'
  1317      and p_size in (14, 7, 21, 24, 35, 33, 2, 20)
  1318      and ps_suppkey not in (
  1319          select
  1320              s_suppkey
  1321          from
  1322              supplier {snapshot = 'tpch_snapshot'}
  1323          where
  1324              s_comment like '%Customer%Complaints%'
  1325      )
  1326  group by
  1327      p_brand,
  1328      p_type,
  1329      p_size
  1330  order by
  1331      supplier_cnt desc,
  1332      p_brand,
  1333      p_type,
  1334      p_size
  1335  ;
  1336  
  1337  select
  1338      p_brand,
  1339      p_type,
  1340      p_size,
  1341      count(distinct ps_suppkey) as supplier_cnt
  1342  from
  1343      partsupp {snapshot = 'tpch_snapshot'},
  1344      part {snapshot = 'tpch_snapshot'}
  1345  where
  1346      p_partkey = ps_partkey
  1347      and p_brand <> 'Brand#35'
  1348      and p_type not like 'ECONOMY BURNISHED%'
  1349      and p_size in (14, 7, 21, 24, 35, 33, 2, 20)
  1350      and ps_suppkey not in (
  1351          select
  1352              s_suppkey
  1353          from
  1354              supplier {snapshot = 'tpch_snapshot'}
  1355          where
  1356              s_comment like '%Customer%Complaints%'
  1357      )
  1358  group by
  1359      p_brand,
  1360      p_type,
  1361      p_size
  1362  order by
  1363      supplier_cnt desc,
  1364      p_brand,
  1365      p_type,
  1366      p_size
  1367  ;
  1368  
  1369  select
  1370      p_brand,
  1371      p_type,
  1372      p_size,
  1373      count(distinct ps_suppkey) as supplier_cnt
  1374  from
  1375      partsupp {snapshot = 'tpch_snapshot'},
  1376      part {snapshot = 'tpch_snapshot'}
  1377  where
  1378      p_partkey = ps_partkey
  1379      and p_brand <> 'Brand#35'
  1380      and p_type not like 'ECONOMY BURNISHED%'
  1381      and p_size in (14, 7, 21, 24, 35, 33, 2, 20)
  1382      and ps_suppkey not in (
  1383          select
  1384              s_suppkey
  1385          from
  1386              supplier {snapshot = 'tpch_snapshot'}
  1387          where
  1388              s_comment like '%Customer%Complaints%'
  1389      )
  1390  group by
  1391      p_brand,
  1392      p_type,
  1393      p_size
  1394  order by
  1395      supplier_cnt desc,
  1396      p_brand,
  1397      p_type,
  1398      p_size
  1399  ;
  1400  
  1401  -- tpch q17 --
  1402  select
  1403      sum(l_extendedprice) / 7.0 as avg_yearly
  1404  from
  1405      lineitem {snapshot = 'tpch_snapshot'},
  1406      part {snapshot = 'tpch_snapshot'}
  1407  where
  1408      p_partkey = l_partkey
  1409      and p_brand = 'Brand#54'
  1410      and p_container = 'LG BAG'
  1411      and l_quantity < (
  1412          select
  1413              0.2 * avg(l_quantity)
  1414          from
  1415              lineitem {snapshot = 'tpch_snapshot'}
  1416          where
  1417              l_partkey = p_partkey
  1418      );
  1419  
  1420  select
  1421      sum(l_extendedprice) / 7.0 as avg_yearly
  1422  from
  1423      lineitem {snapshot = 'tpch_snapshot'},
  1424      part {snapshot = 'tpch_snapshot'}
  1425  where
  1426      p_partkey = l_partkey
  1427      and p_brand = 'Brand#54'
  1428      and p_container = 'LG BAG'
  1429      and l_quantity < (
  1430          select
  1431              0.2 * avg(l_quantity)
  1432          from
  1433              lineitem {snapshot = 'tpch_snapshot'}
  1434          where
  1435              l_partkey = p_partkey
  1436      );
  1437  
  1438  select
  1439      sum(l_extendedprice) / 7.0 as avg_yearly
  1440  from
  1441      lineitem {snapshot = 'tpch_snapshot'},
  1442      part {snapshot = 'tpch_snapshot'}
  1443  where
  1444      p_partkey = l_partkey
  1445      and p_brand = 'Brand#54'
  1446      and p_container = 'LG BAG'
  1447      and l_quantity < (
  1448          select
  1449              0.2 * avg(l_quantity)
  1450          from
  1451              lineitem {snapshot = 'tpch_snapshot'}
  1452          where
  1453              l_partkey = p_partkey
  1454      );
  1455  
  1456  -- tpch q18 --
  1457  select
  1458      c_name,
  1459      c_custkey,
  1460      o_orderkey,
  1461      o_orderdate,
  1462      o_totalprice,
  1463      sum(l_quantity)
  1464  from
  1465      customer {snapshot = 'tpch_snapshot'},
  1466      orders {snapshot = 'tpch_snapshot'},
  1467      lineitem {snapshot = 'tpch_snapshot'}
  1468  where
  1469      o_orderkey in (
  1470          select
  1471              l_orderkey
  1472          from
  1473              lineitem {snapshot = 'tpch_snapshot'}
  1474          group by
  1475              l_orderkey having
  1476                  sum(l_quantity) > 314
  1477      )
  1478      and c_custkey = o_custkey
  1479      and o_orderkey = l_orderkey
  1480  group by
  1481      c_name,
  1482      c_custkey,
  1483      o_orderkey,
  1484      o_orderdate,
  1485      o_totalprice
  1486  order by
  1487      o_totalprice desc,
  1488      o_orderdate
  1489  limit 100
  1490  ;
  1491  
  1492  select
  1493      c_name,
  1494      c_custkey,
  1495      o_orderkey,
  1496      o_orderdate,
  1497      o_totalprice,
  1498      sum(l_quantity)
  1499  from
  1500      customer {snapshot = 'tpch_snapshot'},
  1501      orders {snapshot = 'tpch_snapshot'},
  1502      lineitem {snapshot = 'tpch_snapshot'}
  1503  where
  1504      o_orderkey in (
  1505          select
  1506              l_orderkey
  1507          from
  1508              lineitem {snapshot = 'tpch_snapshot'}
  1509          group by
  1510              l_orderkey having
  1511                  sum(l_quantity) > 314
  1512      )
  1513      and c_custkey = o_custkey
  1514      and o_orderkey = l_orderkey
  1515  group by
  1516      c_name,
  1517      c_custkey,
  1518      o_orderkey,
  1519      o_orderdate,
  1520      o_totalprice
  1521  order by
  1522      o_totalprice desc,
  1523      o_orderdate
  1524  limit 100
  1525  ;
  1526  
  1527  select
  1528      c_name,
  1529      c_custkey,
  1530      o_orderkey,
  1531      o_orderdate,
  1532      o_totalprice,
  1533      sum(l_quantity)
  1534  from
  1535      customer {snapshot = 'tpch_snapshot'},
  1536      orders {snapshot = 'tpch_snapshot'},
  1537      lineitem {snapshot = 'tpch_snapshot'}
  1538  where
  1539      o_orderkey in (
  1540          select
  1541              l_orderkey
  1542          from
  1543              lineitem {snapshot = 'tpch_snapshot'}
  1544          group by
  1545              l_orderkey having
  1546                  sum(l_quantity) > 314
  1547      )
  1548      and c_custkey = o_custkey
  1549      and o_orderkey = l_orderkey
  1550  group by
  1551      c_name,
  1552      c_custkey,
  1553      o_orderkey,
  1554      o_orderdate,
  1555      o_totalprice
  1556  order by
  1557      o_totalprice desc,
  1558      o_orderdate
  1559  limit 100
  1560  ;
  1561  
  1562  -- tpch q19 --
  1563  select
  1564      sum(l_extendedprice* (1 - l_discount)) as revenue
  1565  from
  1566      lineitem {snapshot = 'tpch_snapshot'},
  1567      part {snapshot = 'tpch_snapshot'}
  1568  where
  1569      (
  1570          p_partkey = l_partkey
  1571          and p_brand = 'Brand#23'
  1572          and p_container in ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG')
  1573          and l_quantity >= 5 and l_quantity <= 5 + 10
  1574          and p_size between 1 and 5
  1575          and l_shipmode in ('AIR', 'AIR REG')
  1576          and l_shipinstruct = 'DELIVER IN PERSON'
  1577      )
  1578      or
  1579      (
  1580          p_partkey = l_partkey
  1581          and p_brand = 'Brand#15'
  1582          and p_container in ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK')
  1583          and l_quantity >= 14 and l_quantity <= 14 + 10
  1584          and p_size between 1 and 10
  1585          and l_shipmode in ('AIR', 'AIR REG')
  1586          and l_shipinstruct = 'DELIVER IN PERSON'
  1587      )
  1588      or
  1589      (
  1590          p_partkey = l_partkey
  1591          and p_brand = 'Brand#44'
  1592          and p_container in ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG')
  1593          and l_quantity >= 28 and l_quantity <= 28 + 10
  1594          and p_size between 1 and 15
  1595          and l_shipmode in ('AIR', 'AIR REG')
  1596          and l_shipinstruct = 'DELIVER IN PERSON'
  1597      );
  1598  
  1599  select
  1600      sum(l_extendedprice* (1 - l_discount)) as revenue
  1601  from
  1602      lineitem {snapshot = 'tpch_snapshot'},
  1603      part {snapshot = 'tpch_snapshot'}
  1604  where
  1605      (
  1606          p_partkey = l_partkey
  1607          and p_brand = 'Brand#23'
  1608          and p_container in ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG')
  1609          and l_quantity >= 5 and l_quantity <= 5 + 10
  1610          and p_size between 1 and 5
  1611          and l_shipmode in ('AIR', 'AIR REG')
  1612          and l_shipinstruct = 'DELIVER IN PERSON'
  1613      )
  1614      or
  1615      (
  1616          p_partkey = l_partkey
  1617          and p_brand = 'Brand#15'
  1618          and p_container in ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK')
  1619          and l_quantity >= 14 and l_quantity <= 14 + 10
  1620          and p_size between 1 and 10
  1621          and l_shipmode in ('AIR', 'AIR REG')
  1622          and l_shipinstruct = 'DELIVER IN PERSON'
  1623      )
  1624      or
  1625      (
  1626          p_partkey = l_partkey
  1627          and p_brand = 'Brand#44'
  1628          and p_container in ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG')
  1629          and l_quantity >= 28 and l_quantity <= 28 + 10
  1630          and p_size between 1 and 15
  1631          and l_shipmode in ('AIR', 'AIR REG')
  1632          and l_shipinstruct = 'DELIVER IN PERSON'
  1633      );
  1634  
  1635  select
  1636      sum(l_extendedprice* (1 - l_discount)) as revenue
  1637  from
  1638      lineitem {snapshot = 'tpch_snapshot'},
  1639      part {snapshot = 'tpch_snapshot'}
  1640  where
  1641      (
  1642          p_partkey = l_partkey
  1643          and p_brand = 'Brand#23'
  1644          and p_container in ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG')
  1645          and l_quantity >= 5 and l_quantity <= 5 + 10
  1646          and p_size between 1 and 5
  1647          and l_shipmode in ('AIR', 'AIR REG')
  1648          and l_shipinstruct = 'DELIVER IN PERSON'
  1649      )
  1650      or
  1651      (
  1652          p_partkey = l_partkey
  1653          and p_brand = 'Brand#15'
  1654          and p_container in ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK')
  1655          and l_quantity >= 14 and l_quantity <= 14 + 10
  1656          and p_size between 1 and 10
  1657          and l_shipmode in ('AIR', 'AIR REG')
  1658          and l_shipinstruct = 'DELIVER IN PERSON'
  1659      )
  1660      or
  1661      (
  1662          p_partkey = l_partkey
  1663          and p_brand = 'Brand#44'
  1664          and p_container in ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG')
  1665          and l_quantity >= 28 and l_quantity <= 28 + 10
  1666          and p_size between 1 and 15
  1667          and l_shipmode in ('AIR', 'AIR REG')
  1668          and l_shipinstruct = 'DELIVER IN PERSON'
  1669      );
  1670  
  1671  -- tpch q20 --
  1672  select
  1673      s_name,
  1674      s_address
  1675  from
  1676      supplier {snapshot = 'tpch_snapshot'},
  1677      nation {snapshot = 'tpch_snapshot'}
  1678  where
  1679      s_suppkey in (
  1680          select
  1681              ps_suppkey
  1682          from
  1683              partsupp {snapshot = 'tpch_snapshot'}
  1684          where
  1685              ps_partkey in (
  1686                  select
  1687                      p_partkey
  1688                  from
  1689                      part {snapshot = 'tpch_snapshot'}
  1690                  where
  1691                      p_name like 'lime%'
  1692              )
  1693              and ps_availqty > (
  1694                  select
  1695                      0.5 * sum(l_quantity)
  1696                  from
  1697                      lineitem {snapshot = 'tpch_snapshot'}
  1698                  where
  1699                      l_partkey = ps_partkey
  1700                      and l_suppkey = ps_suppkey
  1701                      and l_shipdate >= date '1993-01-01'
  1702                      and l_shipdate < date '1993-01-01' + interval '1' year
  1703              )
  1704      )
  1705      and s_nationkey = n_nationkey
  1706      and n_name = 'VIETNAM'
  1707  order by s_name
  1708  ;
  1709  
  1710  select
  1711      s_name,
  1712      s_address
  1713  from
  1714      supplier {snapshot = 'tpch_snapshot'},
  1715      nation {snapshot = 'tpch_snapshot'}
  1716  where
  1717      s_suppkey in (
  1718          select
  1719              ps_suppkey
  1720          from
  1721              partsupp {snapshot = 'tpch_snapshot'}
  1722          where
  1723              ps_partkey in (
  1724                  select
  1725                      p_partkey
  1726                  from
  1727                      part {snapshot = 'tpch_snapshot'}
  1728                  where
  1729                      p_name like 'lime%'
  1730              )
  1731              and ps_availqty > (
  1732                  select
  1733                      0.5 * sum(l_quantity)
  1734                  from
  1735                      lineitem {snapshot = 'tpch_snapshot'}
  1736                  where
  1737                      l_partkey = ps_partkey
  1738                      and l_suppkey = ps_suppkey
  1739                      and l_shipdate >= date '1993-01-01'
  1740                      and l_shipdate < date '1993-01-01' + interval '1' year
  1741              )
  1742      )
  1743      and s_nationkey = n_nationkey
  1744      and n_name = 'VIETNAM'
  1745  order by s_name
  1746  ;
  1747  
  1748  select
  1749      s_name,
  1750      s_address
  1751  from
  1752      supplier {snapshot = 'tpch_snapshot'},
  1753      nation {snapshot = 'tpch_snapshot'}
  1754  where
  1755      s_suppkey in (
  1756          select
  1757              ps_suppkey
  1758          from
  1759              partsupp {snapshot = 'tpch_snapshot'}
  1760          where
  1761              ps_partkey in (
  1762                  select
  1763                      p_partkey
  1764                  from
  1765                      part {snapshot = 'tpch_snapshot'}
  1766                  where
  1767                      p_name like 'lime%'
  1768              )
  1769              and ps_availqty > (
  1770                  select
  1771                      0.5 * sum(l_quantity)
  1772                  from
  1773                      lineitem {snapshot = 'tpch_snapshot'}
  1774                  where
  1775                      l_partkey = ps_partkey
  1776                      and l_suppkey = ps_suppkey
  1777                      and l_shipdate >= date '1993-01-01'
  1778                      and l_shipdate < date '1993-01-01' + interval '1' year
  1779              )
  1780      )
  1781      and s_nationkey = n_nationkey
  1782      and n_name = 'VIETNAM'
  1783  order by s_name
  1784  ;
  1785  
  1786  -- tpch q21 --
  1787  select
  1788      s_name,
  1789      count(*) as numwait
  1790  from
  1791      supplier {snapshot = 'tpch_snapshot'},
  1792      lineitem l1 {snapshot = 'tpch_snapshot'},
  1793      orders {snapshot = 'tpch_snapshot'},
  1794      nation {snapshot = 'tpch_snapshot'}
  1795  where
  1796      s_suppkey = l1.l_suppkey
  1797      and o_orderkey = l1.l_orderkey
  1798      and o_orderstatus = 'F'
  1799      and l1.l_receiptdate > l1.l_commitdate
  1800      and exists (
  1801          select
  1802              *
  1803          from
  1804              lineitem l2 {snapshot = 'tpch_snapshot'}
  1805          where
  1806              l2.l_orderkey = l1.l_orderkey
  1807              and l2.l_suppkey <> l1.l_suppkey
  1808      )
  1809      and not exists (
  1810          select
  1811              *
  1812          from
  1813              lineitem l3 {snapshot = 'tpch_snapshot'}
  1814          where
  1815              l3.l_orderkey = l1.l_orderkey
  1816              and l3.l_suppkey <> l1.l_suppkey
  1817              and l3.l_receiptdate > l3.l_commitdate
  1818      )
  1819      and s_nationkey = n_nationkey
  1820      and n_name = 'BRAZIL'
  1821  group by
  1822      s_name
  1823  order by
  1824      numwait desc,
  1825      s_name
  1826  limit 100
  1827  ;
  1828  
  1829  select
  1830      s_name,
  1831      count(*) as numwait
  1832  from
  1833      supplier {snapshot = 'tpch_snapshot'},
  1834      lineitem l1 {snapshot = 'tpch_snapshot'},
  1835      orders {snapshot = 'tpch_snapshot'},
  1836      nation {snapshot = 'tpch_snapshot'}
  1837  where
  1838      s_suppkey = l1.l_suppkey
  1839      and o_orderkey = l1.l_orderkey
  1840      and o_orderstatus = 'F'
  1841      and l1.l_receiptdate > l1.l_commitdate
  1842      and exists (
  1843          select
  1844              *
  1845          from
  1846              lineitem l2 {snapshot = 'tpch_snapshot'}
  1847          where
  1848              l2.l_orderkey = l1.l_orderkey
  1849              and l2.l_suppkey <> l1.l_suppkey
  1850      )
  1851      and not exists (
  1852          select
  1853              *
  1854          from
  1855              lineitem l3 {snapshot = 'tpch_snapshot'}
  1856          where
  1857              l3.l_orderkey = l1.l_orderkey
  1858              and l3.l_suppkey <> l1.l_suppkey
  1859              and l3.l_receiptdate > l3.l_commitdate
  1860      )
  1861      and s_nationkey = n_nationkey
  1862      and n_name = 'BRAZIL'
  1863  group by
  1864      s_name
  1865  order by
  1866      numwait desc,
  1867      s_name
  1868  limit 100
  1869  ;
  1870  
  1871  select
  1872      s_name,
  1873      count(*) as numwait
  1874  from
  1875      supplier {snapshot = 'tpch_snapshot'},
  1876      lineitem l1 {snapshot = 'tpch_snapshot'},
  1877      orders {snapshot = 'tpch_snapshot'},
  1878      nation {snapshot = 'tpch_snapshot'}
  1879  where
  1880      s_suppkey = l1.l_suppkey
  1881      and o_orderkey = l1.l_orderkey
  1882      and o_orderstatus = 'F'
  1883      and l1.l_receiptdate > l1.l_commitdate
  1884      and exists (
  1885          select
  1886              *
  1887          from
  1888              lineitem l2 {snapshot = 'tpch_snapshot'}
  1889          where
  1890              l2.l_orderkey = l1.l_orderkey
  1891              and l2.l_suppkey <> l1.l_suppkey
  1892      )
  1893      and not exists (
  1894          select
  1895              *
  1896          from
  1897              lineitem l3 {snapshot = 'tpch_snapshot'}
  1898          where
  1899              l3.l_orderkey = l1.l_orderkey
  1900              and l3.l_suppkey <> l1.l_suppkey
  1901              and l3.l_receiptdate > l3.l_commitdate
  1902      )
  1903      and s_nationkey = n_nationkey
  1904      and n_name = 'BRAZIL'
  1905  group by
  1906      s_name
  1907  order by
  1908      numwait desc,
  1909      s_name
  1910  limit 100
  1911  ;
  1912  
  1913  drop database if exists TPCH;
  1914  drop snapshot tpch_snapshot;
  1915  drop snapshot tpch_cluster;