buyingyi | c73348c | 2012-11-02 00:31:31 +0000 | [diff] [blame] | 1 | -- create tables and load data |
| 2 | create external table customer (C_CUSTKEY INT, C_NAME STRING, C_ADDRESS STRING, C_NATIONKEY INT, C_PHONE STRING, C_ACCTBAL DOUBLE, C_MKTSEGMENT STRING, C_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/customer'; |
| 3 | create external table orders (O_ORDERKEY INT, O_CUSTKEY INT, O_ORDERSTATUS STRING, O_TOTALPRICE DOUBLE, O_ORDERDATE STRING, O_ORDERPRIORITY STRING, O_CLERK STRING, O_SHIPPRIORITY INT, O_COMMENT STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE LOCATION '/tpch/100/orders'; |
| 4 | |
| 5 | -- create target tables |
| 6 | create table q22_customer_tmp(c_acctbal double, c_custkey int, cntrycode string); |
| 7 | create table q22_customer_tmp1(avg_acctbal double); |
| 8 | create table q22_orders_tmp(o_custkey int); |
| 9 | create table q22_global_sales_opportunity(cntrycode string, numcust int, totacctbal double); |
| 10 | |
| 11 | -- the query |
| 12 | insert overwrite table q22_customer_tmp |
| 13 | select |
| 14 | c_acctbal, c_custkey, substr(c_phone, 1, 2) as cntrycode |
| 15 | from |
| 16 | customer |
| 17 | where |
| 18 | substr(c_phone, 1, 2) = '13' or |
| 19 | substr(c_phone, 1, 2) = '31' or |
| 20 | substr(c_phone, 1, 2) = '23' or |
| 21 | substr(c_phone, 1, 2) = '29' or |
| 22 | substr(c_phone, 1, 2) = '30' or |
| 23 | substr(c_phone, 1, 2) = '18' or |
| 24 | substr(c_phone, 1, 2) = '17'; |
| 25 | |
| 26 | insert overwrite table q22_customer_tmp1 |
| 27 | select |
| 28 | avg(c_acctbal) |
| 29 | from |
| 30 | q22_customer_tmp |
| 31 | where |
| 32 | c_acctbal > 0.00; |
| 33 | |
| 34 | insert overwrite table q22_orders_tmp |
| 35 | select |
| 36 | o_custkey |
| 37 | from |
| 38 | orders |
| 39 | group by |
| 40 | o_custkey; |
| 41 | |
| 42 | insert overwrite table q22_global_sales_opportunity |
| 43 | select |
| 44 | cntrycode, count(1) as numcust, sum(c_acctbal) as totacctbal |
| 45 | from |
| 46 | ( |
| 47 | select cntrycode, c_acctbal, avg_acctbal from |
| 48 | q22_customer_tmp1 ct1 join |
| 49 | ( |
| 50 | select cntrycode, c_acctbal from |
| 51 | q22_orders_tmp ot |
| 52 | right outer join q22_customer_tmp ct |
| 53 | on |
| 54 | ct.c_custkey = ot.o_custkey |
| 55 | where |
| 56 | o_custkey is null |
| 57 | ) ct2 |
| 58 | ) a |
| 59 | where |
| 60 | c_acctbal > avg_acctbal |
| 61 | group by cntrycode |
| 62 | order by cntrycode; |
| 63 | |
| 64 | DROP TABLE customer; |
| 65 | DROP TABLE orders; |
| 66 | DROP TABLE q22_customer_tmp; |
| 67 | DROP TABLE q22_customer_tmp1; |
| 68 | DROP TABLE q22_orders_tmp; |
| 69 | DROP TABLE q22_global_sales_opportunity; |