生成工作时间表 [英] Generating working schedules

查看:111
本文介绍了生成工作时间表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







希望有人可以帮助我(T-SQL的新手):



我想根据用户可以输入的开始日期为员工提前x天的工作时间表。



我有3个相关的表格:



1. 表X 有(1)资源编号,(2)开始日期工作时间表(3)表示开始日期的日期数(这是ISO因此为星期一,2为星期二等)



2. 表Y 有自己的时间表,可以保留7天的时间表或14天的时间表。在7天时间表的情况下,存在14(!)记录,其中(1)资源编号,(2)日数,(3)从上午开始时间(4)结束时间a.m(5)从上午开始时间和(6)结束时间下午。如果是14天的时间表,则有28条记录(上午和下午记录)



3. 表Z 包含资源数据。



澄清一个例子(伪造员工100):

表X

资源:100

开始日期:2012-03-01(从此日期开始计划生效)

Daynumber:4(2012-03-01是一个周四)



表Y (资源有14天的时间表,因为每周2周是休息日):

记录1 显示:

资源:100,

日数:1(=星期一,工作日),

AM-开始时间:09:00,

AM-结束时间:13:00,

PM-开始时间:13:30,

PM结束时间:17:30


记录2 相同但是daynumber是2

记录3 相同但是daynumber是3等。

...

...

记录8 显示:

资源:100,

Daynumber:8(=星期一, off-day ),

AM-开始时间:00:00 ,

AM-结束时间:00:00,

PM-开始时间:00:00,

PM结束时间:00: 00


记录9 与记录2相同,但是daynumber是9。

...

...

记录14 与记录7相同但是日期是14(=该员工的最后一天)



周末时间显示为00:00(与示例中的第8天相同)





我根据开始日期和前几天的x天数生成了CROSS APPLY功能的工作时间表。

然后我评估相应的实际日期数与表Y中的日期数相同的日期。

这可以在7天的时间表内正常工作,但我无法通过14天的时间表修复它。该日程表中的第8天代表实际的第1天,但我怎么知道第8天的实际日期...我想我必须从表X中的开始日期开始...



我认为理想情况下我希望生成的日期如下(例如,如果提前30天开始2014-05-01的14天时间表的话):

2014-05-01 =第4天(=实际日期数字)

2014-05-02 =第5天

2014-05-03 =第6天

...

2014-05-10 =第13天

2014-05-11 =第14天

2014-05 -12 =第1天

2014-05-13 =第2天

2014-05-14 =第3天

...

2014-05-24 =第13天

2014-05-25 =第14天

2014-05-26 =第1天

2014-05-27 =第2天

...

2014-05-31 =第6天



完成后我可以将实际的日数与表Y中的日数进行比较。

CROSS APPLY函数生成的rownumber必须在第14天后重置为1。尝试了在ROW_NUMBER函数中的PARTITION BY,但没有用...我可以分区的唯一字段是daynumber的最大值(14是示例)但rownumber函数中不允许这样做。



我想我必须从头开始重做整个练习,但我想知道解决这个问题的最佳方法是什么。

我被卡住了!





谢谢!



PS

我只是在使用CROSS APPLY时读取PARTITION BY没有任何效果,因为CROSS APPLY在行到行的基础上工作。尽管如此,我仍然被卡住了......

Hi,


Hope someone can help me (a novice on T-SQL) with this:

I would like to generate a working schedule for employees for x-days ahead based on a starting date that the user can enter.

I have got 3 relevant tables:

1. Table X with (1) resourcenumber, (2) starting date working schedule and (3) the daynumber representing the starting date (this is ISO so 1 for Monday, 2 for Tuesday etc.)

2. Table Y has the schedule itself and can hold a 7-days schedule or a 14-days schedule. In case of 7 days schedule there a 14 (!) records with (1) resourcenumber, (2) daynumber, (3) starting hour a.m. (4) ending hour a.m (5) starting hour p.m and (6) ending hour p.m. In case of a 14-days schedule there are 28 records (a.m. and p.m. records)

3. Table Z with resource data.

An example to clarify (for fake employee 100):
Table X:
Resource: 100
Starting date: 2012-03-01 (from this date the schedule will be effective)
Daynumber: 4 (2012-03-01 was a Thursday)

Table Y (Resource has a 14 days schedule because per 2 weeks Monday is an off-day):
Record 1 shows:
Resource: 100,
Daynumber: 1 (= Monday, working day),
AM-Starting hour: 09:00,
AM-Ending hour: 13:00,
PM-starting hour: 13:30,
PM-ending hour: 17:30

Record 2: same but daynumber is 2
Record 3: same but daynumber is 3 etc.
...
...
Record 8 shows:
Resource: 100,
Daynumber: 8 (= Monday, off-day),
AM-Starting hour: 00:00,
AM-Ending hour: 00:00,
PM-starting hour: 00:00,
PM-ending hour: 00:00

Record 9: same as record 2 but daynumber is 9.
...
...
Record 14: same as record 7 but day is 14 (= last day for this employee)

The weekend days show as 00:00 for the hours (same as day 8 in example)


I generated the working schedule with a CROSS APPLY function based on the starting date and the x-number of days ahead.
I then evaluate the actual daynumber corresponding with that date with the daynumber in table Y.
That works fine with a 7-days schedule but I can't get it fixed with a 14-days schedule. Day 8 in that schedule represents an actual day 1 but how do I know what actual date day 8 is ... I think I have to start with the starting date in table X ...

I think ideally I would like to have the generated days as follows (as an example in case of a 14-days schedule starting 2014-05-01 for 30 days ahead):
2014-05-01 = day 4 (= actual daynumber)
2014-05-02 = day 5
2014-05-03 = day 6
...
2014-05-10 = day 13
2014-05-11 = day 14
2014-05-12 = day 1
2014-05-13 = day 2
2014-05-14 = day 3
...
2014-05-24 = day 13
2014-05-25 = day 14
2014-05-26 = day 1
2014-05-27 = day 2
...
2014-05-31 = day 6

With this done I can compare the actual daynumber with the daynumber in Table Y.
The rownumber that the CROSS APPLY function generates has to be reset to 1 after day 14. I tried PARTITION BY in THE ROW_NUMBER function but to no avail ... The only field I can partition by is the maximum value of the daynumber (14 is the example) but that is not allowed in the rownumber function.

I think I have to redo the whole exercise from scratch, but I wonder what the best way is to get this solved.
I am stuck!


Thanks!

P.S.
I just read that PARTITION BY does not have any effect when using CROSS APPLY because CROSS APPLY works on a row-to-row basis. Despite this I am still stuck ...

推荐答案

这篇关于生成工作时间表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