SQL太长,只贴出大概结构
select ohdr.order_number
, oline.line_number || '.' || oline.shipment_number line_number
, asgn.delivery_id
,(
select
sum(wdd.requested_quantity)
from oe_order_headers_all oh,
oe_order_lines_all ol,
wsh_delivery_details wdd,
wsh_delivery_assignments wda,
wsh_new_deliveries wnd
where oh.org_id = ol.org_id
and oh.header_id = ol.header_id
and ol.line_id = wdd.source_line_id
and wdd.DELIVERY_DETAIL_ID = wda.DELIVERY_DETAIL_ID
and wda.DELIVERY_ID = wnd.DELIVERY_ID
and oh.order_number = ohdr.order_number
and ol.line_number = oline.line_number
and ol.shipment_number = oline.shipment_number
and wdd.LOT_NUMBER = ddet.LOT_NUMBER
group by oh.order_number,
ol.line_number,
ol.shipment_number,
wnd.name,
wdd.LOT_NUMBER
)qty_by_job
........
FROM wsh_delivery_details ddet
, (SELECT l.*
, NVL (m1.inventory_item_id, l.inventory_item_id) item_id
FROM oe_order_lines l
, mtl_system_items_b m1
WHERE l.customer_job = m1.segment1(+)
AND l.ship_from_org_id = m1.organization_id(+)) oline
, oe_order_headers ohdr
.............. /* 还有其它表,不一一列出,只列和子查询关联到的*/
GROUP BY ......... /* from 中用到的全部字段*/
对REQUESTED_QUANTITY求和,按结果显示在QTY_BY_JOB
以LOT_NUMBER=6069749为例,总共有两行,LINE_NUMBER分别是2.2,2.1LINE_NUMBER是2.2时,REQUESTED_QUANTITY有两行(excel的第4,5行),分别是966,34,将这两个数求和结果1000,
excel的第4,5行都显示了1000,但我希望excel的第4行才显示1000,第5行为空LINE_NUMBER是2.1时,REQUESTED_QUANTITY有两行(excel的第6,7行,分别是1888,320),将这两个数求和结果2208,
excel的第6,7行都显示了2208,但我希望excel的第6行才显示2208,第7行为空上图是查询结果,下图是期望结果
decode(row_number() over(partition by lot_number order by requested_quantity desc), 1, qty_by_job, null)