求SQL一语句
在一个表中有A,B,C三个字段,例如:
A B C
1 * 2
1 $ 2
2 Y F
2 T F
3 1 R
我要的结果是A和C中有重复的记录可以随便取一条出来,但只要一条,我需要的结果例如:
A B C
1 * 2
2 T F
3 1 R
[解决办法]
;with t
as(
select px=row_number()over(partition by a,c order by getdate()),* from tb
)
select A,B,C from t where px=1
with tb(A,B,C)
as(
select 1,'*','2' union all
select 1,'$','2' union all
select 2,'Y','F' union all
select 2,'T','F' union all
select 3,'1','R'
)
select a,(select top 1 b.b from tb b where a.a=b.a and a.c=b.c order by newid())b,c from tb a group by a,c
select * from tb a where not exists(select 1 from tb b where a.col1=b.col1 and a.col3=b.col3 and a.col2>b.col2)