Getting duplicates with additional information

安稳与你 提交于 2019-12-02 11:17:34

Try this:

SELECT b.ID,displayid,version,company,productdata.title
FROM 
(select A.ID,a.displayid,version,a.company,rn,a.code, COUNT(displayid)  over (partition by displayid,code) cnt from
(select Prod.ID,displayid,version,company,Companies.code, Row_number() over (partition by displayid,company order by version desc) rn
from Prod inner join Companies on Prod.Company = Companies.id) a  
where a.rn=1) b inner join productdata on b.id = productdata.id  where cnt =2

Based on your sample data, you just need to JOIN the tables:

  SELECT 
    p.Id, p.DisplayId, p.Version, p.Company, d.Title
  FROM Products AS p
  INNER JOIN Companies AS c ON p.Company = c.Id
  INNER JOIN ProductData AS d ON d.ProductId = p.Id;

But if you want the latest one, you can use the ROW_NUMBER():

WITH CTE
AS
(
  SELECT 
    p.Id, p.DisplayId, p.Version, p.Company, d.Title,
    ROW_NUMBER() OVER(PARTITION BY p.DisplayId,p.Company ORDER BY p.Id DESC) AS RN
  FROM Products AS p
  INNER JOIN Companies AS c ON p.Company = c.Id
  INNER JOIN ProductData AS d ON d.ProductId = p.Id
)
SELECT * 
FROM CTE
WHERE RN = 1;

sample fiddle

| Id | DisplayId | Version | Company |    Title |
|----|-----------|---------|---------|----------|
|  5 |     12345 |       1 |       2 |  Title 5 |
|  7 |     12345 |       2 |      16 |  Title 7 |
| 10 |      XX45 |       1 |       5 | Title 10 |
| 11 |      XX45 |       1 |       7 | Title 11 |

If i understood you correctly, you can use CTE to find all the duplicated rows from your table, then you can just use SELECT from CTE and even add more manipulations.

WITH CTE AS(
   SELECT Id,DisplayId,Version,Company,Description,ProductData.Title
       RN = ROW_NUMBER()OVER(PARTITION BY DisplayId, Company ORDER BY p.Id DESC)
   FROM dbo.YourTable1
)

SELECT *
FROM CTE

You have to first get the current version and then you see how many times the DisplayID + Code show-up. Then based on that you can select only the ones that have a count greater than one. You can then INNER JOIN ProductData on the final query to get the Title.

WITH
MaxVersion AS --Get the current versions
(
    SELECT
        MAX(Version) AS Version,
        DisplayID,
        Company
    FROM
        #TmpProducts
    GROUP BY
        DisplayID,
        Company
)
,CTE AS
(
    SELECT
        p.DisplayID,
        c.Code,
        COUNT(*) AS RowCounter
    FROM
        #TmpProducts p
    INNER JOIN
        #TmpCompanies c
        ON
            c.ID = p.Company
    INNER JOIN
        MaxVersion mv
        ON
            mv.DisplayID = p.DisplayID
        AND mv.Version = p.Version
        AND mv.Company = p.Company
    GROUP BY
        p.DisplayID,
        c.Code
)

SELECT 
    p.*
FROM
    #TmpProducts p
INNER JOIN
    CTE c
    ON
        c.DisplayID = p.DisplayID
INNER JOIN
    MaxVersion mv
    ON
        mv.DisplayID = p.DisplayID
    AND mv.Company = p.Company
    AND mv.Version = p.Version
WHERE
    c.RowCounter > 1
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!