求SQL:随机函数应用

HuoLarry 2014-02-16 08:48:04
客户表T(6W行),CUS_ID唯一;
CUS_ID,CUS_MOBILE
2001,138123456
2002,138456789
2003,132111111
2004,134124574
2005,138123456
2006,135478956
2007,137478956

客户经理表,有145个客户经理(145行):CM

CM_NAME,
张三丰
李四
王五


业务逻辑是,新的一年需要随机为客户经理平均分配一批客户(当然像例子中的张三丰多了一个,很正常)。
所以需要的得到一张新表,这张新表为New_TB,随机为这三个客户经理分配三个客户ID:

结果表如下:
CUS_ID,CUS_MOBILE,CM_NAME
2001,138123456,张三丰
2002,138456789,张三丰
2003,132111111,李四
2004,134124574,李四
2005,138123456,王五
2006,135478956,王五
2007,137478956,张三丰
...全文
115 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
LongRui888 2014-02-17
  • 打赏
  • 举报
回复
引用 6 楼 19850713 的回复:
[quote=引用 4 楼 19850713 的回复:] [quote=引用 3 楼 ap0405140 的回复:]

create table 客户表
(CUS_ID int,CUS_MOBILE varchar(20))

insert into 客户表
 select 2001,'138123456' union all
 select 2002,'138456789' union all
 select 2003,'132111111' union all
 select 2004,'134124574' union all
 select 2005,'138123456' union all
 select 2006,'135478956' union all
 select 2007,'137478956'

create table 客户经理表
(CM_NAME varchar(10))

insert into 客户经理表
 select '张三丰' union all
 select '李四' union all
 select '王五'


select d.CUS_ID,d.CUS_MOBILE,e.CM_NAME
from
(select CUS_ID,CUS_MOBILE,row_number() over(order by getdate()) 'rn' from 客户表) d
left join
(select CM_NAME,row_number() over(order by newid()) 'rn'
 from
 (select a.CM_NAME 
   from 客户经理表 a
   cross join
   (select number from master.dbo.spt_values
    where type='P' and number>=1 
    and number<=((select count(1) from 客户表)/(select count(1) from 客户经理表))) b
  union all
  select top ((select count(1) from 客户表)
              -(select count(1) from 客户经理表)*((select count(1) from 客户表)/(select count(1) from 客户经理表))) CM_NAME
   from 客户经理表 c
   order by newid()) t) e on d.rn=e.rn

/*
CUS_ID      CUS_MOBILE           CM_NAME
----------- -------------------- ----------
2001        138123456            李四
2002        138456789            王五
2003        132111111            李四
2004        134124574            张三丰
2005        138123456            李四
2006        135478956            王五
2007        137478956            张三丰

(7 row(s) affected)
*/
对的,每次执行都不同,达到公平。感谢!!感谢版主们的热情回答!!![/quote] 补充一句,如果想要把得到的结果表,存储为临时表,into ##TEMP_A 应该加在哪里?[/quote] 对的, select * into ##TEMP_A from xxx 这个##xxx是全局临时表,如果是局部临时表就是#xxx
HuoLarry 2014-02-17
  • 打赏
  • 举报
回复
引用 4 楼 19850713 的回复:
[quote=引用 3 楼 ap0405140 的回复:]

create table 客户表
(CUS_ID int,CUS_MOBILE varchar(20))

insert into 客户表
 select 2001,'138123456' union all
 select 2002,'138456789' union all
 select 2003,'132111111' union all
 select 2004,'134124574' union all
 select 2005,'138123456' union all
 select 2006,'135478956' union all
 select 2007,'137478956'

create table 客户经理表
(CM_NAME varchar(10))

insert into 客户经理表
 select '张三丰' union all
 select '李四' union all
 select '王五'


select d.CUS_ID,d.CUS_MOBILE,e.CM_NAME
from
(select CUS_ID,CUS_MOBILE,row_number() over(order by getdate()) 'rn' from 客户表) d
left join
(select CM_NAME,row_number() over(order by newid()) 'rn'
 from
 (select a.CM_NAME 
   from 客户经理表 a
   cross join
   (select number from master.dbo.spt_values
    where type='P' and number>=1 
    and number<=((select count(1) from 客户表)/(select count(1) from 客户经理表))) b
  union all
  select top ((select count(1) from 客户表)
              -(select count(1) from 客户经理表)*((select count(1) from 客户表)/(select count(1) from 客户经理表))) CM_NAME
   from 客户经理表 c
   order by newid()) t) e on d.rn=e.rn

/*
CUS_ID      CUS_MOBILE           CM_NAME
----------- -------------------- ----------
2001        138123456            李四
2002        138456789            王五
2003        132111111            李四
2004        134124574            张三丰
2005        138123456            李四
2006        135478956            王五
2007        137478956            张三丰

(7 row(s) affected)
*/
对的,每次执行都不同,达到公平。感谢!!感谢版主们的热情回答!!![/quote] 补充一句,如果想要把得到的结果表,存储为临时表,into ##TEMP_A 应该加在哪里?
HuoLarry 2014-02-16
  • 打赏
  • 举报
回复
自己孤陋寡闻,需要多学习,很多函数都看不懂。表示对不起CSND,在这里发帖请各位版主多多包涵!!以后在研究每个字母的意思。 已经经过验证,可以得到我要的结果!!!
HuoLarry 2014-02-16
  • 打赏
  • 举报
回复
引用 3 楼 ap0405140 的回复:

create table 客户表
(CUS_ID int,CUS_MOBILE varchar(20))

insert into 客户表
 select 2001,'138123456' union all
 select 2002,'138456789' union all
 select 2003,'132111111' union all
 select 2004,'134124574' union all
 select 2005,'138123456' union all
 select 2006,'135478956' union all
 select 2007,'137478956'

