0

以下查询为 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 
4

1 回答 1

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.statuswill be中没有匹配的顺序NULL,因此不等于'transaction authorised'

此外,您不需要 group by。您已经拥有为您执行此操作的分析功能SUM

于 2012-08-31T16:49:25.950 回答