好的,我有以下代码
SELECT PO_NUMBER, MIN(PO_QUANTITY) AS PO_QUANTITY, SUM(COALESCE(SHIPPED_WT, 0)) AS SHIPPED_WT
FROM VW_TRAFFIC_PO_SIDE
WHERE SYSDATE > PO_SHIPMENT_TO
GROUP BY PO_NUMBER
HAVING MIN(PO_QUANTITY) > SUM(COALESCE(SHIPPED_WT, 0))
这在获取正确的数据方面做了我想要的一切,但是我想要更多的列,类似于
SELECT PO_NUMBER, SO_ORIGIN, SO_PRODUCT, SUPPLIER, MIN(PO_QUANTITY) AS PO_QUANTITY, SUM(COALESCE(SHIPPED_WT, 0)) AS SHIPPED_WT, PO_SHIPMENT_FROM, PO_SHIPMENT_TO
FROM VW_TRAFFIC_PO_SIDE
WHERE SYSDATE > PO_SHIPMENT_TO
GROUP BY PO_NUMBER
HAVING MIN(PO_QUANTITY) > SUM(COALESCE(SHIPPED_WT, 0))
但是我不能按其他列分组,因为它会抓取我不想要的数据。
有任何想法吗?
我意识到可以使用 Distinct,但是我希望我的数据显示的方式,它会消除我不想消除的记录
PS,我使用 SQL 服务器,我无法创建/修改表,我使用的所有数据都基于视图,我也无法更改它
编辑:我提供了一个答案,但不优雅
PO PO Qty Tracking No Shipped Qty
PO 1 100 1 60
PO 1 100 1 60
PO 1 100 1 60
PO 1 100 2 15
PO 1 100 2 15
PO 2 50 2 20
PO 2 50 2 20
PO 2 50 3 30
PO 2 50 3 30
IN the above scenario, totals are
PO 1 PO 2
PO Qty 100 50
Shipped 75 50
Unshipped 25 0
- In each PO record, the PO qty is stated in full on each line and therefore is repeated each time the same PO is listed as a record
- Within a tracking no for the PO, the shipped quantity is stated in full and therefore is repeated each time the same tracking no (for the same PO #) is listed as a record
- So a tracking number can exist on two different PO’s (as in Tracking no 2 above) in which case the value is counted once for each PO