好的,我有以下代码
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服务器,我无法创建/修改表,我使用的所有数据都是基于View的,我不能改变它编辑:我提供了一个答案,无论多么优雅
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
答案 0 :(得分:0)
A JOIN怎么样?
SELECT DISTINCT X.PO_NUMBER, X.PO_QUANTITY, X.SHIPPED_WT,
Y.SO_ORIGIN, Y.SO_PRODUCT, Y.SUPPLIER,
Y.PO_SHIPMENT_FROM, Y.PO_SHIPMENT_TO
FROM (
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))) X
LEFT JOIN VW_TRAFFIC_PO_SIDE Y
ON X.PO_NUMBER = Y.PO_NUMBER
;
以下是基于旧数据的演示...它没有您需要的所有行,但它也应该通过添加其他行来工作。
答案 1 :(得分:0)
请回答:当您GROUP BY PO_NUMBER
时,一个群组中的所有项目都具有相同的SO_ORIGIN, SO_PRODUCT, SUPPLIER, PO_SHIPMENT_FROM, PO_SHIPMENT_TO
?
如果您的答案是肯定的,那么您可以将查询更改为:
GROUP BY PO_NUMBER, SO_ORIGIN, SO_PRODUCT, SUPPLIER, PO_SHIPMENT_FROM, PO_SHIPMENT_TO
如果您的答案为否,那么您的查询在逻辑上是错误的。
答案 2 :(得分:0)
SELECT PO_NUMBER, PO_QUANTITY, SHIPPED_WT, PO_SHIPMENT_TO, SO_ORIGIN, SO_PRODUCT, SUPPLIER, PO_QUANTITY-SHIPPED_WT UNSHIPPED
FROM (
SELECT DISTINCT PO_NUMBER, PO_QUANTITY, PO_SHIPMENT_TO, SO_ORIGIN, SO_PRODUCT, SUPPLIER, SUM(NVL(SHIPPED_WT,0)) OVER (PARTITION BY PO_NUMBER) AS SHIPPED_WT
FROM(
SELECT DISTINCT PO_NUMBER, PO_TRACKING_NO, PO_QUANTITY, SHIPPED_WT, PO_SHIPMENT_TO, SO_PRODUCT, SO_ORIGIN, SUPPLIER FROM VW_TRAFFIC_PO_SIDE))
WHERE PO_QUANTITY > SHIPPED_WT AND SYSDATE > PO_SHIPMENT_TO
经过很多时间和艰苦的工作,我的公司终于可以分配第二个人来帮助我(谁知道更多关于sql然后我做了)让这个东西工作......然而放入修饰符会很棘手而且相当烦恼..有点低效