简体   繁体   中英

Return duplicates by comparing each records of the same table in the sql server

I have table like below.I wanted to get the duplicate records.Here the condition

Subscriber whose status = 1 ie active and for current year it has the multiple records by comparing start_date and end_date. I have around more than 5000 records in the DB.Showing here few sample example.

id      pkg_id  start_date  end_date    status  subscriber_id
2857206 9128    8/31/2014   8/31/2015   2       3031103
2857207 9128    12/22/2015  12/22/2016  1       3031103
3066285 10308   8/5/2016    8/4/2018    1       3031103
2857206 9128    8/31/2013   8/31/2015   2       3031104
2857207 9128    10/20/2015  11/22/2016  1       3031104
3066285 10308   7/5/2016    7/4/2018    1       3031104
3066285 10308   8/5/2016    8/4/2018    2       3031105

I tried below's query but not worked for all records:

SELECT  *
FROM    dbo.consumer_subsc
WHERE   status = 1
        AND YEAR(GETDATE()) >= YEAR(start_date)
        AND YEAR(GETDATE()) <= YEAR(end_date)
        AND subscriber_id IN (
        SELECT  T.subscriber_id
        FROM    ( SELECT    subscriber_id ,
                            COUNT(subscriber_id) AS cnt
                  FROM      dbo.consumer_subsc
                  WHERE     status = 1
                  GROUP BY  subscriber_id
                  HAVING    COUNT(subscriber_id) > 1
                ) T )
ORDER BY subscriber_id DESC

The problem is I'm not able to find a way, where each row can be compared with each other with above date condition.I should get the result like below as duplicate:

id      pkg_id  start_date  end_date    status  subscriber_id
2857207 9128    12/22/2015  12/22/2016  1       3031103
3066285 10308   8/5/2016    8/4/2018    1       3031103
2857207 9128    10/20/2015  11/22/2016  1       3031104
3066285 10308   7/5/2016    7/4/2018    1       3031104

Just remove the hardcoded subscriberid filter in your where clause. The below query would return the expected output.

SELECT *
FROM dbo.consumer_subsc
WHERE  STATUS = 1
    AND year(getdate()) >= year(start_date)
    AND year(getdate()) <= year(end_date)
    AND subscriber_id IN (
        SELECT T.subscriber_id
        FROM (
            SELECT subscriber_id
                ,count(subscriber_id) AS cnt
            FROM dbo.consumer_subsc
            WHERE STATUS = 1
            GROUP BY subscriber_id
            HAVING count(subscriber_id) > 1
            ) T
        )
ORDER BY subscriber_id ,start_date

You can use EXISTS:

 SELECT t.* FROM dbo.consumer_subsc t 
 WHERE EXISTS(SELECT subscriber_id 
        FROM dbo.consumer_subsc y 
        WHERE y.status=t.status
            AND y.subscriber_id = t.subscriber_id 
        GROUP BY subscriber_id HAVING COUNT(y.subscriber_id)>1) 
 AND STATUS = 1
 AND year(getdate()) >= year(start_date) 
 AND year(getdate()) <= year(end_date)
WITH CTE (Code, DuplicateCount)
AS
(
    SELECT subscriber_id,
    ROW_NUMBER() OVER(PARTITION BY  subscriber_id
    ORDER BY  subscriber_id) AS DuplicateCount
    FROM dbo.consumer_subsc 
    where  subscriber_id in (3031103) 
    and status=1 and year(getdate()) >= year(start_date) 
    and year(getdate()) <= year(end_date)  

)
Select * from CTE

Below's query giving the near to expected O/P:

SELECT A.* FROM (SELECT t.*,Row_number() OVER(partition BY t.subscriber_id ORDER BY t.subscriber_id,t.start_date) rnk  FROM dbo.consumer_subsc t 
 WHERE EXISTS(SELECT subscriber_id 
        FROM dbo.consumer_subsc y 
        WHERE y.status=t.status
            AND y.subscriber_id = t.subscriber_id 
        GROUP BY subscriber_id HAVING COUNT(y.subscriber_id)>1) 
 AND STATUS = 1
 AND year(getdate()) >= year(start_date) 
 AND year(getdate()) <= year(end_date))A WHERE A.rnk>1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM