|
mysql> select
-> c_count,
-> count(*) as custdist
-> from
-> (
-> select
-> c_custkey,
-> count(o_orderkey)c_count
-> from
-> customer left outer join orders on
-> c_custkey = o_custkey
-> and o_comment not like '%unusual%packages%'
-> group by
-> c_custkey
-> ) c_orders
-> group by
-> c_count
-> order by
-> custdist desc,
-> c_count desc limit 10;
+---------+----------+
| c_count | custdist |
+---------+----------+
| 0 | 500023 |
| 10 | 65974 |
| 9 | 65196 |
| 11 | 62247 |
| 8 | 58373 |
| 12 | 55809 |
| 13 | 49853 |
| 7 | 46762 |
| 19 | 46735 |
| 18 | 46216 |
+---------+----------+
10 rows in set (1 min 7.73 sec)
mysql> -- 22
mysql> select
-> cntrycode,
-> count(*) as numcust,
-> sum(c_acctbal) as totacctbal
-> from
-> (
-> select
-> substr(c_phone ,1 ,2) as cntrycode,
-> c_acctbal
-> from
-> customer
-> where
-> substr(c_phone ,1 ,2) in
-> ('23', '39', '40', '27', '33', '31', '25')
-> and c_acctbal > (
-> select
-> avg(c_acctbal)
-> from
-> customer
-> where
-> c_acctbal > 0.00
-> and substr(c_phone ,1 ,2) in
-> ('23', '39', '40', '27', '33', '31', '25')
-> )
-> and not exists (
-> select
-> *
-> from
-> orders
-> where
-> o_custkey = c_custkey
-> )
-> ) custsale
-> group by
-> cntrycode
-> order by
-> cntrycode limit 10;
+-----------+---------+-------------+
| cntrycode | numcust | totacctbal |
+-----------+---------+-------------+
| 23 | 8988 | 67627762.48 |
| 25 | 9111 | 68360817.05 |
| 27 | 9014 | 67640913.65 |
| 31 | 9087 | 68149522.82 |
| 33 | 9182 | 68895118.67 |
+-----------+---------+-------------+
5 rows in set (3.04 sec)
mysql> -- 21
mysql> select
-> s_name,
-> count(*) as numwait
-> from
-> supplier,
-> lineitem l1,
-> orders,
-> nation
-> where
-> s_suppkey = l1.l_suppkey
-> and o_orderkey = l1.l_orderkey
-> and o_orderstatus = 'F'
-> and l1.l_receiptdate > l1.l_commitdate
-> and exists (
-> select
-> *
-> from
-> lineitem l2
-> where
-> l2.l_orderkey = l1.l_orderkey
-> and l2.l_suppkey <> l1.l_suppkey
-> )
-> and not exists (
-> select
-> *
-> from
-> lineitem l3
-> where
-> l3.l_orderkey = l1.l_orderkey
-> and l3.l_suppkey <> l1.l_suppkey
-> and l3.l_receiptdate > l3.l_commitdate
-> )
-> and s_nationkey = n_nationkey
-> and n_name = 'GERMANY'
-> group by
-> s_name
-> order by
-> numwait desc,
-> s_name limit 10;
+--------------------+---------+
| s_name | numwait |
+--------------------+---------+
| Supplier#000007962 | 22 |
| Supplier#000033421 | 22 |
| Supplier#000018276 | 21 |
| Supplier#000028758 | 21 |
| Supplier#000090945 | 21 |
| Supplier#000014045 | 20 |
| Supplier#000014173 | 20 |
| Supplier#000017352 | 20 |
| Supplier#000025900 | 20 |
| Supplier#000032900 | 20 |
+--------------------+---------+
10 rows in set (3 min 21.40 sec)
mysql> -- 19
select
mysql> select
-> sum(l_extendedprice* (1 - l_discount)) as revenue
-> from
-> lineitem,
-> part
-> where
-> (
-> p_partkey = l_partkey
-> and p_brand = 'Brand#31'
-> and p_container in ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG')
-> and l_quantity >= 3 and l_quantity <= 3 + 10
-> and p_size between 1 and 5
-> and l_shipmode in ('AIR', 'AIR REG')
-> and l_shipinstruct = 'DELIVER IN PERSON'
-> )
-> or
-> (
-> p_partkey = l_partkey
-> and p_brand = 'Brand#21'
-> and p_container in ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK')
-> and l_quantity >= 15 and l_quantity <= 15 + 10
-> and p_size between 1 and 10
-> and l_shipmode in ('AIR', 'AIR REG')
-> and l_shipinstruct = 'DELIVER IN PERSON'
-> )
-> or
-> (
-> p_partkey = l_partkey
-> and p_brand = 'Brand#45'
-> and p_container in ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG')
-> and l_quantity >= 23 and l_quantity <= 23 + 10
-> and p_size between 1 and 15
-> and l_shipmode in ('AIR', 'AIR REG')
-> and l_shipinstruct = 'DELIVER IN PERSON'
-> ) limit 10;
+---------------+
| revenue |
+---------------+
| 35382266.2072 |
+---------------+
1 row in set (59.92 sec)
mysql> -- 18 c
mysql> select
-> c_name,
-> c_custkey,
-> o_orderkey,
-> o_orderdate,
-> o_totalprice,
-> sum(l_quantity)
-> from
-> customer,
-> orders,
-> lineitem l,
-> (
-> select
-> l_orderkey
-> from
-> lineitem
-> group by
-> l_orderkey having
-> sum(l_quantity) > 313
-> )l1
-> where
-> o_orderkey =l1.l_orderkey
-> and c_custkey = o_custkey
-> and o_orderkey = l.l_orderkey
-> group by
-> c_name,
-> c_custkey,
-> o_orderkey,
-> o_orderdate,
-> o_totalprice
-> order by
-> o_totalprice desc,
-> o_orderdate limit 10;
+--------------------+-----------+------------+-------------+--------------+-----------------+
| c_name | c_custkey | o_orderkey | o_orderdate | o_totalprice | sum(l_quantity) |
+--------------------+-----------+------------+-------------+--------------+-----------------+
| Customer#001287812 | 1287812 | 42290181 | 1997-11-26 | 558289.17 | 318.00 |
| Customer#001172513 | 1172513 | 36667107 | 1997-06-06 | 550142.18 | 322.00 |
| Customer#000571654 | 571654 | 21213895 | 1992-01-03 | 549380.08 | 327.00 |
| Customer#000667882 | 667882 | 2199712 | 1996-09-30 | 542154.01 | 327.00 |
| Customer#001471966 | 1471966 | 1263015 | 1997-02-02 | 540476.80 | 320.00 |
| Customer#001101754 | 1101754 | 46794464 | 1992-04-28 | 532623.90 | 320.00 |
| Customer#000591466 | 591466 | 55799200 | 1996-02-11 | 524209.45 | 327.00 |
| Customer#001196317 | 1196317 | 34509573 | 1995-10-15 | 522897.01 | 318.00 |
| Customer#000634048 | 634048 | 36327201 | 1992-04-25 | 519634.30 | 315.00 |
| Customer#001308376 | 1308376 | 28077922 | 1996-10-27 | 518711.99 | 319.00 |
+--------------------+-----------+------------+-------------+--------------+-----------------+
10 rows in set (33.39 sec)
mysql>
mysql> -- 21 fix
mysql> select s_name ,count(*) as numwait from
-> (select
-> s_name,l_suppkey ,l_orderkey
-> -- count(*) as numwait
-> from
-> supplier,
-> lineitem l1,
-> orders,
-> nation
-> where
-> s_suppkey = l1.l_suppkey
-> and o_orderkey = l1.l_orderkey
-> and o_orderstatus = 'F'
-> and l1.l_receiptdate > l1.l_commitdate
-> and s_nationkey = n_nationkey
-> and n_name = 'GERMANY'
-> ) l1
-> where exists (
-> select
-> *
-> from
-> lineitem l2
-> where
-> l2.l_orderkey = l1.l_orderkey
-> and l2.l_suppkey <> l1.l_suppkey
-> )
-> and not exists (
-> select
-> *
-> from
-> lineitem l3
-> where
-> l3.l_orderkey = l1.l_orderkey
-> and l3.l_suppkey <> l1.l_suppkey
-> and l3.l_receiptdate > l3.l_commitdate
-> )
-> group by
-> s_name
-> order by
-> numwait desc,
-> s_name
-> limit 10;
+--------------------+---------+
| s_name | numwait |
+--------------------+---------+
| Supplier#000007962 | 22 |
| Supplier#000033421 | 22 |
| Supplier#000018276 | 21 |
| Supplier#000028758 | 21 |
| Supplier#000090945 | 21 |
| Supplier#000014045 | 20 |
| Supplier#000014173 | 20 |
| Supplier#000017352 | 20 |
| Supplier#000025900 | 20 |
| Supplier#000032900 | 20 |
+--------------------+---------+
10 rows in set (1 min 44.05 sec)
mysql> -- 20 a
mysql> select s_name, s_address
-> from supplier, nation
-> where s_suppkey in (select ps_suppkey
-> from partsupp
-> ,(select l_partkey,l_suppkey, sum(l_quantity) l_quantity_SUM
-> from lineitem,part
-> where l_partkey = p_partkey and p_name like 'bisque%'
-> and l_shipdate >= date '1994-01-01'
-> and l_shipdate < date '1994-01-01' + interval '1' year
-> GROUP BY l_partkey,l_suppkey
-> )a
-> where l_partkey = ps_partkey
-> and l_suppkey = ps_suppkey
-> and ps_availqty > 0.5*l_quantity_SUM
-> )
-> and s_nationkey = n_nationkey
-> and n_name = 'CANADA'
-> order by s_name
-> limit 10; |
|