如果您的字段数量有限,您可以试试这个。
否则,您将不得不构建一个动态查询
SQL小提琴
MS SQL Server 2008 架构设置:
Create table Test (ID int,
data datetime,
field1 varchar(50),
field2 varchar(50),
field3 varchar(50))
insert into test select 1,'2013-02-14','value1',NULL,NULL
insert into test select 2,'2013-02-12','xxx',NULL,NULL
insert into test select 1,'2013-02-10',NULL,'value2',NULL
insert into test select 1,'2013-02-04','init1','init2','init3'
查询 1:
;WITH MinDate AS (
SELECT ID, MIN(DATA) AS DATA, MAX(DATA) AS Lastdate
FROM Test
GROUP BY id
)-- Date of the initial state of each id
, D1 AS (
SELECT ID, Field1, ROW_NUMBER() OVER (ORDER BY DATA DESC) AS RowNumber
FROM TEST
WHERE Field1 IS NOT NULL
) --Last date when Field 1 was changed
, D2 AS (
SELECT ID, Field2, ROW_NUMBER() OVER (ORDER BY DATA DESC) AS RowNumber
FROM TEST
WHERE Field2 IS NOT NULL
)
, D3 AS (
SELECT ID, Field3, ROW_NUMBER() OVER (ORDER BY DATA DESC) AS RowNumber
FROM TEST
WHERE Field3 IS NOT NULL
)
SELECT T.ID, M.LastDate
, ISNULL(D1.Field1, T.Field1) AS Field1
, ISNULL(D2.Field2, T.Field2) AS Field2
, ISNULL(D3.Field3, T.Field3) AS Field3
FROM Test T
JOIN MinDate M ON T.id=M.id AND T.data=M.data
LEFT OUTER JOIN D1 ON T.ID=D1.ID AND D1.RowNumber=1
LEFT OUTER JOIN D2 ON T.ID=D2.ID AND D2.RowNumber=1
LEFT OUTER JOIN D3 ON T.ID=D3.ID AND D3.RowNumber=1
结果:
| ID | LASTDATE | FIELD1 | FIELD2 | FIELD3 |
-------------------------------------------------------------------
| 1 | February, 14 2013 00:00:00+0000 | value1 | value2 | init3 |
| 2 | February, 12 2013 00:00:00+0000 | xxx | (null) | (null) |