Currently I have a table after using PIVOT() with the format and data values as follows
NameID DocumentName Time Value1 Value2 Value3 Value4 Value5
------ ------------ ---- ------ ------ ------ ------ ------
2221 Doc1 1053 23 24 25 NULL NULL
2221 Doc1 1153 31 32 NULL 28 30
2221 Doc2 1253 NULL NULL NULL 40 41
2222 Doc3 1053 03 06 09 12 15
I need to merge record 1,2,3 with the latest values into one single row, where the format would be like this.
NameID Value1 Value2 Value3 Value4 Value5
------ ------ ------ ------ ------ ------
2221 31 32 25 40 41
2222 03 06 09 12 15
It would use timestamp as reference to decide what values to update and which values will be kept. Any help or start would be appreciated for me to carry on my work!
This is tricky.
Without knowledge about the pivot
query itself, perhaps the simplest method is correlated subqueries or apply
:
select nameId,
(select top (1) t2.documentname
from t t2
where t2.nameId = t.nameId and t2.documentname is not null
order by t2.time desc
) as documentname,
max(time) as time,
(select top (1) t2.value1
from t t2
where t2.nameId = t.nameId and t2.value1 is not null
order by t2.time desc
) as value1,
(select top (1) t2.value2
from t t2
where t2.nameId = t.nameId and t2.value2 is not null
order by t2.time desc
) as value2,
. . .
from t;
I can not see any difference between this and your question here...
How to transpose rows of data into a single row with different columns in SQL Server
The answer I gave you is already taking the last values before do the pivot, if this is a new requirement you probably just need to adapt the partitions to it...
Anyway, If that is not possible and assuming that you are using SQL Server 2014 for this requirement too, then you can use LAST_VALUE over your current approach
WITH YourPivot AS (
<Put your current pivot code here>
)
SELECT DISTINCT
NameID
,LAST_VALUE(Value1) OVER (PARTITION BY NameID ORDER BY CASE WHEN Value1 IS NOT NULL THEN 1 ELSE 0 END, [Time] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) Value1
,LAST_VALUE(Value2) OVER (PARTITION BY NameID ORDER BY CASE WHEN Value2 IS NOT NULL THEN 1 ELSE 0 END, [Time] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) Value2
,LAST_VALUE(Value3) OVER (PARTITION BY NameID ORDER BY CASE WHEN Value3 IS NOT NULL THEN 1 ELSE 0 END, [Time] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) Value3
,LAST_VALUE(Value4) OVER (PARTITION BY NameID ORDER BY CASE WHEN Value4 IS NOT NULL THEN 1 ELSE 0 END, [Time] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) Value4
,LAST_VALUE(Value5) OVER (PARTITION BY NameID ORDER BY CASE WHEN Value5 IS NOT NULL THEN 1 ELSE 0 END, [Time] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) Value5
FROM YourPivot
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.