连接表后选择不同的记录ID

时间:2014-05-12 03:06:32

标签: sql join distinct

我有两个表,我使用内连接。 ArticleDetail和我的临时表#tempexceptionBag

Select [Country],[Date],[Location] ,[Article],[Colour],b.Size ,b.Barcode
  ,[SoldQty],[UnitCost],[GrossPrice],[Netprice],[NetpriceSGD],[TransactionID],[StaffID],[UploadFlagUpdated]
  ,[RecordID]
from #tempexceptionBag a
inner join CK_DW..ArticleDetail b
on a.Article = b.Articleno
and right('0' + a.Colour,2) = right('0' + b.Color_code,2)
where a.size is not null

这个命令会导致我重复记录...如何选择唯一的记录? 我尝试使用

Select Distinct [Country],[Date],[Location] ,[Article],[Colour],b.Size ,b.Barcode ,[SoldQty],[UnitCost],[GrossPrice],[Netprice],[NetpriceSGD],[TransactionID],[StaffID],[UploadFlagUpdated] ,[RecordID] from #tempexceptionBag a inner join CK_DW..ArticleDetail b on a.Article = b.Articleno and right('0' + a.Colour,2) = right('0' + b.Color_code,2) where a.size is not null

但结果我仍然会重复记录

示例结果

> Country   Date         Location            Article     Colour Size    Barcode     SoldQty UnitCost    GrossPrice  Netprice    NetpriceSGD TransactionID   StaffID UploadFlagUpdated   RecordID

INDONESIA   20/4/2014     BAZAR BANDUNG    CK2-10680039   22    XS      1001025022328   1   167769      339000      209000       22.82       BZ02359         NULL      0                30711547
INDONESIA   20/4/2014     BAZAR BANDUNG    CK2-10680039   22    XS      1001025022328   1   167769      339000      209000       22.82       BZ02436         NULL      0                30711547
INDONESIA   20/4/2014     BAZAR BANDUNG    CK2-10680050   1     XS      1001107201320   1   154845      349000      209000       22.82       BZ02136         NULL      0                30711551
INDONESIA   20/4/2014     BAZAR BANDUNG    CK2-10680073   6     XS      1001226106322   1   186402      419000      299000       32.66       BZ02231         NULL      0                30711560

但我想要的是

   Country  Date         Location            Article     Colour Size    Barcode     SoldQty UnitCost    GrossPrice  Netprice    NetpriceSGD TransactionID   StaffID UploadFlagUpdated   RecordID

INDONESIA   20/4/2014     BAZAR BANDUNG    CK2-10680039   22    XS      1001025022328   1   167769      339000      209000       22.82       BZ02436         NULL      0                30711547
INDONESIA   20/4/2014     BAZAR BANDUNG    CK2-10680050   1     XS      1001107201320   1   154845      349000      209000       22.82       BZ02136         NULL      0                30711551
INDONESIA   20/4/2014     BAZAR BANDUNG    CK2-10680073   6     XS      1001226106322   1   186402      419000      299000       32.66       BZ02231         NULL      0                30711560

附加

尝试后

select [Country],[Date],[Location] ,[Article],[Colour],b.Size ,b.Barcode,[SoldQty],[UnitCost],[GrossPrice],[Netprice],[NetpriceSGD],[TransactionID],[StaffID],[UploadFlagUpdated],COUNT([RecordID]) As num_Recordid from #tempexceptionBag a inner join CK_DW..ArticleDetail b on a.Article = b.Articleno and right('0' + a.Colour,2) = right('0' + b.Color_code,2) where a.size is not nullgroup by [Country],[Date],[Location] ,[Article],[Colour],b.Size ,b.Barcode ,[SoldQty],[UnitCost],[GrossPrice],[Netprice],[NetpriceSGD],[StaffID],[TransactionID],[UploadFlagUpdated]

我无法显示记录

1 个答案:

答案 0 :(得分:0)

您正在获取重复项,因为您在库存查询中包含交易如果您排除了TransactionID,它将为DISTINCT提供足够的唯一性,以便为您提供您期望的结果。

您还可以汇总交易以获取该项目的交易计数。 e.g。

SELECT 
     [Country]
    ,[Date]
    ,[Location]
    ,[Article]
    ,[Colour]
    ,b.Size
    ,b.Barcode
    ,[SoldQty]
    ,[UnitCost]
    ,[GrossPrice]
    ,[Netprice]
    ,[NetpriceSGD]
    ,[StaffID]
    ,[UploadFlagUpdated]
    ,[RecordID]
    ,COUNT([TransactionID]) AS num_transactions
FROM #tempexceptionBag a
    INNER JOIN CK_DW..ArticleDetail b ON a.Article = b.Articleno
        AND right('0' + a.Colour, 2) = right('0' + b.Color_code, 2)
