I am trying to get a result from a single query to display the sum of a quantity even when a row is not found.
This query does exactly what I want:
select max(m.item_number), max(m.item_description),
max(m.uom), sum(nvl(i.qty_ordered,0))
from (((dex.ITEM_MASTER m FULL JOIN dex.order_request_items i ON m.item_number = i.item_number)
FULL JOIN dex.order_request h ON h.orderid = i.orderid)
FULL JOIN dex.customer c ON h.customer_number = c.customer_number)
where m.item_number in ('199', '198', '11415', '11425', '11435', '1145', '766')
group by m.item_number
order by 1
and returns this
MAX(M.ITEM_NUMBER) MAX(M.ITEM_DESCRIPTION) MAX(M.UOM) "SUM(NVL(I.QTY_ORDERED,0))"
11415 Bananas HALF CASE GREEN 1/2 0
11425 Bananas HALF CASE GREEN TIP 1/2 0
11435 Bananas HALF CASE RIPE 1/2 0
1145 Bananas HALF CASE 1/2 5
198 Watermelons SEEDLESS EACH 0
199 Watermelons EACH 80
766 Pumpkins EACH EACH 0
This displays the zeroes for rows that have no quantities just fine. The problem occurs when I add additional conditions that are needed to narrow down the data.
I need the same kind of result with the additional conditions in this query
select max(m.item_number), max(m.item_description),
max(m.uom), sum(nvl(i.qty_ordered,0))
from (((dex.ITEM_MASTER m FULL JOIN dex.order_request_items i ON m.item_number = i.item_number)
FULL JOIN dex.order_request h ON h.orderid = i.orderid)
FULL JOIN dex.customer c ON h.customer_number = c.customer_number)
where m.item_number in ('199', '198', '11415', '11425', '11435', '1145', '766')
and h.REQUEST_DELIVERY_DATE = TO_DATE('10/24/2012', 'MM/DD/YYYY')
and c.subcontractorid is null
and h.request_state <> 'Incomplete'
group by m.item_number
order by 1
That query though returns this result.
MAX(M.ITEM_NUMBER) MAX(M.ITEM_DESCRIPTION) MAX(M.UOM) "SUM(NVL(I.QTY_ORDERED,0))"
199 Watermelons EACH 31
The rows that should show a zero are now gone. Is it possible to have all the rows like the first result show when using additional conditions?
Thanks!
Try
When you add new condition on h.request_state you must consider possible NULL data, adding NVL can help.
hth