简体   繁体   中英

sum over partition

The following query bring the correct value for totalqty. however it leaves or misses counting the qty for some items, why?

        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 

Your where clause makes the left outer join behave like an inner join . Change it like this:

WHERE
    prodId = prodId and
    (
        o.order_id is null or 
        (
            o.status = 'transaction authorised' AND
            o.delTime = '#FORM.delDateselect#'
        )
    )

The reason is that if there is no matching order in orders o.status will be NULL and thus not equal to 'transaction authorised' .

Furthermore, you don't need the group by. You already have the analytical function that does the SUM for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM