SQL Server不等于查询返回相等的值

时间:2018-02-28 01:33:04

标签: sql sql-server duplicates aggregation

我在下面有一个查询,它比较两个相关的数据集,寻找一组中qty的总和与另一组不匹配的行。作为参考,一个数据集是基于PO的(MRP),一个是基于SO的(ARUN),但是它们在PO和SO编号以及材料,销售订单项等上链接。查询运行正常,但结果显示相同我所瞄准的价值观是在他们不相等的地方呼唤。我将补充说,不使用SUM的正常输出会返回两倍的行,因为每个数据集的总数分为两行(货物分开,所以有不同的容器号)。我的目标是通过使用SUM函数来摆脱它,并排除之前聚合的唯一字段。可能发生的任何想法?我已经包含了代码以及SUM和之前的结果。所以在ARUN中可以有一个唯一的集装箱号码,同样在MRP中可以有一个独特的计划行

代码:

SELECT 
       M.STOCK_NUMBER AS [PO_NUMBER],
       M.MATERIAL, 
       M.MATERIAL_DESCRIPTION, 
       M.SIZE_LITERAL,
       A.GRID_VALUE,
       SUM(M.QUANTITY) AS [PO_QTY],
       SUM(A.QUANTITY) AS [SO_QTY],
       M.SALES_ORDER_NUMBER, 
       M.SALES_ORDER_ITEM_NUMBER, 
       M.CUSTOMER_NAME, 
       M.PLANNED_RECEIPT_DATE AS [ETA], 
       M.PLANT_CODE, 
       M.STOCK_TYPE



FROM   VW_MRP_ALLOCATION M
JOIN   VW_ARUN_NORM_NEW A   
ON     M.SALES_ORDER_NUMBER = A.SALES_ORDER_NUMBER
AND    M.SALES_ORDER_ITEM_NUMBER = A.SALES_ORDER_ITEM_NUMBER
AND    M.STOCK_NUMBER = A.SAP_PO_NUMBER
AND    M.SIZE_LITERAL = A.GRID_VALUE
AND    M.STOCK_TYPE = A.STOCK_TYPE
AND    M.MATERIAL = A.MATERIAL_NUMBER

WHERE  M.REQUIREMENT_TYPE = 'KE'
AND    M.STOCK_TYPE NOT IN ('A','C')
AND    M.STOCK_NUMBER IS NOT NULL
AND    M.QUANTITY <> A.QUANTITY

GROUP BY M.STOCK_NUMBER,
       M.MATERIAL, 
       M.MATERIAL_DESCRIPTION, 
       M.SIZE_LITERAL, 
       M.SALES_ORDER_NUMBER, 
       M.SALES_ORDER_ITEM_NUMBER, 
       M.CUSTOMER_NAME, 
       M.PLANNED_RECEIPT_DATE,
       M.PLANT_CODE, 
       M.STOCK_TYPE,
       A.GRID_VALUE

ORDER BY  M.STOCK_NUMBER,
          M.SIZE_LITERAL ASC

此代码导致以下(行样本):

PO_NUMBER    MATERIAL           MATERIAL_DESCRIPTION                     SIZE_LITERAL GRID_VALUE PO_QTY                                  SO_QTY                                  SALES_ORDER_NUMBER SALES_ORDER_ITEM_NUMBER                 CUSTOMER_NAME                       ETA                     PLANT_CODE STOCK_TYPE
------------ ------------------ ---------------------------------------- ------------ ---------- --------------------------------------- --------------------------------------- ------------------ --------------------------------------- ----------------------------------- ----------------------- ---------- ----------
0283472626   D93889             ADI STAIRS MEN      BLACK                L            L          400                                     400                                     6194114805         10                                      ROSS STORES INC                     2018-02-28 00:00:00.000 6010       L
0283472626   D93889             ADI STAIRS MEN      BLACK                M            M          400                                     400                                     6194114805         10                                      ROSS STORES INC                     2018-02-28 00:00:00.000 6010       L
0283472626   D93889             ADI STAIRS MEN      BLACK                S            S          200                                     200                                     6194114805         10                                      ROSS STORES INC                     2018-02-28 00:00:00.000 6010       L
0283472626   D93889             ADI STAIRS MEN      BLACK                XL           XL         200                                     200                                     6194114805         10                                      ROSS STORES INC                     2018-02-28 00:00:00.000 6010       L

如果我删除SUM并将QUANTITY添加到组中,我得到:

