处理随机数?关于考勤资料的。

911853 2003-10-17 09:17:38
1: 工作日历 calendar([date] smalldatetime,IsWorkday [bit] NOT NULL ) --注:已经过简化处理
一年365天的每一天一笔资料,date为日期,IsWorkday表示是否上班,1表示上班,0表示不上班

2: 员工信息:workers(workNo char(6))

3:问题: (不考虑排班复杂性和漏打卡之类的异常)
现在需加入所有人9月份每天(需上班时间) 07:30-08:00 之间的一笔打卡资料(当然是假的)
语句如下:
insert tableName (workno,[date],[time])
Select workNo,[date],'07:21' from calendar,workers
where convert(char(6),[date],112) = '200309' and IsWorkday = 1 -- 应上班日期

当然这样做出来的打卡时间也太假了,所有人每天的都是07:21,能不能将它变成随机的?
1:同一个人每天打卡时间尽量随机
2:不同的人同一天打卡时间尽量随机
(写法不限,只要能达到目的即可
1:如上写将'07:21'换成随机的
2:先如上写法加入新表,再将时间改成随机的)
------------------------------------------------------------------
简单一点就是如下语句中的'07:21'如何改成随机的。
insert tableName (workno,[date],[time])
Select workNo,[date],'07:21' from calendar,workers
where convert(char(6),[date],112) = '200309' and IsWorkday = 1 -- 应上班日期




...全文
63 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2003-10-17
  • 打赏
  • 举报
回复
注意,速度比较慢,因为我的处理方法可以一次性最多处理生成:100*100*100条记录.

如果根本不会生成这这么多记录,可以调整
declare @sql varchar(8000)
set @sql='insert into #tb(dt) select top '+cast(@r as varchar)+'
dateadd(ss,rand(a.id)*3000,''07:30'')
from(select top 100 id from sysobjects) a, --减少top 100的值
(select top 100 id from sysobjects) b, --减少top 100的值
(select top 100 id from sysobjects) c --减少top 100的值,或取消
order by newid()'
911853 2003-10-17
  • 打赏
  • 举报
回复
想不到这么快就可以结贴了,多谢。
zjcxc 2003-10-17
  • 打赏
  • 举报
回复
--显示结果
select id,workno,[date]=convert(varchar(10),[date],120),[time]=convert(varchar(5),[time],108) from @打卡资料表

--我的电脑上的结果:
id workno date time
----------- ---------- ---------- -----
1 01 2003-01-01 08:06
2 02 2003-01-01 07:37
3 03 2003-01-01 08:04
4 04 2003-01-01 08:04
5 05 2003-01-01 08:05
6 06 2003-01-01 08:05
7 07 2003-01-01 07:36

(所影响的行数为 7 行)
txlicenhe 2003-10-17
  • 打赏
  • 举报
回复
邹建的办法好。

dateadd(minute,rand(a.id)*30,'07:30') 或
dateadd(second,rand(a.id)*1800,'07:30') 来控制 时间在07:30-08:00之间


zjcxc 2003-10-17
  • 打赏
  • 举报
回复
--要控制时间在7:30-8:00,改生成随机时间的部分.
--生成随机时间
declare @sql varchar(8000)
set @sql='insert into #tb(dt) select top '+cast(@r as varchar)+'
dateadd(ss,rand(a.id)*3000,''07:30'')
from(select top 100 id from sysobjects) a,
(select top 100 id from sysobjects) b,
(select top 100 id from sysobjects) c
order by newid()
zjcxc 2003-10-17
  • 打赏
  • 举报
回复
--例子:

--创建打卡资料表
declare @打卡资料表 table(id int identity(1,1),workno varchar(10),date datetime,time datetime)

declare @r int
insert into @打卡资料表
/*--先按固定时间插入资料,我这里改为直接插入
Select workNo,[date],'07:21' from calendar,workers
where convert(char(6),[date],112) = '200309' and IsWorkday = 1 -- 应上班日期
--*/
select '01','2003-01-01','07:21'
union all select '02','2003-01-01','07:21'
union all select '03','2003-01-01','07:21'
union all select '04','2003-01-01','07:21'
union all select '05','2003-01-01','07:21'
union all select '06','2003-01-01','07:21'
union all select '07','2003-01-01','07:21'

--得到要处理的记录数
set @r=@@rowcount

--创建得到随机时间的临时表
create table #tb(id int identity(1,1),dt datetime)

--生成随机时间
declare @sql varchar(8000)
set @sql='insert into #tb(dt) select top '+cast(@r as varchar)+'
dateadd(ss,rand(a.id)*10000,''07:21'')
from(select top 100 id from sysobjects) a,
(select top 100 id from sysobjects) b,
(select top 100 id from sysobjects) c
order by newid()
'
exec(@sql)

--更新时间
update @打卡资料表 set time=b.dt
from @打卡资料表 a inner join #tb b on a.id=b.id

--显示结果
select * from @打卡资料表


--删除临时表
drop table #tb

911853 2003-10-17
  • 打赏
  • 举报
回复
不好意思,是07:30-08:00之间。
911853 2003-10-17
  • 打赏
  • 举报
回复
邹建的办法差不多了。 还要控制在07:00-08:00之间。
911853 2003-10-17
  • 打赏
  • 举报
回复
07+randome(30)肯定是不行的,只不过是将'07:21'改成了另外一个数罢了,也还是全相同。
除非放在循环中,即一楼的办法。
zjcxc 2003-10-17
  • 打赏
  • 举报
回复
--得到随机时间列表
declare @tb table(id int identity(1,1),dt datetime)
insert into @tb(dt)

select top 10000 dateadd(ss,rand(id)*10000,'07:21') from sysobjects order by newid()

select * from @tb
911853 2003-10-17
  • 打赏
  • 举报
回复
楼上方法可行,但我的资料太多,有没有更好的办法? 14:00 前结贴
DelphiDu 2003-10-17
  • 打赏
  • 举报
回复
提醒﹕07+randome(30)即可
txlicenhe 2003-10-17
  • 打赏
  • 举报
回复
try:
1:先生成结果表再改
id workno date time
1 wn1 20030901 07:21
2 wn1 20030902 07:21
...
22 wn1 20030930 07:21
23 wn2 20030901 07:21
24 wn2 20030902 07:21
...
44 wn2 20030930 07:21

再用游标循环,在循环中用30*rand()来代替后两位。
当然效率比较差一点,看有没有更好的办法。

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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