buyingyi | c73348c | 2012-11-02 00:31:31 +0000 | [diff] [blame] | 1 | -- create tables and load data |
| 2 | create external table supplier (S_SUPPKEY INT, S_NAME STRING, S_ADDRESS STRING, S_NATIONKEY INT, S_PHONE STRING, S_ACCTBAL DOUBLE, S_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/supplier'; |
| 3 | create external table nation (N_NATIONKEY INT, N_NAME STRING, N_REGIONKEY INT, N_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/nation'; |
| 4 | create external table partsupp (PS_PARTKEY INT, PS_SUPPKEY INT, PS_AVAILQTY INT, PS_SUPPLYCOST DOUBLE, PS_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION'/tpch/100/partsupp'; |
| 5 | |
| 6 | -- create the target table |
| 7 | create table q11_important_stock(ps_partkey INT, value DOUBLE); |
| 8 | create table q11_part_tmp(ps_partkey int, part_value double); |
| 9 | create table q11_sum_tmp(total_value double); |
| 10 | |
| 11 | -- the query |
| 12 | insert overwrite table q11_part_tmp |
| 13 | select |
| 14 | ps_partkey, sum(ps_supplycost * ps_availqty) as part_value |
| 15 | from |
| 16 | nation n join supplier s |
| 17 | on |
| 18 | s.s_nationkey = n.n_nationkey and n.n_name = 'GERMANY' |
| 19 | join partsupp ps |
| 20 | on |
| 21 | ps.ps_suppkey = s.s_suppkey |
| 22 | group by ps_partkey; |
| 23 | |
| 24 | insert overwrite table q11_sum_tmp |
| 25 | select |
| 26 | sum(part_value) as total_value |
| 27 | from |
| 28 | q11_part_tmp; |
| 29 | |
| 30 | insert overwrite table q11_important_stock |
| 31 | select |
| 32 | ps_partkey, part_value as value |
| 33 | from |
| 34 | ( |
| 35 | select ps_partkey, part_value, total_value |
| 36 | from q11_part_tmp join q11_sum_tmp |
| 37 | ) a |
| 38 | where part_value > total_value * 0.0001 |
| 39 | order by value desc; |
| 40 | |
| 41 | DROP TABLE partsupp; |
| 42 | DROP TABLE supplier; |
| 43 | DROP TABLE nation; |
| 44 | DROP TABLE q11_important_stock; |
| 45 | DROP TABLE q11_part_tmp; |
| 46 | DROP TABLE q11_sum_tmp; |