create table 客户经理表
(CM_NAME varchar(10))

insert into 客户经理表
 select '张三丰' union all
 select '李四' union all
 select '王五'


select d.CUS_ID,d.CUS_MOBILE,e.CM_NAME
from
(select CUS_ID,CUS_MOBILE,row_number() over(order by getdate()) 'rn' from 客户表) d
left join
(select CM_NAME,row_number() over(order by newid()) 'rn'
 from
 (select a.CM_NAME 
   from 客户经理表 a
   cross join
   (select number from master.dbo.spt_values
    where type='P' and number>=1 
    and number<=((select count(1) from 客户表)/(select count(1) from 客户经理表))) b
  union all
  select top ((select count(1) from 客户表)
              -(select count(1) from 客户经理表)*((select count(1) from 客户表)/(select count(1) from 客户经理表))) CM_NAME
   from 客户经理表 c
   order by newid()) t) e on d.rn=e.rn

/*
CUS_ID      CUS_MOBILE           CM_NAME
----------- -------------------- ----------
2001        138123456            李四
2002        138456789            王五
2003        132111111            李四
2004        134124574            张三丰
2005        138123456            李四
2006        135478956            王五
2007        137478956            张三丰

(7 row(s) affected)
*/
对的,每次执行都不同,达到公平。感谢!!感谢版主们的热情回答!!!
唐诗三百首 2014-02-16
  • 打赏
  • 举报
回复

create table 客户表
(CUS_ID int,CUS_MOBILE varchar(20))

insert into 客户表
 select 2001,'138123456' union all
 select 2002,'138456789' union all
 select 2003,'132111111' union all
 select 2004,'134124574' union all
 select 2005,'138123456' union all
 select 2006,'135478956' union all
 select 2007,'137478956'

create table 客户经理表
(CM_NAME varchar(10))

insert into 客户经理表
 select '张三丰' union all
 select '李四' union all
 select '王五'


select d.CUS_ID,d.CUS_MOBILE,e.CM_NAME
from
(select CUS_ID,CUS_MOBILE,row_number() over(order by getdate()) 'rn' from 客户表) d
left join
(select CM_NAME,row_number() over(order by newid()) 'rn'
 from
 (select a.CM_NAME 
   from 客户经理表 a
   cross join
   (select number from master.dbo.spt_values
    where type='P' and number>=1 
    and number<=((select count(1) from 客户表)/(select count(1) from 客户经理表))) b
  union all
  select top ((select count(1) from 客户表)
              -(select count(1) from 客户经理表)*((select count(1) from 客户表)/(select count(1) from 客户经理表))) CM_NAME
   from 客户经理表 c
   order by newid()) t) e on d.rn=e.rn

/*
CUS_ID      CUS_MOBILE           CM_NAME
----------- -------------------- ----------
2001        138123456            李四
2002        138456789            王五
2003        132111111            李四
2004        134124574            张三丰
2005        138123456            李四
2006        135478956            王五
2007        137478956            张三丰

(7 row(s) affected)
*/
LongRui888 2014-02-16
  • 打赏
  • 举报
回复
修改一下:

create table 客户表T(CUS_ID int,CUS_MOBILE varchar(15))

insert into 客户表T
select 2001,'138123456' union all
select 2002,'138456789' union all
select 2003,'132111111' union all
select 2004,'134124574' union all
select 2005,'138123456' union all
select 2006,'135478956' union all
select 2007,'137478956'


create table 客户经理表(CM_NAME varchar(10))

insert into 客户经理表
select '张三丰' union all
select '李四' union all
select '王五'
go

;with t
as
(
select *,
       ROW_NUMBER() over(order by getdate()) rownum
from 客户表T
),

tt
as
(
select a.*,
       b.c,
       ROW_NUMBER() over(order by newid()) rownum
from 客户经理表 a
inner join (select COUNT(*) as c from 客户经理表) b
        on 1 = 1
)

select t.CUS_ID,t.CUS_MOBILE,tt.CM_NAME
from t
inner join tt 
        on t.rownum % tt.c = tt.rownum - 1

/*
CUS_ID	CUS_MOBILE	CM_NAME
2003	132111111	李四
2006	135478956	李四
2001	138123456	王五
2004	134124574	王五
2007	137478956	王五
2002	138456789	张三丰
2005	138123456	张三丰
*/
LongRui888 2014-02-16
  • 打赏
  • 举报
回复
是这样吗,每次执行结果都不同:

create table 客户表T(CUS_ID int,CUS_MOBILE varchar(15))

insert into 客户表T
select 2001,'138123456' union all
select 2002,'138456789' union all
select 2003,'132111111' union all
select 2004,'134124574' union all
select 2005,'138123456' union all
select 2006,'135478956' union all
select 2007,'137478956'


create table 客户经理表(CM_NAME varchar(10))

insert into 客户经理表
select '张三丰' union all
select '李四' union all
select '王五'
go

;with t
as
(
select *,
       ROW_NUMBER() over(order by newid()) rownum
from 客户表T
),

tt
as
(
select a.*,
       b.c,
       ROW_NUMBER() over(order by newid()) rownum
from 客户经理表 a
inner join (select COUNT(*) as c from 客户经理表) b
        on 1 = 1
)

select t.CUS_ID,t.CUS_MOBILE,tt.CM_NAME
from t
inner join tt 
        on t.rownum % tt.c = tt.rownum - 1
/*
CUS_ID	CUS_MOBILE	CM_NAME
2001	138123456	王五
2004	134124574	王五
2005	138123456	张三丰
2004	134124574	张三丰
2006	135478956	张三丰
2004	134124574	李四
2003	132111111	李四
*/

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