在连接查询上获得重复结果

时间:2017-08-05 00:59:10

标签: sql sql-server

我正在尝试连接两个表(VW_ATP_DOWNLOAD_NOREQCAT和VW_MM_SALES_GRID),它们必须是共同的列1.Material 2.Size_Literal我只想要表#2中的material和size_lite(VW_MM_SALES_GRID) - 我加入的VW_MM_SALES GRID以下栏目:

物料, Technical_index, Size_literal, active_flag, size_conversion, record_date_time, nrf_sizecode, technical_index_2digits

我希望制作的是所有的' A'字段,最重要的是来自&m;' m'因为M包含所有尺寸,其中A只包含具有现有值的尺寸 - 我希望看到所有尺寸,如果“A' A”中没有值,则返回null。表。如果我只是用这种材料运行一张桌子,我会得到结果:

尺寸L = 45 大小M = 51

我知道有一个大小S,我需要看到null,所以我可以转换为零值但是当我运行下面的查询时,它只是重复所有大小的值45和51 ....亏损谢谢。

查询:

 SELECT A.MATERIAL, 
       M.SIZE_LITERAL, 
       AVAIL_DATE,
       A.AVAIL_QTY,
       A.STOCK_CATEGORY

FROM   VW_ATP_DOWNLOAD_NOREQCAT A LEFT OUTER JOIN VW_MM_SALES_GRID M ON A.MATERIAL = M.MATERIAL

WHERE  M.MATERIAL = 'AA2721'

AND A.STOCK_CATEGORY IN ('A60381000','A60382000')

AND  A.AVAIL_DATE BETWEEN GETDATE() AND GETDATE() + 90

AND   A.MATERIAL = 'AA2721' 

AND  M.ACTIVE_FLAG IS NOT NULL

GROUP BY A.MATERIAL, 
         M.SIZE_LITERAL, 
         AVAIL_DATE, 
         A.AVAIL_QTY,
         A.STOCK_CATEGORY

我的结果:

AA2721 L 2017-08-26 00:00:00.000 45 A60382000
AA2721 L 2017-08-26 00:00:00.000 51 A60382000
AA2721 M 2017-08-26 00:00:00.000 45 A60382000
AA2721 M 2017-08-26 00:00:00.000 51 A60382000
AA2721 S 2017-08-26 00:00:00.000 45 A60382000
AA2721 S 2017-08-26 00:00:00.000 51 A60382000

当我只使用' A'表:

SELECT A.MATERIAL, 
       A.SIZE_LITERAL, 
       A.DATEPART(mm,AVAIL_DATE) AS 'MONTH',
       A.AVAIL_QTY,
       A.STOCK_CATEGORY

FROM   VW_ATP_DOWNLOAD_NOREQCAT A LEFT OUTER JOIN VW_MM_SALES_GRID M ON A.MATERIAL = M.MATERIAL

WHERE  M.MATERIAL IN ('AA2721')

AND A.STOCK_CATEGORY IN ('A60381000','A60382000')

AND  A.AVAIL_DATE BETWEEN GETDATE() AND GETDATE() + 90

AND   A.MATERIAL ='AA2721'

我明白了:

AA2721              L   8   45  A60382000       
AA2721              M   8   51  A60382000       

当我使用Join运行时,我得到了:

AA2721              L   2017-08-26 00:00:00.000 45  A60382000       
AA2721              L   2017-08-26 00:00:00.000 51  A60382000       
AA2721              M   2017-08-26 00:00:00.000 45  A60382000       
AA2721              M   2017-08-26 00:00:00.000 51  A60382000       
AA2721              S   2017-08-26 00:00:00.000 45  A60382000       
AA2721              S   2017-08-26 00:00:00.000 51  A60382000 

大应该有45而中应该有52,小有效大小应该是null因为它不在连接表中....不确定我做错了什么。谢谢!

M表的结果仅包含相关材料:

MATERIAL           TECHNICAL_INDEX SIZE_LITERAL ACTIVE_FLAG SIZE_CONVERSION_MATERIAL_ID RECORD_DATE_TIME        NRF_SIZECODE TECHNICAL_INDEX_2DIGITS
------------------ --------------- ------------ ----------- --------------------------- ----------------------- ------------ -----------------------
AA2721             480             2XS          NULL        KC                          2017-07-29 06:16:40.000 20900        21
AA2721             500             XS           NULL        KC                          2017-07-29 06:16:40.000 20901        23
AA2721             520             S            X           KC                          2017-07-29 06:16:40.000 20902        25
AA2721             540             M            X           KC                          2017-07-29 06:16:40.000 20903        27
AA2721             560             L            X           KC                          2017-07-29 06:16:40.000 20904        29

1 个答案:

答案 0 :(得分:0)

我认为M Table应该是数据透视表。我提出了一些加入条件的条件。

你能试试吗?

SELECT A.MATERIAL, 
       M.SIZE_LITERAL, 
       A.AVAIL_DATE,
       A.AVAIL_QTY,
       A.STOCK_CATEGORY
FROM   VW_MM_SALES_GRID M LEFT OUTER JOIN VW_ATP_DOWNLOAD_NOREQCAT A 
    ON A.MATERIAL = M.MATERIAL 
    AND A.SIZE_LITERAL = M.SIZE_LITERAL
    AND A.STOCK_CATEGORY IN ('A60381000','A60382000')
    AND A.AVAIL_DATE BETWEEN GETDATE() AND GETDATE() + 90

WHERE  M.MATERIAL = 'AA2721' AND  M.ACTIVE_FLAG IS NOT NULL

GROUP BY A.MATERIAL, 
         M.SIZE_LITERAL, 
         AVAIL_DATE, 
         A.AVAIL_QTY,
         A.STOCK_CATEGORY