blob: bfa3743e5803a1c90def276b5dd0df7f10a4a856 [file] [log] [blame]
buyingyic73348c2012-11-02 00:31:31 +00001DROP TABLE partsupp;
2DROP TABLE supplier;
3DROP TABLE nation;
4DROP TABLE q11_important_stock;
5DROP TABLE q11_part_tmp;
6DROP TABLE q11_sum_tmp;
7
8-- create tables and load data
9create 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/10/supplier';
10create 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/10/nation';
11create 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/10/partsupp';
12
13-- create the target table
14create table q11_important_stock(ps_partkey INT, value DOUBLE);
15create table q11_part_tmp(ps_partkey int, part_value double);
16create table q11_sum_tmp(total_value double);
17
18-- the query
19insert overwrite table q11_part_tmp
20select
21 ps_partkey, sum(ps_supplycost * ps_availqty) as part_value
22from
23 nation n join supplier s
24 on
25 s.s_nationkey = n.n_nationkey and n.n_name = 'GERMANY'
26 join partsupp ps
27 on
28 ps.ps_suppkey = s.s_suppkey
29group by ps_partkey;
30
31insert overwrite table q11_sum_tmp
32select
33 sum(part_value) as total_value
34from
35 q11_part_tmp;
36
37insert overwrite table q11_important_stock
38select
39 ps_partkey, part_value as value
40from
41 (
42 select ps_partkey, part_value, total_value
43 from q11_part_tmp join q11_sum_tmp
44 ) a
45where part_value > total_value * 0.0001
46order by value desc;
47