请教一个MS SQL查找重复记录集的问题

fatxinglee 2019-01-18 09:01:06
我大概的表结构是这样的
ysid cfid sourceid
101 1001 201
101 1001 202
101 1001 203
101 1002 201
101 1002 202
101 1002 203
101 1003 201
101 1003 203
101 1004 202

我的需求就是查找同一个ysid下的cfid内容完全一样的cfid
就是我是要得到结果是
ysid cfid
101 1001
101 1002

我就是想不明白怎么可以比较同一个表里面的不同记录集是否相同,求教各位数据库高手了
create table #temp
(ysid nvarchar(10),
cfid nvarchar(10),
sourceid nvarchar(10))

insert into #temp values('101','1001','201')
insert into #temp values('101','1001','202')
insert into #temp values('101','1001','203')
insert into #temp values('101','1002','201')
insert into #temp values('101','1002','202')
insert into #temp values('101','1002','203')
insert into #temp values('101','1003','201')
insert into #temp values('101','1003','203')
insert into #temp values('101','1004','202')


select * from #temp
...全文
212 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hello World, 2019-01-18
  • 打赏
  • 举报
回复
DECLARE @t TABLE(ysid NVARCHAR(10), cfid NVARCHAR(10), sourceid NVARCHAR(10));

INSERT INTO @t VALUES('101', '1001', '201');
INSERT INTO @t VALUES('101', '1001', '202');
INSERT INTO @t VALUES('101', '1001', '203');
INSERT INTO @t VALUES('101', '1002', '201');
INSERT INTO @t VALUES('101', '1002', '202');
INSERT INTO @t VALUES('101', '1002', '203');
INSERT INTO @t VALUES('101', '1003', '201');
INSERT INTO @t VALUES('101', '1003', '203');
INSERT INTO @t VALUES('101', '1004', '202');

WITH data AS (
SELECT DISTINCT tt.ysid, tt.cfid,
(SELECT sourceid
FROM @t t
WHERE t.ysid=tt.ysid AND t.cfid=tt.cfid
ORDER BY t.sourceid
FOR XML PATH('')) sourceid
FROM @t tt)
SELECT dd.ysid, dd.cfid, dd.sourceid
FROM data dd
WHERE (SELECT COUNT(*) FROM data d WHERE d.sourceid=dd.sourceid AND d.ysid = dd.ysid)>1

  • 打赏
  • 举报
回复
这是with 语法 t as(xxx)
t 就是代表 (xxx) 里面的子查询
fatxinglee 2019-01-18
  • 打赏
  • 举报
回复
引用 6 楼 砸死牛顿的苹果 的回复:
create table #temp
(ysid nvarchar(10),
cfid nvarchar(10),
sourceid nvarchar(10))

insert into #temp values('101','1001','201')
insert into #temp values('101','1001','202')
insert into #temp values('101','1001','203')
insert into #temp values('101','1002','201')
insert into #temp values('101','1002','202')
insert into #temp values('101','1002','203')
insert into #temp values('101','1003','201')
insert into #temp values('101','1003','203')
insert into #temp values('101','1004','202')

;with t as (select ysid,cfid,sourceid,count(1)over(partition by ysid,cfid)ct from #temp)
select distinct t1.ysid,t1.cfid from t t1
left join t t2
on (t1.ysid <>t2.ysid or t1.cfid <>t2.cfid) and t1.ct =t2.ct and t1.sourceid=t2.sourceid
group by t1.ysid,t1.cfid,t2.ysid,t2.cfid,t1.ct
having count(t2.sourceid)=t1.ct

drop table #temp
/*
ysid cfid
101 1001
101 1002
*/


大神·····你的语句太牛了···但我看不懂,t是哪里来的啊?
  • 打赏
  • 举报
回复
create table #temp
(ysid nvarchar(10),
cfid nvarchar(10),
sourceid nvarchar(10))

insert into #temp values('101','1001','201')
insert into #temp values('101','1001','202')
insert into #temp values('101','1001','203')
insert into #temp values('101','1002','201')
insert into #temp values('101','1002','202')
insert into #temp values('101','1002','203')
insert into #temp values('101','1003','201')
insert into #temp values('101','1003','203')
insert into #temp values('101','1004','202')

;with t as (select ysid,cfid,sourceid,count(1)over(partition by ysid,cfid)ct from #temp)
select distinct t1.ysid,t1.cfid from t t1
left join t t2
on (t1.ysid <>t2.ysid or t1.cfid <>t2.cfid) and t1.ct =t2.ct and t1.sourceid=t2.sourceid
group by t1.ysid,t1.cfid,t2.ysid,t2.cfid,t1.ct
having count(t2.sourceid)=t1.ct

drop table #temp
/*
ysid cfid
101 1001
101 1002
*/
  • 打赏
  • 举报
回复
就是不同YSID和CFID 情况下 含有相同的 SOURCEID 吧
fatxinglee 2019-01-18
  • 打赏
  • 举报
回复
引用 2 楼 二月十六 的回复:
同一个ysid下的cfid内容完全一样的cfid

什么意思?楼主给的例子ysid都是101,但是cfid不一样有1001 、1002

就是同一个YSID可以有很多个CFID,CFID里面有明细SOURCEID,我就是要找出同一个YSID下,明细都相同的CFID。按我的用例来说,就是1001和1002这两个是相同的,我想用语句来找出来
fatxinglee 2019-01-18
  • 打赏
  • 举报
回复
引用 1 楼 RINK_1 的回复:
SELECT YSID,CFID FROM TABLE GROUP BY YSID,CFID HAVING COUNT(*)>1


你这语句怎么体现“重复”啊?只是明细条目多过1条而已
二月十六 2019-01-18
  • 打赏
  • 举报
回复
同一个ysid下的cfid内容完全一样的cfid 什么意思?楼主给的例子ysid都是101,但是cfid不一样有1001 、1002
RINK_1 2019-01-18
  • 打赏
  • 举报
回复
SELECT YSID,CFID FROM TABLE GROUP BY YSID,CFID HAVING COUNT(*)>1
  • 打赏
  • 举报
回复
楼上才是正解~

22,297

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