Prometheus
Prometheus

Reputation: 33585

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 

Upvotes: 0

Views: 448

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174279

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.

Upvotes: 2

Related Questions