以下查询为totalqty带来正确的值。然而,它为某些项目留下或错过了数量,为什么?
SELECT
PRODID, ITEMDES, QTY, SUM(QTY) over (partition by prodId) as totalqty, StockCode,shipName, shipCompany, shipAddress1, shipAddress2, shipAddress3,shipPostCode,shipcity,shipCountry,shipCounty,customerMessage
FROM orderedItems oi
left join orders o on oi.order_id = o.order_id
WHERE prodId = prodId
AND o.status = 'transaction authorised'
AND o.delTime = '#FORM.delDateselect#'
Group by PRODID,ITEMDES,QTY, StockCode,shipName, shipCompany, shipAddress1, shipAddress2, shipAddress3,shipPostCode,shipcity,shipCountry,shipCounty,customerMessage
ORDER BY PRODID
答案 0 :(得分:2)
您的where
条款使left outer join
的行为与inner join
相似。改变它:
WHERE
prodId = prodId and
(
o.order_id is null or
(
o.status = 'transaction authorised' AND
o.delTime = '#FORM.delDateselect#'
)
)
原因是,如果orders
o.status
中没有匹配的订单,则NULL
将不等于'transaction authorised'
。
此外,您不需要分组。您已拥有为您执行SUM
的分析功能。