0

我正在尝试为每个半月支付期和半月工资单为多个项目的数百名员工建立一个表格。我从ERP系统收到的行数据格式如下:

名称、项目、负担金额(所有期间的总工资)、calcbegin(日期)、calend(日期)

我想建立一个表格,将每一行复制 n 次(n 是 calcbegin 和 calcend 之间的半月支付期数)。

我的问题是如何生成范围之间的半月日期​​,然后使用 mssql 2008 生成包含每个日期和相关信息的行。

这是一个数据样本。我认为 CTE 是要走的路,但我不知道如何让它发挥作用。如果我能让 CTE 正常工作,我可以对日期感到不安。

Existing Data                   
Name    Amount   Per amt    calcbegin   calcend Pay Periods
Name1   5000     2,500  4/15/2013   5/15/2013   2
Name2   10000    2,000  4/15/2013   6/30/2013   5

Results Requested                   
Name    Amount   Pay amt    calcbegin   calcend Payperiods
1-Name1 5000     2,500  04/15/13    4/30/2013   1
2-Name1 5000     2,500  05/01/13    5/15/2013   1

1-Name2 10000    2,000  04/15/13    4/30/2013   1
2-Name2 10000    2,000  05/01/13    5/15/2013   1
3-Name2 10000    2,000  05/16/13    5/31/2013   1
4-Name2 10000    2,000  06/01/13    6/15/2013   1
5-Name2 10000    2,000  06/16/13    6/30/2013   1
4

1 回答 1

0

非常感谢 Justin Stolle,SQL: Turn a Date Range into a List of Dates found with Google。扩展他的示例,我能够构建一个正常工作的递归 CTE。现在调整日期

WITH cte (id, b, a_p, n,e ,pp, a, ae) AS 
          (SELECT        Range_ID as id
                        ,DATEADD(wk, 2,[calc begin]) AS B
                        ,[Activity/Project] AS a_p 
                        ,Name as n
                        ,[Calc End] as E
                        ,DATEDIFF(w,[calc begin],[Calc End])/14 as pp
                        ,([Salary Encumbrance] + [Benefits Encumbrance])/(DATEDIFF(w,[calc begin],[Calc End])/14) as a
                        ,[Account Executive] as ae
               FROM EncumReport
           WHERE [Account Executive] like '%25%' 

           UNION ALL
           SELECT    Range_Id as id
                    ,(DATEADD(wk, 2, cte.b)) AS b
                    ,[activity/project] as a_p 
                    ,Name as n
                    ,[Calc End] as E
                    ,DATEDIFF(w,[calc begin],[Calc End])/14 as pp
                    ,([Salary Encumbrance] + [Benefits Encumbrance])/(DATEDIFF(w,[calc begin],[Calc End])/14) as a
                    ,[Account Executive] as ae
               FROM cte
               INNER JOIN encumreport
                      ON cte.id = range_id
                WHERE cte.b < [calc end]-7)

     SELECT id AS range_id  
           ,b AS date_within_range,a_p as Projects,  n as Name,e as EndDate, pp as PayPeriods, a as Amounts,ae as AccountExecutive  
      FROM cte
     ORDER BY id, b
于 2013-04-25T21:35:22.467 回答