buyingyi | c73348c | 2012-11-02 00:31:31 +0000 | [diff] [blame^] | 1 | DROP TABLE customer; |
| 2 | DROP TABLE orders; |
| 3 | DROP TABLE q13_customer_distribution; |
| 4 | |
| 5 | -- create the tables and load the data |
| 6 | 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/10/customer'; |
| 7 | 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/10/orders'; |
| 8 | |
| 9 | -- create the result table |
| 10 | create table q13_customer_distribution (c_count int, custdist int); |
| 11 | |
| 12 | -- the query |
| 13 | insert overwrite table q13_customer_distribution |
| 14 | select |
| 15 | c_count, count(1) as custdist |
| 16 | from |
| 17 | (select |
| 18 | c_custkey, count(o_orderkey) as c_count |
| 19 | from |
| 20 | customer c left outer join orders o |
| 21 | on |
| 22 | c.c_custkey = o.o_custkey and not o.o_comment like '%special%requests%' |
| 23 | group by c_custkey |
| 24 | ) c_orders |
| 25 | group by c_count |
| 26 | order by custdist desc, c_count desc; |
| 27 | |