如何仅打印前2个重复的SKU?

时间:2019-04-11 02:12:58

标签: sql sql-server

我需要选择下面的数据(imei-> websiteprice)以及前2个重复的sku。到目前为止,我已经从“ Vadim Loboda”获得了想法,尽管我使用partition获得了重复SKU的列表,但我只想打印TOP 2。

 ```  Sku                   Row_Number
      R-SONXPZ5CWHT-VF     1
      R-SONXPZ5CWHT-VF     2
      R-SONXPZ5CWHT-VF     3
      R-SONXPZ5CWHT-VF     4
      R-SONXPZ5GRN         1
      R-SONXPZ5BLK-EE      1         
      R-SONXPZ5BLK-EE      2
      R-SONXPZ5BLK-EE      3

    I want to just select the top 2 for each duplicate sku (which is 
    row_number 1 & 2) and order it 

WITH cteSku
    AS
    (
        SELECT 
             i.Id
            ,idesc.Sku
            ,ROW_NUMBER() OVER(Partition by idesc.Sku ORDER BY idesc.Sku) AS dupSku 
        FROM Item AS i
        INNER JOIN ItemDescription idesc
            ON i.Id = idesc.Id
    )  


SELECT  
         i.Imei  
        ,make.[Name] 'Make'
        ,model.[Name] 'Model'
        ,color.[Name] 'Color'
        ,storage.[Name] 'Storage'
        ,condition.[Name] 'Condition'
        ,connectivity.[Name] 'Connectivity'
        ,network.[Name] 'Network'
        ,idesc.Description 'Product'
        ,idesc.Sku 'SKU'
        ,cteSku.dupSku 'DupSku'
        ,i.Quantity 'Quantity'
        ,i.Id 'Stock ID'
        ,i.StoredOnUtc 'Scan in Date'
        ,DATEDIFF(DAY, I.StoredOnUtc, GETUTCDATE()) 'Age in Days'
        ,IIF(i.TaxSchemeId = 1, 'Standard', 'Marginal Vat') 'Tax Scheme'
        ,i.OriginalCogs 'Cost of Goods'
        ,i.RemanCost 'Roman Cost'
        ,i.OriginalPurchaseDate 'Original Purchase Date'
        ,i.Source 'Source'
        ,idesc.BasePrice 'Base Price'
        ,idesc.WebsitePrice 'Current Website Price'
    FROM Item AS i
    INNER JOIN ItemDescription idesc
        ON i.Id = idesc.Id
    LEFT JOIN (
        SELECT * FROM cteSku
    )AS cte ON cte.Id = i.Id
    LEFT JOIN ItemDescriptionMake make 
        ON idesc.ItemDescriptionMakeId = make.Id
    LEFT JOIN ItemDescriptionModel model 
        ON idesc.ItemDescriptionModelId = model.Id
    LEFT JOIN ItemDescriptionColor color 
        ON idesc.ItemDescriptionColorId = color.Id
    LEFT JOIN ItemDescriptionMemorySize storage 
        ON idesc.ItemDescriptionMemorySizeId = storage.Id
    LEFT JOIN ItemGrade condition 
        ON idesc.ItemDescriptionGradeId = condition.Id
    LEFT JOIN ItemDescriptionConnectivity connectivity 
        ON idesc.ItemDescriptionConnectivityId = connectivity.Id
    LEFT JOIN ItemDescriptionNetwork network 
        ON idesc.ItemDescriptionNetworkId = network.Id
    LEFT OUTER JOIN ProcessStatusWorkflowRecord AS psw 
        ON i.ProcessStatusWorkflowRecordId = psw.Id 
    LEFT OUTER JOIN Process AS p 
        ON psw.NextProcessId = p.Id
    WHERE (p.SystemName = 'storage_out') AND (i.ItemTypeId = 1)

我尝试从您的答案的底部添加“ AND cteSku.dupSku <3”。这与我期望的不同。结果不包括分割 每个SKU仅显示1。

但是当我尝试使用这些表而不加入表时:

 SELECT 
    id,
   sku,
    ROW_NUMBER() OVER(Partition by Sku ORDER BY Id) AS dupSku
 FROM  ItemDescription 

 ORDER BY dupSku desc

它显示我想要的结果。(请参阅上文)。

1 个答案:

答案 0 :(得分:0)

我做了一些更正。 LEFT JOIN到INNER JOIN并在底部添加WHERE条件。希望这对您有所帮助:

  SELECT  
         i.Imei  
        ,make.[Name] 'Make'
        ,model.[Name] 'Model'
        ,color.[Name] 'Color'
        ,storage.[Name] 'Storage'
        ,condition.[Name] 'Condition'
        ,connectivity.[Name] 'Connectivity'
        ,network.[Name] 'Network'
        ,idesc.Description 'Product'
        ,idesc.Sku 'SKU'
        ,cteSku.dupSku 'DupSku'
        ,i.Quantity 'Quantity'
        ,i.Id 'Stock ID'
        ,i.StoredOnUtc 'Scan in Date'
        ,DATEDIFF(DAY, I.StoredOnUtc, GETUTCDATE()) 'Age in Days'
        ,IIF(i.TaxSchemeId = 1, 'Standard', 'Marginal Vat') 'Tax Scheme'
        ,i.OriginalCogs 'Cost of Goods'
        ,i.RemanCost 'Roman Cost'
        ,i.OriginalPurchaseDate 'Original Purchase Date'
        ,i.Source 'Source'
        ,idesc.BasePrice 'Base Price'
        ,idesc.WebsitePrice 'Current Website Price'
    FROM Item AS i
    INNER JOIN ItemDescription idesc
        ON i.Id = idesc.Id
    INNER JOIN
       cteSku
    ON 
    cteSku.Id = i.Id
    LEFT JOIN ItemDescriptionMake make 
        ON idesc.ItemDescriptionMakeId = make.Id
    LEFT JOIN ItemDescriptionModel model 
        ON idesc.ItemDescriptionModelId = model.Id
    LEFT JOIN ItemDescriptionColor color 
        ON idesc.ItemDescriptionColorId = color.Id
    LEFT JOIN ItemDescriptionMemorySize storage 
        ON idesc.ItemDescriptionMemorySizeId = storage.Id
    LEFT JOIN ItemGrade condition 
        ON idesc.ItemDescriptionGradeId = condition.Id
    LEFT JOIN ItemDescriptionConnectivity connectivity 
        ON idesc.ItemDescriptionConnectivityId = connectivity.Id
    LEFT JOIN ItemDescriptionNetwork network 
        ON idesc.ItemDescriptionNetworkId = network.Id
    LEFT OUTER JOIN ProcessStatusWorkflowRecord AS psw 
        ON i.ProcessStatusWorkflowRecordId = psw.Id 
    LEFT OUTER JOIN Process AS p 
        ON psw.NextProcessId = p.Id
    WHERE (p.SystemName = 'storage_out') 
    AND (i.ItemTypeId = 1)
    AND cteSku.dupSku < 3