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

时间:2016-09-12 06:52:49

标签: sql sql-server sql-server-2008 sql-server-2005

我有如下表格。我想得到重复记录。这是条件

状态为1的订阅者,即活动状态和当前年份,通过比较start_date和end_date,它具有多个记录。我在DB中有大约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

我尝试了下面的查询但不适用于所有记录:

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

问题是我找不到方法,每行可以用上述日期条件相互比较。我应该得到如下所示的重复结果:

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

4 个答案:

答案 0 :(得分:1)

只需删除where子句中的硬编码用户标识过滤器即可。以下查询将返回预期的输出。

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

答案 1 :(得分:1)

您可以使用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)

答案 2 :(得分:0)

var gotPass = jsonFileArr.some(function (obj) {
                 return obj.pass == pass;
                });

答案 3 :(得分:0)

下面的查询给出接近预期的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