blob: 271b614f2f7500ee1dd15df8ef558e2886822ce8 [file] [log] [blame]
buyingyi55df5212013-03-24 07:20:08 +00001-- create tables and load data
2create 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';
3create 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';
4create 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
7create table q11_important_stock(ps_partkey INT, value DOUBLE);
8create table q11_part_tmp(ps_partkey int, part_value double);
9create table q11_sum_tmp(total_value double);
10
11-- the query
12insert overwrite table q11_part_tmp
13select
14 ps_partkey, sum(ps_supplycost * ps_availqty) as part_value
15from
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
22group by ps_partkey;
23
24insert overwrite table q11_sum_tmp
25select
26 sum(part_value) as total_value
27from
28 q11_part_tmp;
29
30insert overwrite table q11_important_stock
31select
32 ps_partkey, part_value as value
33from
34 (
35 select ps_partkey, part_value, total_value
36 from q11_part_tmp join q11_sum_tmp
37 ) a
38where part_value > total_value * 0.0001
39order by value desc;
40
41DROP TABLE partsupp;
42DROP TABLE supplier;
43DROP TABLE nation;
44DROP TABLE q11_important_stock;
45DROP TABLE q11_part_tmp;
46DROP TABLE q11_sum_tmp;