buyingyi | c73348c | 2012-11-02 00:31:31 +0000 | [diff] [blame] | 1 | -- create the tables and load the data |
| 2 | 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'; |
| 3 | 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'; |
| 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 | 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'; |
| 6 | create external table region (R_REGIONKEY INT, R_NAME STRING, R_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/region'; |
| 7 | |
| 8 | -- create result tables |
| 9 | create table q2_minimum_cost_supplier_tmp1 (s_acctbal double, s_name string, n_name string, p_partkey int, ps_supplycost double, p_mfgr string, s_address string, s_phone string, s_comment string); |
| 10 | create table q2_minimum_cost_supplier_tmp2 (p_partkey int, ps_min_supplycost double); |
| 11 | create table q2_minimum_cost_supplier (s_acctbal double, s_name string, n_name string, p_partkey int, p_mfgr string, s_address string, s_phone string, s_comment string); |
| 12 | |
| 13 | -- the query |
| 14 | insert overwrite table q2_minimum_cost_supplier_tmp1 |
| 15 | select |
| 16 | s.s_acctbal, s.s_name, n.n_name, p.p_partkey, ps.ps_supplycost, p.p_mfgr, s.s_address, s.s_phone, s.s_comment |
| 17 | from |
| 18 | nation n join region r |
| 19 | on |
| 20 | n.n_regionkey = r.r_regionkey and r.r_name = 'EUROPE' |
| 21 | join supplier s |
| 22 | on |
| 23 | s.s_nationkey = n.n_nationkey |
| 24 | join partsupp ps |
| 25 | on |
| 26 | s.s_suppkey = ps.ps_suppkey |
| 27 | join part p |
| 28 | on |
| 29 | p.p_partkey = ps.ps_partkey and p.p_size = 15 and p.p_type like '%BRASS' ; |
| 30 | |
| 31 | insert overwrite table q2_minimum_cost_supplier_tmp2 |
| 32 | select |
| 33 | p_partkey, min(ps_supplycost) |
| 34 | from |
| 35 | q2_minimum_cost_supplier_tmp1 |
| 36 | group by p_partkey; |
| 37 | |
| 38 | insert overwrite table q2_minimum_cost_supplier |
| 39 | select |
| 40 | t1.s_acctbal, t1.s_name, t1.n_name, t1.p_partkey, t1.p_mfgr, t1.s_address, t1.s_phone, t1.s_comment |
| 41 | from |
| 42 | q2_minimum_cost_supplier_tmp1 t1 join q2_minimum_cost_supplier_tmp2 t2 |
| 43 | on |
| 44 | t1.p_partkey = t2.p_partkey and t1.ps_supplycost=t2.ps_min_supplycost |
| 45 | order by s_acctbal desc, n_name, s_name, p_partkey |
| 46 | limit 100; |
| 47 | |
| 48 | DROP TABLE part; |
| 49 | DROP TABLE supplier; |
| 50 | DROP TABLE partsupp; |
| 51 | DROP TABLE nation; |
| 52 | DROP TABLE region; |
| 53 | DROP TABLE q2_minimum_cost_supplier; |
| 54 | DROP TABLE q2_minimum_cost_supplier_tmp1; |
| 55 | DROP TABLE q2_minimum_cost_supplier_tmp2; |