【发布时间】:2018-10-20 20:29:57
【问题描述】:
我有一张带有FieldID, ChangeField, OldValue, NewValue and ChangeDate 的表格,如下所示:
FieldID ChangeField OldValue NewValue ChangeDate
1 interest 1.5 1.2 2018-05-01 13:00:00
1 interest 1.2 1.3 2018-05-01 14:00:00
1 quantity 2 1 2018-05-01 15:00:00
1 quantity 1 2 2018-05-01 16:00:00
1 quantity 2 3 2018-05-01 17:00:00
2 quantity 10 20 2018-05-01 18:00:00
2 quantity 20 30 2018-05-01 19:00:00
是否可以获得每个 FieldID 的每个 ChangeField 的第一个和最后一个更改,例如下面的结果?
FieldID ChangeField OldValue NewValue dtChangeDate
1 interest 1.5 1.3 2018-05-01 14:00:00
--> The original value for interest was 1.5, the last value for interest is 1.3
1 quantity 2 3 2018-05-01 17:00:00
--> The original value for quantity was 2, the last value for interest is 3
2 quantity 10 30 2018-05-01 19:00:00
--> The original value for quantity was 10, the last value for interest is 30
请注意,ChangeDate 始终是最新的 ChangeDate
【问题讨论】:
-
是的,您可以使用适当命名的
FIRST_VALUE和LAST_VALUE函数。
标签: sql sql-server tsql