34,594
社区成员
发帖
与我相关
我的任务
分享
--查询出表中类型相同,但编号不相同的记录(合格的记录)
select * from #EE EE where Exists (select * from #EE where ttype=EE.ttype and Num<>EE.Num)
create table #EE
(
id int,
ttype nvarchar(20),
Num int
)
insert into #EE select 1,'A',10
union all select 2,'A',10
union all select 3,'A',11
union all select 4,'B',10
union all select 4,'B',11
--1:查询出表中类型相同,编号相同的记录(不合格的记录)
select * from #EE EE where Exists (select * from #EE where ttype=EE.ttype and Num=EE.Num and id<>EE.id)
2:查询出表中类型相同,但编号不相同的记录(合格的记录) ??
2 A 10
3 A 11
1 A 10
3 A 11
这样的组合符合要求吗
select a.* from 表 a where a.类型 in(select t.类型 from 表 t where exists(select 1 from 表 where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号))
select t.* from 表 t where not exists(select 1 from 表 where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号)
--Sorry 上面的理解错了
create table T
(
序号 int,
类型 nvarchar(10),
编号 int
)
insert T
select 1, 'A', 10
union all select 2, 'A', 10
union all select 3, 'A', 11
union all select 4, 'B', 10
union all select 4, 'B', 11
select * from T as tmp
where (select count(*) from T where 类型=tmp.类型 and 编号=tmp.编号)>1
declare @t table(序号 int,类型 varchar(8),编号 int)
insert into @t values(1,'A',10)
insert into @t values(2,'A',10)
insert into @t values(3,'A',11)
insert into @t values(4,'B',10)
insert into @t values(5,'B',11)
--如果是前者:
--1:查询出表中类型相同,编号相同的记录(不合格的记录)
select a.* from @t a where a.类型 in(select t.类型 from @t t where exists(select 1 from @t where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号))
/*
序号 类型 编号
----------- -------- -----------
1 A 10
2 A 10
3 A 11
*/
--2:查询出表中类型相同,但编号不相同的记录(合格的记录)
select a.* from @t a where a.类型 not in(select t.类型 from @t t where exists(select 1 from @t where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号))
/*
序号 类型 编号
----------- -------- -----------
4 B 10
5 B 11
*/
--如果是后者:
--1:查询出表中类型相同,编号相同的记录(不合格的记录)
select t.* from @t t where exists(select 1 from @t where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号)
/*
序号 类型 编号
----------- -------- -----------
1 A 10
2 A 10
*/
--2:查询出表中类型相同,但编号不相同的记录(合格的记录)
select t.* from @t t where not exists(select 1 from @t where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号)
/*
序号 类型 编号
----------- -------- -----------
3 A 11
4 B 10
5 B 11
*/
DECLARE @TB TABLE(ID INT, COL VARCHAR(2), COL2 INT)
INSERT @TB
SELECT 1, 'A', 10 UNION ALL
SELECT 2, 'A', 10 UNION ALL
SELECT 3, 'A', 11 UNION ALL
SELECT 4, 'B', 10 UNION ALL
SELECT 4, 'B', 11
SELECT * FROM @TB AS A WHERE EXISTS(SELECT 1 FROM @TB WHERE COL=A.COL AND COL2=A.COL2 AND ID<>A.ID)
/*
ID COL COL2
----------- ---- -----------
1 A 10
2 A 10
*/
SELECT * FROM @TB AS A WHERE NOT EXISTS(SELECT 1 FROM @TB WHERE COL=A.COL AND COL2=A.COL2 AND ID<>A.ID)
/*
ID COL COL2
----------- ---- -----------
3 A 11
4 B 10
4 B 11
*/
select * from tab a
where exists (
select 1 from tab
where 类型=a.类型 and 编号=a.编号
and 序号<>a.序号
)
select * from tab a
where not exists (
select 1 from tab
where 类型=a.类型 and 编号=a.编号
and 序号<>a.序号
)
select a.* from 表 a where a.类型 in(select t.类型 from 表 t where exists(select 1 from 表 where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号))
select a.* from 表 a where a.类型 not in(select t.类型 from 表 t where exists(select 1 from 表 where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号))
select t.* from 表 t where exists(select 1 from 表 where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号)
select t.* from 表 t where not exists(select 1 from 表 where 序号!=t.序号 and 类型=t.类型 and 编号=t.编号)