日期:2014-05-19  浏览次数:20543 次

请教SQL语句。
有一个表table1
字段为
A,B,C
内容为
1,2,3
1,3,2
2,3,2
3,2,3
条件是:A相同B不相同
根据条件该表的结果是:
1,2,3
1,3,2
请问怎么写SQL语句,谢谢!

------解决方案--------------------
if object_id( 'pubs..tb ') is not null
drop table tb
go

create table tb(A int,B int,C int)

insert into tb(A,B,C) values(1,2,3)
insert into tb(A,B,C) values(1,3,2)
insert into tb(A,B,C) values(2,3,2)
insert into tb(A,B,C) values(3,2,3)

select A.* from
(select * from tb where A in (select A from tb group by A having count(*) > 1)) A,
(select A, B , max(C) C from(select * from tb where A in (select A from tb group by A having count(*) > 1)) t group by A ,B) B
where A.A = B.A and A.B = B.B and A.C = B.C

drop table tb
/*
A B C
----------- ----------- -----------
1 2 3
1 3 2

(所影响的行数为 2 行)
*/
------解决方案--------------------
Create Table table1
(A Int,
B Int,
C Int)
Insert table1 Select 1,2,3
Union All Select 1,3,2
Union All Select 2,3,2
Union All Select 3,2,3
GO
Select
Distinct
A.*
From
Table1 A
Inner Join
Table1 B
On A.A = B.A And A.B != B.B
GO
Drop Table Table1
--Result
/*
A B C
1 2 3
1 3 2
*/