SQLServer和Oracle的随机关系对应
有需求如下: 现在要补齐tb1中演唱歌曲字段。条件是去tb2中查找相同艺人演唱过的歌曲,随机填充到tb1中的歌曲名字段 tb1 tb2 艺人 演唱歌曲名? 艺人 演唱歌曲名称 ========================================================= 一、最终SQL结果 1、sqlserver的实现:
create table tb1( id varchar(60),--需要表的主键 yr varchar(20),ycgqm varchar(50) ) create table tb2( id varchar(60),--表的主键(可以没有) yr varchar(20),ycgqm varchar(50) ) insert into tb1(id,yr,ycgqm) values(newid(),'a',null); insert into tb1(id,'b','e','s','d',null); insert into tb2(id,'aa'); insert into tb2(id,'ab'); insert into tb2(id,'bb'); insert into tb2(id,'ba'); insert into tb2(id,'bbb'); insert into tb2(id,'dd'); insert into tb2(id,'d2'); insert into tb2(id,'f','ddd'); insert into tb2(id,'c','cc'); insert into tb2(id,'ac'); update tb1 set ycgqm= ( select bycgqm from ( select * from ( select t.*,ROW_NUMBER() OVER(PARTITION BY anumyr ORDER BY bycgqm) AS tnum from ( select b.*,a.*,cast(anum as varchar(20))+ ayr as anumyr from ( select id as arid,a.yr as ayr,a.ycgqm as aycgqm,ROW_NUMBER() OVER(PARTITION BY yr ORDER BY yr) AS anum from tb1 a ) a,( select id as brid,b.yr as byr,b.ycgqm as bycgqm from tb2 b ) b where ayr = byr ) t ) t where anum=tnum ) t WHERE arid=tb1.id ) 2、oracle的实现:
create table tb1( yr varchar(20),ycgqm varchar(50) ) create table tb2( yr varchar(20),ycgqm varchar(50) ) select * from tb1 insert into tb1(yr,ycgqm) values('a',null); insert into tb1(yr,ycgqm) values('b',ycgqm) values('e',ycgqm) values('s',ycgqm) values('d',null); insert into tb2(yr,'aa'); insert into tb2(yr,'ab'); insert into tb2(yr,'bb'); insert into tb2(yr,'ba'); insert into tb2(yr,'bbb'); insert into tb2(yr,'dd'); insert into tb2(yr,'d2'); insert into tb2(yr,ycgqm) values('f','ddd'); insert into tb2(yr,ycgqm) values('c','cc'); insert into tb2(yr,'ac'); update tb1 set ycgqm= ( select bycgqm from ( select * from ( select rownum r,t.*,(select count(*) from tb1 where tb1.yr=t.ayr) as cnt,ROW_NUMBER() OVER(PARTITION BY anumyr ORDER BY bycgqm) AS tnum from ( select b.*,anum || ayr as anumyr from ( select rowid as arid,rownum || 'a' as ra,ROW_NUMBER() OVER(PARTITION BY yr ORDER BY yr) AS anum from tb1 a ) a,( select rowid as brid,rownum || 'b' as rb,b.ycgqm as bycgqm from tb2 b ) b where ayr = byr order by ayr ) t order by byr,anum,tnum ) where anum=tnum ) WHERE arid=tb1.rowid ) 二、实现思路 整个思路关键在于tb1中的多个歌手需要随机填写tb2中的歌手对应的歌曲,而且不重复。对于这点,第一想到随机,rand,但这没法保证不重复。于是想到方法 1、对tb1中根据歌手分组,每个歌手有多条记录,则按歌手内记录顺序编号,也就是第一个歌手如果有2条记录,则为1,2,第二个歌手有3条记录,则为1,2,3,这也就是对应第一层SQL 2、对tb1和tb2做笛卡尔积,形成矩阵表(效率是值得斟酌的,如果数据量大,那必须抛弃了),根据结果,按歌手分组,按歌曲在歌手内顺序编号。 3、取歌手顺序和歌曲顺序相等的记录。这是因为如果有1个歌手,在tb1中有3条记录,那么编号是1,3,按歌曲编号后,每条记录对应3个歌曲,也就是笛卡尔积后生成3条记录,这记录编号也是1,3排序,而且每条记录的编号排序规则是一样的。所以第一条记录取第一首歌曲,第二条记录取第二首歌曲,依次类推,只要歌曲数多肯定不会重复 4、根据主键或者rowid最终定位没条tb1的记录位置,便于update 三、最后补充一种评论中的思路,因为SQL太长,评论中不让贴,这个是随机获取歌手下的歌曲
update tb1 set ycgqm=( select bycgqm from ( select t3.id,(--根据tb1中生成的随机数取歌曲 select ycgqm from ( select b.*,ROW_NUMBER() OVER(PARTITION BY yr ORDER BY ycgqm) AS tnum from tb2 b ) b2 where b2.yr=t3.yr and b2.tnum = t3.rndnum ) as bycgqm from (--根据tb1中取歌手名称下哪首歌的行号 select t2.*,cast(ceiling(rand(checksum(newid()))*gqtotalnum) as int) as rndnum from ( select t.*,(select count(*) from tb2 where yr=t.yr) as gqtotalnum from tb1 t ) t2 ) t3 ) t4 where t4.id = tb1.id ) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |