简体   繁体   English

通过比较sql服务器中同一表的每个记录来返回重复项

[英]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. 通过比较start_date和end_date,状态为1(即活跃且当前年度)的订户具有多个记录。 I have around more than 5000 records in the DB.Showing here few sample example. 我在数据库中有大约5000条记录,这里显示一些示例示例。

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. 只需删除where子句中的硬编码订户ID过滤器即可。 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: 您可以使用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: 以下查询提供了接近预期的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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