日期:2014-05-18  浏览次数:20449 次

帮忙写个SQL,谢谢
create table test(
a int,
b char,
c char
)

insert into test values(1,'a','b')

insert into test values(2,'a','b')

insert into test values(3,'b','c')

要得到如下结果(请考虑如果有多行相同的数据):
a b c
----------- ---- ----
1 a b
2 a b



------解决方案--------------------
SQL code

;with cte as (
select b,c,count(1) num from test
group by b,c having count(1)>1
)
select * from test t where exists(
select 1 from cte where b=t.b and c=t.c
)

------解决方案--------------------
SQL code
SELECT  A.*
FROM    test A
        INNER JOIN ( SELECT b ,
                            c
                     FROM   test
                     GROUP BY b ,
                            c
                     HAVING COUNT(1) > 1
                   ) T ON A.b = T.b
                          AND A.c = T.c