WHERE a.size IS NOT NULL
GROUP BY [Country]
        ,[Date]
        ,[Location]
        ,[Article]
        ,[Colour]
        ,b.Size
        ,b.Barcode
        ,[SoldQty]
        ,[UnitCost]
        ,[GrossPrice]
        ,[Netprice]
        ,[NetpriceSGD]
        ,[StaffID]
        ,[UploadFlagUpdated]
        ,[RecordID]

或者,如果您想要一个包含所有事务ID的列,则可以将它们选为xml字符串。然后,您可以使用STUFF函数很好地格式化它。我现在不能在某个地方测试它,所以你可能需要理智检查我的查询,但它会是这样的:

SELECT 
     [Country]
    ,[Date]
    ,[Location]
    ,[Article]
    ,[Colour]
    ,b.Size
    ,b.Barcode
    ,[SoldQty]
    ,[UnitCost]
    ,[GrossPrice]
    ,[Netprice]
    ,[NetpriceSGD]
    ,[StaffID]
    ,[UploadFlagUpdated]
    ,[RecordID]
    ,(STUFF((
                SELECT
                    TransactionID
                FROM #tempexceptionBag ia
                    INNER JOIN CK_DW..ArticleDetail ib ON ia.Article = ib.Articleno
                        AND right('0' + ia.Colour, 2) = right('0' + ib.Color_code, 2)
                WHERE ia.size IS NOT NULL
                    AND a.Article = ia.Article
                FOR XML PATH('')
                ), 1, 2, ''
            )
      ) AS TransactionIDs
FROM #tempexceptionBag a
    INNER JOIN CK_DW..ArticleDetail b ON a.Article = b.Articleno
        AND right('0' + a.Colour, 2) = right('0' + b.Color_code, 2)
WHERE a.size IS NOT NULL
GROUP BY [Country]
        ,[Date]
        ,[Location]
        ,[Article]
        ,[Colour]
        ,b.Size
        ,b.Barcode
        ,[SoldQty]
        ,[UnitCost]
        ,[GrossPrice]
        ,[Netprice]
        ,[NetpriceSGD]
        ,[StaffID]
        ,[UploadFlagUpdated]
        ,[RecordID]

背后的想法是将内联查询作为XML字符串进行,例如

SELECT  
     ( SELECT
            ', ' + CAST(surrogate_key AS NVARCHAR(50)) AS [text()]
        FROM dimension IDIM
        WHERE IDIM.code = DIM.code
        FOR XML PATH('')
     )
    ,code
FROM dimension DIM
WHERE code LIKE 'Code With Duplicates'
GROUP BY code

这会给你结果,然后STUFF只是格式化

SELECT  
     (STUFF((SELECT
                    ', ' + CAST(surrogate_key AS NVARCHAR(50)) AS [text()]
                FROM dimension IDIM
                WHERE IDIM.code = DIM.code
                FOR XML PATH('')
                ), 1, 2, ''
            )
     )
    ,code
FROM dimension DIM
WHERE code LIKE 'Code With Duplicates'
GROUP BY code

如果您只想为该文章显示第一个transactionID,可以使用ROW_NUMBER(),然后将其包装在另一个select中。

SELECT
     [Country]
    ,[Date]
    ,[Location]
    ,[Article]
    ,[Colour]
    ,b.Size
    ,b.Barcode
    ,[SoldQty]
    ,[UnitCost]
    ,[GrossPrice]
    ,[Netprice]
    ,[NetpriceSGD]
    ,[StaffID]
    ,[UploadFlagUpdated]
    ,[RecordID]
    ,TransactionID
FROM(
    SELECT 
         [Country]
        ,[Date]
        ,[Location]
        ,[Article]
        ,[Colour]
        ,b.Size
        ,b.Barcode
        ,[SoldQty]
        ,[UnitCost]
        ,[GrossPrice]
        ,[Netprice]
        ,[NetpriceSGD]
        ,[StaffID]
        ,[UploadFlagUpdated]
        ,[RecordID]
        ,TransactionID
        ,ROW_NUMBER() OVER (
                        PARTITION BY Article 
                        ORDER BY TransactionID
                        ) SEQ
    FROM #tempexceptionBag a
        INNER JOIN CK_DW..ArticleDetail b ON a.Article = b.Articleno
            AND right('0' + a.Colour, 2) = right('0' + b.Color_code, 2)
    WHERE a.size IS NOT NULL
    GROUP BY [Country]
            ,[Date]
            ,[Location]
            ,[Article]
            ,[Colour]
            ,b.Size
            ,b.Barcode
            ,[SoldQty]
            ,[UnitCost]
            ,[GrossPrice]
            ,[Netprice]
            ,[NetpriceSGD]
            ,[StaffID]
            ,[UploadFlagUpdated]
            ,[RecordID]
) I
WHERE SEQ = 1