1

好的,我有以下代码

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
4

3 回答 3

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
     ;

以下是基于旧数据的演示......它没有您需要的所有行,但它也应该通过添加其他行来工作。

于 2013-01-09T05:23:04.513 回答
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

如果您的答案是否定的,那么您的查询在逻辑上是错误的。

于 2013-01-09T05:23:26.323 回答
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)来让这件事正常工作..但是添加修饰符会很棘手而且很烦人..它有点低效

于 2013-01-09T08:11:25.967 回答