tcp-h数据&postgresql查询(数据库学习之二)

首先要了解好tcp-h各个表之间的关系,才能更好的查询我们想要的信息。
这里附上关系图的链接
tcp-h数据&postgresql查询(数据库学习之二)_第1张图片

理清所有表各自的联系,我就为大家翻译一遍吧(只能是大概意思)
region 地区、范围的意思
nation 国家
supplier 供应商
customer顾客
orders 订单
lineitem 订单详情联系
part 产品
partsupp产品供应商
ship 运输

Q1:Display the key and quantity of parts which are shipped on April 1st, 1992.

SELECT S.l_quantity, S.l_partkey
FROM lineitem S
WHERE S.l_shipdate = '1992-04-01'

tcp-h数据&postgresql查询(数据库学习之二)_第2张图片

Q2: For each ship mode, find the total quantity of items that are shipped before October 1st,1992. Display the results in descending order of the total quantity.

首先可以判断选择的日期要小于 1992-10-01
接着从句子意思是要对 运输方式进行分组,并统计各种运输方式的总数量

SELECT S.l_shipmode, SUM(S.l_quantity) AS total
FROM lineitem S
WHERE S.l_shipdate < '1992-10-01'
GROUP BY S.l_shipmode
ORDER BY  total DESC;

tcp-h数据&postgresql查询(数据库学习之二)_第3张图片

Q3: Display the total number of comments which contains “carefully” in Table lineitem.

SELECT Count (*) AS total
FROM lineitem S
WHERE S.l_comment LIKE '%carefully%';

tcp-h数据&postgresql查询(数据库学习之二)_第4张图片

Q4: Display the total quantity of parts which are satisfied the following conditions in LINEITEM:
– the ship mode is RAIL
– the priority of the order that the parts belong to is URGENT

首先在orders 查找 orderpriority =‘urgent’的 orderkey
再对lineitem表查询orderkey以及运输方式

SELECT SUM(S.l_quantity) AS total
FROM lineitem S
WHERE S.l_shipmode = 'RAIL' AND S.l_orderkey IN (SELECT R.o_orderkey
                                                 FROM orders R
                                                 WHERE R.o_orderpriority = '1-URGENT');

tcp-h数据&postgresql查询(数据库学习之二)_第5张图片

Q5: Find out the suppliers that can supply the part whose key is 99 . Display their names, nation, regions and brands.

把各种表进行拼接

SELECT S.s_name, R.n_name, T.r_name, F.p_brand
FROM supplier S, nation R, region T, part F, partsupp H
WHERE S.s_suppkey = H.ps_suppkey AND S.s_nationkey = R.n_nationkey AND R.n_regionkey = T.r_regionkey 
    AND H.ps_partkey = F.p_partkey AND H.ps_partkey = '99';

tcp-h数据&postgresql查询(数据库学习之二)_第6张图片

Q6: Calculate the number of distinct customers who have ordered parts whose type is STEEL.

SELECT COUNT (DISTINCT S.c_custkey) AS total
FROM customer S, orders R,  lineitem T, part H
WHERE S.c_custkey = R.o_custkey AND R.o_orderkey = T.l_orderkey AND T.l_partkey = H.p_partkey AND H.p_type LIKE '%STEEL%';

tcp-h数据&postgresql查询(数据库学习之二)_第7张图片

Q7: Find the top 10 parts, in terms of their total quantity shipped in 1996. List the parts’ keys,names, brands and total quality shipped in 1996. Display the results in ascending order of the total quantity.
这道题比较坑
首先要找出运输数量排前十的商品
那么就要按递减排序创建出前10商品的表
找出之后再进行升序排序

SELECT S.p_partkey, S.p_name, S.p_brand, T.total
FROM part S, (SELECT R.l_partkey as partkey, SUM(R.l_quantity) AS total
             FROM lineitem R 
             WHERE R.l_shipdate > '1995-12-31' AND R.l_shipdate < '1997-01-01'
             GROUP BY R.l_partkey
             ORDER BY  total DESC
             LIMIT 10
             ) AS T
WHERE S.p_partkey = T.partkey
ORDER BY  total ASC;

tcp-h数据&postgresql查询(数据库学习之二)_第8张图片

Q8: Find out the customers who had at least ordered 10 times in 1998. Display their keys, names, addresses, phones and the total of their orders in 1998. List the results in descending order of the total orders.

SELECT S.c_custkey, S.c_name, S.c_address, S.c_phone, T.total
FROM customer S, (SELECT R.o_custkey as custkey, COUNT(*) AS total
             FROM orders R
             WHERE R.o_orderdate > '1997-12-31' AND R.o_orderdate < '1999-01-01'
             GROUP BY R.o_custkey
             HAVING COUNT(*)>= 10
             ) AS T
WHERE S.c_custkey = T.custkey;

tcp-h数据&postgresql查询(数据库学习之二)_第9张图片

Q9: Find out the lineitem which has the max extended price with a 10% discount and is ordered in 1998. Display the value of this max extended price.

SELECT MAX(S.l_extendedprice) AS maxprice
FROM lineitem S
WHERE S.l_discount = '0.10' AND S.l_orderkey In (SELECT R.o_orderkey
                       FROM orders R
                       WHERE R.o_orderdate > '1997-12-31' AND R.o_orderdate < '1999-01-01');

tcp-h数据&postgresql查询(数据库学习之二)_第10张图片

Q10: Display total quantity of lineitems which are ordered in May 1995 for each ship mode except air and ship.

SELECT S.l_shipmode, SUM(S.l_quantity) AS total
FROM lineitem S
WHERE S.l_shipmode != 'AIR' AND S.l_shipmode != 'SHIP' AND S.l_orderkey In (SELECT R.o_orderkey
                       FROM orders R
                       WHERE R.o_orderdate > '1995-04-30' AND R.o_orderdate < '1995-06-01')
GROUP BY S.l_shipmode;

tcp-h数据&postgresql查询(数据库学习之二)_第11张图片

你可能感兴趣的:(数据库)