读书人

求SQL一语句,该如何处理

发布时间: 2013-10-21 17:03:30 作者: rapoo

求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

[解决办法]
select * from tb t
where not exists(select 1 from tb A=t.A and C=t.C and B>T.b)
[解决办法]
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)

读书人网 >SQL Server

热点推荐