2

我正在尝试从单个查询中获取结果以显示数量的总和,即使未找到行也是如此。

这个查询正是我想要的:

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

并返回这个

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

这将显示没有数量的行的零就好了。当我添加缩小数据范围所需的其他条件时,就会出现问题。我需要与此查询中的附加条件相同的结果

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

该查询虽然返回此结果。

MAX(M.ITEM_NUMBER) MAX(M.ITEM_DESCRIPTION)     MAX(M.UOM) "SUM(NVL(I.QTY_ORDERED,0))"
199                Watermelons                 EACH       31

应该显示零的行现在消失了。使用附加条件时,是否可以显示第一个结果中的所有行?

谢谢!

4

1 回答 1

1

尝试

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     nvl(h.request_state,'Complete') <> 'Incomplete'
group by m.item_number
order by 1

当您在 h.request_state 上添加新条件时,您必须考虑可能的 NULL 数据,添加 NVL 会有所帮助。

hth

于 2012-11-01T19:59:09.730 回答