我必须在我们的数据库中运行一个sql命令,我无法想出这个。这是我们需要的:
“显示多次向我们出售商品的供应商的名称。列出供应商,商品和订购的次数。假设供应商在12月1日销售8件商品240,在12月销售商品240件商品12件30;这计为项240的两个总订单(不是20)。“
这是我到目前为止所拥有的。我知道这是错误的,但我不知道如何做对。
select v.VENDORID, poi.PARTNUM, count (po.PONUM)
from vendor v, purchaseorder po, poitems poi
where v.VENDORID=po.VENDORID and po.PONUM=poi.PONUM
任何帮助将不胜感激!非常感谢!
答案 0 :(得分:2)
您在查询中缺少HAVING和GROUP BY。查询应该类似于:
SELECT v.name, poi.PARTNUM, count (po.PONUM)
FROM vendor v
LEFT JOIN purchaseorder po ON (v.vendorid=po.vendorid)
LEFT JOIN poitems poi ON (po.PONUM=poi.PONUM)
WHERE poi.PARTNUM IS NOT NULL
GROUP BY (v.vendorID, poi.partnum)
HAVING COUNT(po.ponum) > 1
答案 1 :(得分:0)
希望此查询适合您:
Select name
,description
,count(quantity)
from parts p
,(Select a.vendorid,name,ponum from purchaseorders a,
(Select vendorid,count(*) from purchaseorders
--All vendors who have purchased more then once
group by vendorid having count(vendorid)>1) b
,vendor v
where a.vendorid=b.vendorid
and v.vendorid=a.vendorid) o
,(select vendorid,o.ponum,partnum,count(partnum) quantity
--the quantity of orders placed
from poitems p ,purchaseorders o
where o.ponum=p.ponum
group by vendorid,partnum,o.ponum) i
where i.vendorid=o.vendorid
and p.partnum=i.partnum
and i.ponum=o.ponum
group by name,description