![](/img/trans.png)
[英]How to not return duplicates when comparing records in same table (A:B and B:A)
[英]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.