基于多个日期和时间的T-SQL分组结果集

时间:2014-03-28 13:43:36

标签: sql sql-server date time grouping

ID....| Date Created on System |.Action..............| Appointment Date
-----------------------------------------------------------------------------
ID123 |......15/03/2013 16:32..| Due.......          |.NULL
ID123 |......05/04/2013 09:27..|.Appointment booked..|.08/04/2013 11:30
ID123 |......24/03/2014 11:55..|.Appointment booked..|.10/04/2014 09:30
ID123 |......09/02/2014 11:43..|.Appointment Due.....|.NULL
ID123 |......24/03/2014 11:55..|.Appointment booked..|.20/06/2014 09:30

我需要在每次“约会到期”时将上述结果分开。与随后的约会预订操作相关联,但我还需要过滤由创建日期预约的约会,以便在组中给出约会到期日期和最早的约会预约日期

ID....| Date Created on System |.Action............| Appointment Date
-----------------------------------------------------------------------------
ID123 |.15/03/2013 16:32.......|.Appointment Due...|...NULL


ID123 |.05/04/2013 09:27.......|.Appointment booked.|.08/04/2013 11:30

最终结果集应该是两行,如下所示

ID    | Date Created on System 1 | Action 1        | Date Created on System | Action 2          | Appointment Date|
---------------------------------------------------------------------------------------------------------------------
ID123 | 15/03/2013 16:32         | Appointment Due | 05/04/2013 09:27       | Appointment booked | 08/04/2013 11:30
ID123 | 09/02/2014 11:43         | Appointment Due | 24/03/2014 11:55       | Appointment booked | 20/06/2014 09:30

提前致谢!

1 个答案:

答案 0 :(得分:0)

我为你演示了一个sqlFiddle。我根据您的评论添加了ID列和PersonId列:see here

希望这适合你!