插入时循环/遍历日期范围 [英] Loop/Iterate through date range while inserting

查看:108
本文介绍了插入时循环/遍历日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我将数据以以下格式导入到表中(让我们称之为RAWDATA):

So I have data being imported into a table (let's call is RAWDATA) in the following format:

EMPID  | STARTDATE  | ENDDATE    | TOTALHOURS | TOTALWAGES
ABC123 | 01-01-2013 | 01-28-2013 | 160.0      | 1800.00
XYZ987 | 01-01-2013 | 01-31-2013 | 200.0      | 2500.00

我需要获取这些数据,并将其放在下面的另一个表(EMPDATA)中格式:

I need to take that data, and put it in a different table (EMPDATA) in the following format:

EMPID  | DATE       | HOURS | WAGES
ABC123 | 01-01-2013 | 5.71  | 64.29
ABC123 | 01-02-2013 | 5.71  | 64.29
ABC123 | 01-03-2013 | 5.71  | 64.29
...... | .......... | ....  | .....
XYZ987 | 01-01-2013 | 6.45  | 80.66
XYZ987 | 01-02-2013 | 6.45  | 80.66
XYZ987 | 01-03-2013 | 6.45  | 80.66
...... | .......... | ....  | .....

我的想法是在STARTDATE和ENDDATE之间执行DATEDIFF,以找出多少天(在这种情况下:28 )来分配工时和工资,然后每天插入一行,其中包含每天的平均工时和工资。所有这些都将通过RAWDATA表上的触发器来完成。我只是不确定如何在触发器中从STARTDATE迭代到ENDDATE。

My thought is to do a DATEDIFF between STARTDATE and ENDDATE to figure out how many days (in this case: 28) to spread the hours and wages over, then for each day, insert a row that contains the average hours and wages worked per day. This will all be done with a trigger on the RAWDATA table. I'm just not sure how to iterate from STARTDATE to ENDDATE in a trigger.

编辑:
我还应该指出,导入的数据并不总是每一行都有相同的开始/结束日期。我已经更新了第一个表示例来表明这一点。

I should also state that the data being imported does not always have the same start/end date for each row. I have updated the first table example to indicate this.

推荐答案


  1. 创建 date 表并使用 JOIN

  2. 计算 startdate 结束日期

  3. 划分总小时数总工资按计算的天数。

  1. Create a date table and use a JOIN.
  2. Calculate days between startdate and enddate
  3. Divide totalhours and totalwages by calculated days.

这是我的解决方案:

SELECT a.empid, b.dd AS date, 
  CAST(a.totalhours AS decimal) / (DATEDIFF(day, startdate, enddate) + 1) AS hours,
  CAST(a.totalwages AS decimal) / (DATEDIFF(day, startdate, enddate) + 1) AS wages
FROM wages a
INNER JOIN dates b ON dd BETWEEN a.startdate AND a.enddate

结果

|  EMPID |       DATE |         HOURS |          WAGES |
--------------------------------------------------------
| ABC123 | 2013-01-01 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-02 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-03 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-04 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-05 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-06 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-07 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-08 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-09 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-10 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-11 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-12 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-13 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-14 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-15 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-16 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-17 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-18 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-19 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-20 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-21 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-22 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-23 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-24 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-25 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-26 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-27 | 5.71428571428 | 64.28571428571 |
| ABC123 | 2013-01-28 | 5.71428571428 | 64.28571428571 |

查看示例

这篇关于插入时循环/遍历日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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