SQL Server - 将记录超过一个月的记录分成一个月

时间:2017-02-27 14:20:06

标签: sql-server tsql

我们说我有下表:

ID | user | from       | to         | loan
1  | BB   | 01/01/2016 | 01/05/2016 | 50
2  | AD   | 01/01/2016 | 01/03/2016 | 25
3  | AD   | 01/03/2016 | 17/05/2016 | 30

此表中的loan是每月一次。因此,用户BB从2016年1月1日到2016年5月1日每月赚50个硬币。我想把它分成以下记录:

 ID | user | from       | to         | loan
 1  | BB   | 01/01/2016 | 01/02/2016 | 50
 1  | BB   | 01/02/2016 | 01/03/2016 | 50
 1  | BB   | 01/03/2016 | 01/04/2016 | 50
 1  | BB   | 01/04/2016 | 01/05/2016 | 50
 2  | AD   | 01/01/2016 | 01/02/2016 | 25
 2  | AD   | 01/02/2016 | 01/03/2016 | 25
 3  | AD   | 01/03/2016 | 01/04/2016 | 30
 3  | AD   | 01/04/2016 | 01/05/2016 | 30
 3  | AD   | 01/05/2016 | 17/05/2016 | 30

关于如何做到这一点的任何想法/建议?

2 个答案:

答案 0 :(得分:3)

你可以试试这个:

+--------------+----+------+------------+------------+------+
| FirstOfMonth | ID | user | from       | to         | loan |
+--------------+----+------+------------+------------+------+
| 2016-01-01   | 1  | BB   | 2016-01-01 | 2016-05-01 | 50   |
+--------------+----+------+------------+------------+------+
| 2016-02-01   | 1  | BB   | 2016-01-01 | 2016-05-01 | 50   |
+--------------+----+------+------------+------------+------+
| 2016-03-01   | 1  | BB   | 2016-01-01 | 2016-05-01 | 50   |
+--------------+----+------+------------+------------+------+
| 2016-04-01   | 1  | BB   | 2016-01-01 | 2016-05-01 | 50   |
+--------------+----+------+------------+------------+------+
| 2016-01-01   | 2  | AD   | 2016-01-01 | 2016-03-01 | 25   |
+--------------+----+------+------------+------------+------+
| 2016-02-01   | 2  | AD   | 2016-01-01 | 2016-03-01 | 25   |
+--------------+----+------+------------+------------+------+
| 2016-03-01   | 3  | AD   | 2016-03-01 | 2016-05-17 | 30   |
+--------------+----+------+------------+------------+------+
| 2016-04-01   | 3  | AD   | 2016-03-01 | 2016-05-17 | 30   |
+--------------+----+------+------------+------------+------+

结果

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.WinFx.targets(268,9): e
rror MC1000: Unknown build error, 'Could not load file or assembly 'file:///C:\
Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5
.2\Facades\System.Threading.Timer.dll' or one of its dependencies. The module w
as expected to contain an assembly manifest.'  [*.csproj]

答案 1 :(得分:1)

    ;WITH testtable(ID,[USER],[from],[to],loan)AS(
    SELECT 1,'BB',CONVERT(DATE,'01/01/2016'),CONVERT(DATE,'01/05/2016'),50 UNION all
    SELECT 2,'AD','01/01/2016','01/03/2016',25 UNION all
    SELECT 3 ,'AD','01/03/2016','01/05/2016',30
 )
 SELECT t.ID,t.[USER],DATEADD(d,sv.number,t.[FROM]) AS [From],dateadd(d,sv.number+1,t.[FROM]) AS [To],t.loan FROM testtable AS t
 INNER JOIN master.dbo.spt_values AS sv ON sv.type='P' AND sv.number BETWEEN 0 AND DATEDIFF(d,t.[FROM],t.[TO])-1
ID          USER From       To         loan
----------- ---- ---------- ---------- -----------
1           BB   2016-01-01 2016-01-02 50
1           BB   2016-01-02 2016-01-03 50
1           BB   2016-01-03 2016-01-04 50
1           BB   2016-01-04 2016-01-05 50
2           AD   2016-01-01 2016-01-02 25
2           AD   2016-01-02 2016-01-03 25
3           AD   2016-01-03 2016-01-04 30
3           AD   2016-01-04 2016-01-05 30