buyingyi | cf48fb5 | 2012-11-02 00:31:31 +0000 | [diff] [blame] | 1 | -- create the tables and load the data |
| 2 | create external table lineitem (L_ORDERKEY INT, L_PARTKEY INT, L_SUPPKEY INT, L_LINENUMBER INT, L_QUANTITY DOUBLE, L_EXTENDEDPRICE DOUBLE, L_DISCOUNT DOUBLE, L_TAX DOUBLE, L_RETURNFLAG STRING, L_LINESTATUS STRING, L_SHIPDATE STRING, L_COMMITDATE STRING, L_RECEIPTDATE STRING, L_SHIPINSTRUCT STRING, L_SHIPMODE STRING, L_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/lineitem'; |
| 3 | create external table part (P_PARTKEY INT, P_NAME STRING, P_MFGR STRING, P_BRAND STRING, P_TYPE STRING, P_SIZE INT, P_CONTAINER STRING, P_RETAILPRICE DOUBLE, P_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/part'; |
| 4 | |
| 5 | -- create the result table |
| 6 | create table q17_small_quantity_order_revenue (avg_yearly double); |
| 7 | create table lineitem_tmp (t_partkey int, t_avg_quantity double); |
| 8 | |
| 9 | -- the query |
| 10 | insert overwrite table lineitem_tmp |
| 11 | select |
| 12 | l_partkey as t_partkey, 0.2 * avg(l_quantity) as t_avg_quantity |
| 13 | from |
| 14 | lineitem |
| 15 | group by l_partkey; |
| 16 | |
| 17 | insert overwrite table q17_small_quantity_order_revenue |
| 18 | select |
| 19 | sum(l_extendedprice) / 7.0 as avg_yearly |
| 20 | from |
| 21 | (select l_quantity, l_extendedprice, t_avg_quantity from |
| 22 | lineitem_tmp t join |
| 23 | (select |
| 24 | l_quantity, l_partkey, l_extendedprice |
| 25 | from |
| 26 | part p join lineitem l |
| 27 | on |
| 28 | p.p_partkey = l.l_partkey |
| 29 | and p.p_brand = 'Brand#23' |
| 30 | and p.p_container = 'MED BOX' |
| 31 | ) l1 on l1.l_partkey = t.t_partkey |
| 32 | ) a |
| 33 | where l_quantity < t_avg_quantity; |
| 34 | |
| 35 | DROP TABLE lineitem; |
| 36 | DROP TABLE part; |
| 37 | DROP TABLE q17_small_quantity_order_revenue; |
| 38 | DROP TABLE lineitem_tmp; |