SQL问题 - 中等难度

时间:2011-01-25 11:53:54

标签: sql

我正在通过此链接工作,我在本网站的Q7和Q8上停留:http://sqlzoo.net/a3m.htm

我对Q7的尝试是:

SELECT tprod.dscr, sum(qnty), max(tpurcd.recv)
FROM tprod, tpurcd
WHERE tprod.code = tpurcd.prod
GROUP BY tprod.dscr, tpurcd.recv

Q8尝试:

SELECT tpurcd.cust, tpurcd.recv, qnty
FROM tpurcd, tprod
WHERE tprod.code = tpurcd.prod
AND tpurcd.qnty * tprod.pric

表信息列于此处:http://sqlzoo.net/a3.htm

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

0.7。对于每个产品,显示说明和订购的总数以及最近订单的日期。

(您不应按tpurcd.recv分组)

SELECT tprod.dscr, sum(qnty), max(tpurcd.recv)
FROM tprod
INNER JOIN tpurcd ON tprod.code = tpurcd.prod
GROUP BY tprod.dscr

0.8。显示所有采购订单的客户代码,收货日期和总价值,总价值至少为475英镑。

(表达式tpurcd.qnty * tprod.pric悬挂)

SELECT tpurcd.cust, tpurcd.recv, sum(tpurcd.qnty * tprod.pric)
FROM tpurcd
INNER JOIN tprod ON tprod.code = tpurcd.prod
GROUP BY tpurcd.cust, tpurcd.recv
HAVING sum(tpurcd.qnty * tprod.pric) >= 475

答案 1 :(得分:0)

使用mysql

7

SELECT tprod.dscr as Description, sum(tpurcd.qnty) as Quantity, max(tpurcd.recv) as 'Most Recent Order'
FROM tprod
INNER JOIN tpurcd ON tprod.code = tpurcd.prod
GROUP BY tprod.dscr

8

SELECT tpurcd.cust as 'Customer Code', tpurcd.recv as 'Received Date', sum(tpurcd.qnty * tprod.pric) as 'Total Value'
FROM tprod
INNER JOIN tpurcd ON tprod.code = tpurcd.prod 
group by tpurcd.cust, tpurcd.recv
having sum(tpurcd.qnty * tprod.pric) > 475