表test的结果如下:序号 字段1 字段2 字段3
1 a a 6
2 b c 2
3 b d 2
4 b e 2
5 b e 5要求:1.只要字段1和字段2的值都相同,就认为记录是相同的。
2.只要字段3的值相同,就认为记录是相同的。根据上述表可知最终的结果是2条不同记录。如果通过sql语句得到结果:2 ?
1 a a 6
2 b c 2
3 b d 2
4 b e 2
5 b e 5要求:1.只要字段1和字段2的值都相同,就认为记录是相同的。
2.只要字段3的值相同,就认为记录是相同的。根据上述表可知最终的结果是2条不同记录。如果通过sql语句得到结果:2 ?
select * from T as tmp
where (select count(*) from T where 字段3=tmp.字段3)=1
and 字段1<>字段2
insert T select 1, 'a', 'a', 6
union all select 2, 'b', 'c', 2
union all select 3, 'b', 'd', 2
union all select 4, 'b', 'e', 2
union all select 5, 'b', 'e', 5select * from T as tmp
where (select count(*) from T where 字段3=tmp.字段3)=1
and 字段1<>字段2--result
序号 字段1 字段2 字段3
----------- ---- ---- -----------
5 b e 5(1 row(s) affected)
insert @T select 1, 'a', 'a', 6
union all select 2, 'b', 'c', 2
union all select 3, 'b', 'd', 2
union all select 4, 'b', 'e', 2
union all select 5, 'b', 'e', 5select count(序号) from @t a where not exists
(select 1 from @t where (字段3=a.字段3 or (字段1=a.字段1 and 字段2=a.字段2)) and 序号<a.序号)--结果
-----------
2
declare @t table(序号 int, 字段1 char(1), 字段2 char(1), 字段3 int)
insert @T select 1, 'a', 'a', 6
union all select 2, 'b', 'c', 2
union all select 3, 'b', 'd', 2
union all select 4, 'b', 'e', 2
union all select 5, 'b', 'e', 5select * from @t a where not exists
(select 1 from @t where (字段3=a.字段3 or (字段1=a.字段1 and 字段2=a.字段2)) and 序号<a.序号)
--结果
序号 字段1 字段2 字段3
----------- ---- ---- -----------
1 a a 6
2 b c 2(所影响的行数为 2 行)
where not exists(select 1 from T where (字段1=tmp.字段1 and 字段2=tmp.字段2) or 字段3=tmp.字段3)
where not exists
(select 1 from T where 字段1+字段2=a.字段1+a.字段2 or 字段3=a.字段3)
drop table tb
gocreate table tb
(
序号 int,
字段1 varchar(1),
字段2 varchar(1),
字段3 varchar(1)
)insert into tb(序号,字段1,字段2,字段3) values(1,'a','a','6')
insert into tb(序号,字段1,字段2,字段3) values(2,'b','c','2')
insert into tb(序号,字段1,字段2,字段3) values(3,'b','d','2')
insert into tb(序号,字段1,字段2,字段3) values(4,'b','e','2')
insert into tb(序号,字段1,字段2,字段3) values(5,'b','e','5')select count(*) as result from
(
select 字段1,字段2 from
(
select * from tb where 字段3 in
(select 字段3 from tb group by 字段3 having count(*) = 1)
) t
group by 字段1,字段2 having count(*) = 1
) mdrop table tbresult
-----------
2(所影响的行数为 1 行)
而dawugui(潇洒老乌龟) 的对于增加一条新记录,比如
变成如下以后:
序号 字段1 字段2 字段3
1 a a 6
2 b c 2
3 b d 2
4 b e 2
5 b e 5
6 b f 5得到的结果是1,正确的应该还是2才对啊。
hinco(桃色德鲁依)的可用
dawugui(潇洒老乌龟) ( 两星(中级)) 信誉:100
的没看透
(select count(*) from
(select distinct 字段3 from @test t where
(select count(字段3) from @test where 字段3=t.字段3)>1) as a)
+
(select count(*) from @test group by 字段1,字段2 having 字段1=字段2)
好像上面的所有写法都有问题- -!
对多次关联且顺序不同意的数据,结果都是错的
如这样的数据
insert @T select 1, 'a', 'a', 6
union all select 2, 'b', 'h', 5
union all select 3, 'b', 'd', 2
union all select 4, 'b', 'e', 2
union all select 5, 'b', 'e', 5
union all select 7, 'c', 'd', 6
union all select 8, 'b', 'd', 6
先创建递归存储过程
create proc sp_Searchsame
as
declare @count int
insert into #temp1
select * from #temp a where
exists(select 1 from #temp where (字段1=a.字段1 and 字段2=a.字段2) or 字段3=a.字段3)
and not exists(select 1 from #temp1 where 序号=a.序号)
if @@rowcount=0
begin
insert into #temp1
select top 1 * from #temp a where
not exists(select 1 from #temp1 where 序号=a.序号) order by 序号
if @@rowcount=0
set @count=0
else
begin
exec @count=sp_Searchsame
set @count=@count+1
end
end
else
exec @count=sp_Searchsame
return @count
--然后具体数据测试
create table #temp(序号 int, 字段1 char(1), 字段2 char(1), 字段3 int)
insert #temp select 1, 'a', 'a', 6
union all select 2, 'b', 'h', 5
union all select 3, 'b', 'd', 2
union all select 4, 'b', 'e', 2
union all select 5, 'b', 'e', 5
union all select 7, 'c', 'd', 6
union all select 8, 'b', 'd', 6
--创建临时表#temp1
create table #temp1(序号 int, 字段1 char(1), 字段2 char(1), 字段3 int)
--具体执行过程
declare @count int
delete from #temp1
exec @count=sp_Searchsame
print @count
--结果
1
insert into @t values(2,'b','c','2')
insert into @t values(3,'b','d','2')
insert into @t values(4,'b','e','2')
insert into @t values(5,'b','e','5')
select * from @t where id not in
(select b.id from @t a,@t b where ((a.f1=b.f1 and a.f2=b.f2) or(a.f3=b.f3)) and (a.id < b.id))
(select b.id from @t a,@t b where ((a.f1=b.f1 and a.f2=b.f2) or(a.f3=b.f3)) and (a.id < b.id))
id f1 f2 f3
----------- ---------- ---------- ----------
1 a a 6
2 b c 2
(select count(*) from (select distinct 字段3 from @test group by 字段3 having count(字段3)>1) as a
)+
(select count(*) from @test t1 where (select count(*) from @test where 字段1=t1.字段1 and 字段2=t1.字段2)>1
)-
(select count(*) from @test t2 where (select count(*) from @test where 字段1=t2.字段1 and 字段2=t2.字段2 and 字段3=t2.字段3)>1
)*2
(select count(*) from (select distinct 字段3 from @test group by 字段3 having count(字段3)>1) as a
)+
(select count (*) from (select distinct 字段1,字段2 from @test t1 where (select count(*) from @test where 字段1=t1.字段1 and 字段2=t1.字段2)>1) as e
)这么写吧
(select count(*) from (select distinct 字段3 from @test group by 字段3 having count(字段3)>1) as a
)+
(select count (*) from (select distinct 字段1,字段2 from @test t1 where (select count(*) from @test where 字段1=t1.字段1 and 字段2=t1.字段2)>1) as e
)
-
(select count (*) from (select distinct 字段1,字段2,字段3 from @test t2 where
(select
case
when count(*)=(select count(*) from @test where 字段1=t2.字段1 and 字段2=t2.字段2)
and count(*)=(select count(*) from @test where 字段3=t2.字段3)
then count(*)
else
1
end as l
from @test as t3 where 字段1=t2.字段1 and 字段2=t2.字段2 and 字段3=t2.字段3)>1) as h)写的不好 不过是正确答案
insert @T select 1, 'a', 'a', 6
union all select 2, 'b', 'c', 2
union all select 3, 'b', 'd', 2
union all select 4, 'b', 'e', 2
union all select 5, 'b', 'e', 5select count(*) from @t a
left join (select * from @t) b on ((a.字段1=b.字段1 and a.字段2=b.字段2) or (a.字段3=a.字段3)) and a.序号<b.序号)
where b.序号 is null
变成如下以后:
序号字段1字段2字段3
1aa6
2bc2
3bd2
4be2
5be5
6bf5得到的结果是1,正确的应该还是2才对啊。1.只要字段1和字段2的值都相同,就认为记录是相同的。
2.只要字段3的值相同,就认为记录是相同的。上面的数据只有1aa6满足条件,结果是1,怎么会是2?
LZ要找的是有几种不同的记录,而不是让你找出没有重复记录的,汗
这样求连通区域的只能用循环或者递归,想一个语句解决是很困难,如果有人觉得能用一个语句解决,请先用给的数据测试一下你得出的结果是不是1
1aa6
2bc2
3bd2
4be2
5be5
6bf5
1.只要字段1和字段2的值都相同,就认为记录是相同的。
2.只要字段3的值相同,就认为记录是相同的。字段1和字段2相同 也就是 be 和be 相同 就应该是记录是相同的
字段3重复 就应该是相同的
这题唯一注意的是 出现字段1 2 3 完全相同的记录并且没有在其他行出现的
(select count(*) from (select distinct 字段3 from @test group by 字段3 having count(字段3)>1) as a
)+
(select count (*) from (select distinct 字段1,字段2 from @test t1 where (select count(*) from @test where 字段1=t1.字段1 and 字段2=t1.字段2)>1) as e
)
-
(select count (*) from (select distinct 字段1,字段2,字段3 from @test t2 where
(select
case
when count(*)=(select count(*) from @test where 字段1=t2.字段1 and 字段2=t2.字段2)
and count(*)=(select count(*) from @test where 字段3=t2.字段3)
then count(*)
else
1
end as l
from @test as t3 where 字段1=t2.字段1 and 字段2=t2.字段2 and 字段3=t2.字段3)>1) as h)这就是正确答案
对于这组数据
declare @test table(序号 int, 字段1 char(1), 字段2 char(1), 字段3 int)
insert @test select 1, 'a', 'a', 6
union all select 2, 'b', 'h', 5
union all select 3, 'b', 'd', 2
union all select 4, 'b', 'e', 2
union all select 5, 'b', 'e', 5
union all select 7, 'c', 'd', 6
union all select 8, 'b', 'd', 6
正确答案应该是1,而你的语句是5
题:
序号 字段1 字段2 字段3
1 a a 6
2 b c 2
3 b d 2
4 b e 2
5 b e 5
1.只要字段1和字段2的值都相同,就认为记录是相同的。
2.只要字段3的值相同,就认为记录是相同的。根据上述表可知最终的结果是2条不同记录。如果通过sql语句得到结果:2 ?郁闷:
1.只要字段1和字段2的值都相同,就认为记录是相同的。
4和5相同
2.只要字段3的值相同,就认为记录是相同的。
2,3,4相同那不就只有1不同了吗~~~怎么会有2条~~
楼上的解4下~~~看晕了~~~
declare @upfiled1 varchar
declare @upfiled2 varchar
declare @upfiled3 varchar
declare @bj int
set @upfiled1=''
set @upfiled2=''
set @upfiled3=''
update #xzx set @bj= case when @upfiled1=字段1 and @upfiled2=字段2 or @upfiled3=字段3 then 0 else 1 end,標記=@bj,
@upfiled1=字段1,@upfiled2=字段2,@upfiled3=字段3
select count(*) from #xzx where 標記=1
select 序號,字段1,字段2,字段3,0 as 標記 into #xzx from tt order by 字段3,字段1﹐字段2
'說明一下﹐第一第語句將按照可能相同的記錄放在一起。
declare @upfiled1 varchar
declare @upfiled2 varchar
declare @upfiled3 varchar
declare @bj int
set @upfiled1=''
set @upfiled2=''
set @upfiled3=''
update #xzx set @bj= case when @upfiled1=字段1 and @upfiled2=字段2 or @upfiled3=字段3 then 0 else 1 end,標記=@bj,
@upfiled1=字段1,@upfiled2=字段2,@upfiled3=字段3
select count(*) from #xzx where 標記=1
再次说下你审错题了
序号 字段1 字段2 字段3
1 a a 6
2 b c 2
3 b d 2
4 b e 2
5 b e 5
这是楼主得题
最后结果是2
楼主说只要字段1和字段2得值都相同 说得不是序号是1得a a 而是序号4和5出现得
字段1和字段2得值相同
mygod 我没错额
select @Allcount=count(*) from TT
select count(*)+1
from(
select a.序號,count(*) as 數量
from tt a inner join tt b on a.序號<>b.序號
where a.字段3<>b.字段3 and (a.字段1<>b.字段1 or a.字段2<>b.字段2)
group by a.序號
having count(*)=@Allcount-1) a
LZ给的数据不够典型,他给的数据答案是2没错,用我原来错误但是很简单的方法(我在4搂发的)算出来也是2,但是用我后来给的数据算就是错的
你要试试我给的那组数据
其实这个连通图问题作可能出错的情况是下面这样的
字段1 字段2 字段3
e f 2
a b 1
c d 1
c d 2
这四条记录其实是相同的只算1条记录,可是一条语句很难算出来
1.只要字段1和字段2的值都相同,就认为记录是相同的。
2.只要字段3的值相同,就认为记录是相同的。
比如:
序号 字段1 字段2 字段3
1 e f 2
2 a b 1
3 c d 1
4 c d 2结果是1序号 字段1 字段2 字段3
1 a a 6
2 b c 2
3 b d 2
4 b e 2
5 b e 5
的结果是2
1 e f 2
2 a b 1
3 c d 1
4 c d 2结果是哪一条序号 字段1 字段2 字段3
1 a a 6
2 b c 2
3 b d 2
4 b e 2
5 b e 5
的结果是哪两条
用你上面的数据说就是
序号 字段1 字段2 字段3
1 e f 2
2 a b 1
3 c d 1
4 c d 2
四条记录都关联,所以答案是1
序号 字段1 字段2 字段3
1 a a 6
2 b c 2
3 b d 2
4 b e 2
5 b e 5
2、3、4、5关联,1独立,于是有2个连同区域,所以答案是2
谢谢hinco(桃色德鲁依).....LZ真会误倒人...
你怎么一下就看懂了~~郁闷~!!
create table #temp(序号 int, 字段char char(1), 字段int int)
insert #temp select 1, 'a', 6
union all select 2, 'b', 3
union all select 3, 'c', 2
union all select 4, 'd', 5
union all select 5, 'd', 6
union all select 7, 'f', 6
union all select 8, 'c', 9
union all select 9, 'a', 9select 字段int into #1 from #temp group by 字段int
select top 1 * into #2 from #1declare @count int
set @count = 0while(exists(select 1 from #1))
begin
set @count = @count+1
while(
exists(select 1
from #temp a where 字段char in
(select 字段char
from #temp where 字段int in(select 字段int from #2))
and not EXISTS(select 1 from #2 b where b.字段int=a.字段int))
)
begin
insert #2 select 字段int
from #temp a where 字段char in
(select 字段char
from #temp where 字段int in(select 字段int from #2))
and not EXISTS(select 1 from #2 b where b.字段int=a.字段int)
enddelete from #1
where 字段int in(select 字段int from #2)
delete #2
insert #2 select top 1 * from #1
enddrop table #1
drop table #2print @count
可是我觉得~猜到了就要能做到~~~
hinco(桃色德鲁依)~~以后我们要相互学习啊....
致敬!给出这么详细的解释!
我很早就已经给出答案了,看我2007-2-16 9:51:36回的贴,怎么就是没人看呢,汗……
在LZ发帖的第二天,我就把用存储过程递归的正确答案写出来了(第一天回的那个方法是错的,但第二天我自己更正了),可能LZ看那么长没去试验……