簡體   English   中英

通過比較sql服務器中同一表的每個記錄來返回重復項

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

我有如下表格,我想獲取重復的記錄。

通過比較start_date和end_date,狀態為1(即活躍且當前年度)的訂戶具有多個記錄。 我在數據庫中有大約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

只需刪除where子句中的硬編碼訂戶ID過濾器即可。 下面的查詢將返回預期的輸出。

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

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

以下查詢提供了接近預期的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