表1
学校 姓名
002 张三
002 李桑
002 王大
002 赵2
......
001 A1
001 ZHAN
001 陆家
......
003 甲
003 丙
003 乙
.........
结果表
序号 学校 姓名
1 001 陆家
2 002 李桑
3 003 丙
4 001 A1
5 002 张三
6 003 甲
.........
30 xxx xxx
1 xxx xxx
2 xxx xxx
....
30 xxx xxx
1 xxx xxx
2 xxx xxx
......
30 xxx xxx
学校、学生随机取,一组每家学校取一名学生排好(每家学校都要取1名学生),30人为一组排好后取下一组,序号都是1至30,剩余的有几人算几人(学校、学生对应,不要重复取),直到所有学校取完为止,存入另一个数据库。(用于APS.NTE,不用LINQ,我不会)谢谢。
数据库
------解决方案--------------------
--試試以下:
if OBJECT_ID('tempdb..#temp', 'u') is not null drop table #temp;
go
create table #temp( [学校] varchar(100), [姓名] varchar(100));
insert #temp
select '002','张三' union all
select '002','李桑' union all
select '002','王大' union all
select '002','赵2' union all
select '001','A1' union all
select '001','ZHAN' union all
select '001','陆家' union all
select '003','甲' union all
select '003','丙' union all
select '003','乙'
;WITH a1 AS
(
select rowid1 = ROW_NUMBER() OVER(PARTITION BY 学校 ORDER BY NEWID()),* from #temp
)
,a2 AS
(
select rowid2 = ROW_NUMBER() OVER(PARTITION BY rowid1 ORDER BY NEWID()),* from a1
)
select rowid = ROW_NUMBER() OVER(PARTITION BY rowid1,(rowid2-1)/30 ORDER BY NEWID()),[学校],[姓名]
from a2
------解决方案--------------------
先是建表,插入数据:
if OBJECT_ID('tb') is not null
drop table tb;
go
create table tb( [学校] varchar(100), [姓名] varchar(100));
insert tb
select '002','张三' union all
select '002','李桑' union all
select '002','王大' union all
select '002','赵2' union all
select '001','A1' union all
select '001','ZHAN' union all
select '001','陆家' union all
select '003','甲' union all
select '003','丙' union all
select '003','乙' union all
select '002','张三2' union all
select '002','李桑2' union all
select '002','王大2' union all
select '002','赵22' union all
select '001','A12' union all
select '001','ZHAN2' union all
select '001','陆家2' union all
select '003','甲2' union all
select '003','丙2' union all
select '003','乙2' union all
select '002','张三3' union all
select '002','李桑3' union all
select '002','王大3' union all
select '002','赵23' union all
select '001','A13' union all
select '001','ZHAN3' union all
select '001','陆家3' union all
select '003','甲3' union all
select '003','丙3' union all
select '003','乙3' union all
select '001','陆家123' union all
select '003','甲123' union all
select '001','陆家1234' union all
select '003','甲1234' union all
select '002','张三' union all
select '002','李桑' union all
select '002','王大' union all
select '002','赵2' union all
select '001','A1' union all
select '001','ZHAN' union all
select '001','陆家' union all
select '003','甲' union all
select '003','丙' union all
select '003','乙' union all