插入时循环/迭代日期范围

时间:2013-03-29 14:22:07

标签: sql sql-server triggers

所以我按以下格式将数据导入表中(让我们调用RAWDATA):

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)中:

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。

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

2 个答案:

答案 0 :(得分:5)

  1. 创建date表并使用JOIN
  2. 计算startdateenddate
  3. 之间的天数
  4. 按计算天数划分totalhourstotalwages
  5. 这是我的解决方案:

    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 |

    See the demo

答案 1 :(得分:0)

假设您使用的是SQL Server 2005及更高版本,则可以使用Recursive CTE来创建动态日期范围(使用查找表)。这在empid字段上使用GROUP BY,并为每个empid假定一行:

WITH RecCTE AS (
  SELECT empid, startdate
  FROM rawdata
  UNION ALL
  SELECT R.empid, DATEADD(day,1,R.startdate)
  FROM RecCTE R JOIN rawdata RD ON R.startdate < RD.enddate
  )
INSERT INTO EMPDATA 
SELECT R.EmpId, R.StartDate, T.TotalHours/R2.cnt, T.TotalWages/R2.cnt
FROM RecCTE R
  JOIN (SELECT empid, totalhours, totalwages
        FROM rawdata
       ) T ON R.empid = T.empid
  JOIN (SELECT EmpID, COUNT(*) cnt 
        FROM RecCTE 
        GROUP BY EmpID) R2 ON R.EmpID=R2.EmpId

SQL Fiddle Demo

可能有一种方法可以让它更有效率,但这应该让你朝着正确的方向前进。例如,您可以使用COUNT OVER函数返回计数与子查询:

WITH RecCTE AS (
  SELECT empid, startdate
  FROM rawdata
  UNION ALL
  SELECT R.empid, DATEADD(day,1,R.startdate)
  FROM RecCTE R JOIN rawdata RD ON R.startdate < RD.enddate
  )
INSERT INTO EMPDATA 
SELECT R.EmpId, R.StartDate, 
    T.TotalHours/COUNT(1) OVER (PARTITION BY R.EmpId), 
    T.TotalWages/COUNT(1) OVER (PARTITION BY R.EmpId)
FROM RecCTE R
  JOIN (SELECT empid, totalhours, totalwages
        FROM rawdata
       ) T ON R.empid = T.empid
ORDER BY R.StartDate