我正在尝试为每个半月付款期间建立一个表格,并在多个项目中为每个数百名员工创建半月工资额。我从ERP系统收到的行数据格式如下:
姓名,项目,担保金额(所有期间的总薪资),calcbegin(日期),日期(日期)
我想构建一个表格,它会复制每一行n次(n是calcbegin和calcend之间半月的payperiods的数量)。
我的问题是如何在范围之间生成半月日期,然后使用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
答案 0 :(得分:0)
非常感谢Justin Stolle,SQL:将日期范围转换为与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