有A,B两张表,A表是打分员信息,B表是打分员打分详细,现在要求查询打分员打分情况,每个打分员随机抽查一名学生.A对应B是一对多的关系.现在A表连B表查询,但是只要求B中表一条记录连接就行了,不需要全部,用SQL能实现吗?怎么实现?
select * from
A inner join
(select row_Number() over(PARTITION BY 打分员 order by newid()) as index1,B.* from B) as b on a.打分员 = b.打分员 and index1 = 1
sql2005的你试一试
select * from (
select *,(select top 1 BID from B where 打分员 = A.打分员 order by newid()) as BID from A ) a inner join B on A.BID = B.BID
sql2000的
解决了,没有随机了,默认取了第一条,通过
select row_Number() over(PARTITION BY 打分员 order by newid()) as index1,B.* from B 给相同打分员的记录生成了index值,然后默认取了第一条,谢谢啦,学到东西啦!!!