服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Sql Server - 随机抽取的sql语句 每班任意抽取3名学生

随机抽取的sql语句 每班任意抽取3名学生

2020-03-26 15:35whsnow Sql Server

表 student ,字段 class 及name 。其中class 表示班级,name 表示每班学生姓名。要求每班任意抽取3名学生出来,进行学校大扫除

学校有一、二、三。。。。至十班。
假设每个班上有30名学生。张、李、刘、苏等
现有这样的表 student ,字段 class 及name 。其中class 表示班级,name 表示每班学生姓名。
要求每班任意抽取3名学生出来,进行学校大扫除。

请写出这样的SQL语句。

?
1
2
3
4
select name
from (select (row_number() over (partition by class order by checksum(newid()))) id,*
from student ) a
where id<=3

 

延伸 · 阅读

精彩推荐