SQL:将具有开始和结束日期的阶段映射到事务表

时间:2018-03-21 01:08:26

标签: sql sql-server

我有两个表,一个包含开始日期和结束日期列表,另一个表格包含支出清单。

表1:MilestonePhases

+-----------+------------------------------+------------+------------+
| ProjectID | MilestonePhase               | StartDate  | EndDate    |
+-----------+------------------------------+------------+------------+
| 1         | Planning/ Project Initiation | 1/01/2010  | 5/01/2010  |
+-----------+------------------------------+------------+------------+
| 1         | Design                       | 6/01/2010  | 10/01/2010 |
+-----------+------------------------------+------------+------------+
| 1         | Construction                 | 11/01/2010 | 18/01/2010 |
+-----------+------------------------------+------------+------------+
| 1         | Project Finalisation         | 19/01/2010 | 20/01/2010 |
+-----------+------------------------------+------------+------------+

表2:支出

+-----------+-------------+
| Date      | Expenditure |
+-----------+-------------+
| 1/01/2010 | 482         |
+-----------+-------------+
| 2/01/2010 | 541         |
+-----------+-------------+
| 3/01/2010 | 840         |
+-----------+-------------+
| 4/01/2010 | 147         |
+-----------+-------------+
| 5/01/2010 | 297         |
+-----------+-------------+
| 6/01/2010 | 1185        |
+-----------+-------------+
| ...       | ...         |
+-----------+-------------+

我希望将交易映射到每个阶段的开始和结束日期,并重现以下结果但是有困难。

+-----------+-------------+------------------------------+
| Date      | Expenditure | MilestonePhase               |
+-----------+-------------+------------------------------+
| 1/01/2010 | 482         | Planning/ Project Initiation |
+-----------+-------------+------------------------------+
| 2/01/2010 | 541         | Planning/ Project Initiation |
+-----------+-------------+------------------------------+
| 3/01/2010 | 840         | Planning/ Project Initiation |
+-----------+-------------+------------------------------+
| 4/01/2010 | 147         | Planning/ Project Initiation |
+-----------+-------------+------------------------------+
| 5/01/2010 | 297         | Planning/ Project Initiation |
+-----------+-------------+------------------------------+
| 6/01/2010 | 1185        | Design                       |
+-----------+-------------+------------------------------+
| ...       | ...         | ...                          |
+-----------+-------------+------------------------------+

我可以在将表连接在一起时取消透视MilestonePhases表,但我只加入开始日期和结束日期本身,这不会让我成为每笔交易的里程碑阶段

+-----------+-------------+------------------------------+
| Date      | Expenditure | MilestonePhase               |
+-----------+-------------+------------------------------+
| 1/01/2010 | 482         | Planning/ Project Initiation |
+-----------+-------------+------------------------------+
| 2/01/2010 | 541         |                              |
+-----------+-------------+------------------------------+
| 3/01/2010 | 840         |                              |
+-----------+-------------+------------------------------+
| 4/01/2010 | 147         |                              |
+-----------+-------------+------------------------------+
| 5/01/2010 | 297         | Planning/ Project Initiation |
+-----------+-------------+------------------------------+
| 6/01/2010 | 1185        | Design                       |
+-----------+-------------+------------------------------+
| ...       | ...         | ...                          |
+-----------+-------------+------------------------------+

有人能指出我正确的方向吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用join

select e.*, mp. MilestonePhase
from expenditure e left join
     MilestonePhases mp
     on e.date >= mp.start and e.date <= mp.end;

这假设日期存储为日期而不是字符串。如果不是,请修复数据模型,以便正确存储。