PO_NUMBER    MATERIAL           MATERIAL_DESCRIPTION                     SIZE_LITERAL GRID_VALUE PO_QTY                                  SO_QTY                                  SALES_ORDER_NUMBER SALES_ORDER_ITEM_NUMBER                 CUSTOMER_NAME                       ETA                     PLANT_CODE STOCK_TYPE
------------ ------------------ ---------------------------------------- ------------ ---------- --------------------------------------- --------------------------------------- ------------------ --------------------------------------- ----------------------------------- ----------------------- ---------- ----------
0283472626   D93889             ADI STAIRS MEN      BLACK                L            L          28                                      372                                     6194114805         10                                      ROSS STORES INC                     2018-02-28 00:00:00.000 6010       L
0283472626   D93889             ADI STAIRS MEN      BLACK                L            L          372                                     28                                      6194114805         10                                      ROSS STORES INC                     2018-02-28 00:00:00.000 6010       L
0283472626   D93889             ADI STAIRS MEN      BLACK                M            M          28                                      372                                     6194114805         10                                      ROSS STORES INC                     2018-02-28 00:00:00.000 6010       L
0283472626   D93889             ADI STAIRS MEN      BLACK                M            M          372                                     28                                      6194114805         10                                      ROSS STORES INC                     2018-02-28 00:00:00.000 6010       L
0283472626   D93889             ADI STAIRS MEN      BLACK                S            S          14                                      186                                     6194114805         10                                      ROSS STORES INC                     2018-02-28 00:00:00.000 6010       L
0283472626   D93889             ADI STAIRS MEN      BLACK                S            S          186                                     14                                      6194114805         10                                      ROSS STORES INC                     2018-02-28 00:00:00.000 6010       L
0283472626   D93889             ADI STAIRS MEN      BLACK                XL           XL         14                                      186                                     6194114805         10                                      ROSS STORES INC                     2018-02-28 00:00:00.000 6010       L
0283472626   D93889             ADI STAIRS MEN      BLACK                XL           XL         186                                     14                                      6194114805         10                                      ROSS STORES INC                     2018-02-28 00:00:00.000 6010       L

1 个答案:

答案 0 :(得分:2)

如果我理解你的逻辑,那么你应该在HAVING子句中比较两个总和:

SELECT
    M.STOCK_NUMBER AS [PO_NUMBER],
    M.MATERIAL, 
    M.MATERIAL_DESCRIPTION, 
    M.SIZE_LITERAL,
    A.GRID_VALUE,
    SUM(M.QUANTITY) AS [PO_QTY],
    SUM(A.QUANTITY) AS [SO_QTY],
    M.SALES_ORDER_NUMBER, 
    M.SALES_ORDER_ITEM_NUMBER, 
    M.CUSTOMER_NAME, 
    M.PLANNED_RECEIPT_DATE AS [ETA], 
    M.PLANT_CODE, 
    M.STOCK_TYPE
FROM VW_MRP_ALLOCATION M
INNER JOIN VW_ARUN_NORM_NEW A   
    ON M.SALES_ORDER_NUMBER = A.SALES_ORDER_NUMBER AND
       M.SALES_ORDER_ITEM_NUMBER = A.SALES_ORDER_ITEM_NUMBER AND
       M.STOCK_NUMBER = A.SAP_PO_NUMBER AND
       M.SIZE_LITERAL = A.GRID_VALUE AND
       M.STOCK_TYPE = A.STOCK_TYPE AND
       M.MATERIAL = A.MATERIAL_NUMBER
WHERE
    M.REQUIREMENT_TYPE = 'KE' AND
    M.STOCK_TYPE NOT IN ('A','C') AND
    M.STOCK_NUMBER IS NOT NULL AND
    M.QUANTITY <> A.QUANTITY
GROUP BY
    M.STOCK_NUMBER,
    M.MATERIAL, 
    M.MATERIAL_DESCRIPTION, 
    M.SIZE_LITERAL, 
    M.SALES_ORDER_NUMBER, 
    M.SALES_ORDER_ITEM_NUMBER, 
    M.CUSTOMER_NAME, 
    M.PLANNED_RECEIPT_DATE,
    M.PLANT_CODE, 
    M.STOCK_TYPE,
    A.GRID_VALUE
HAVING
    SUM(M.QUANTITY) <> SUM(A.QUANTITY)
ORDER BY
    M.STOCK_NUMBER,
    M.SIZE_LITERAL;