总结分区

时间:2012-08-31 16:47:09

标签: sql

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

1 个答案:

答案 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的分析功能。