简体   繁体   English

如何使用时间戳作为参考合并SQL Server 2014中同一表中的记录

[英]How do I merge records in the same table in SQL Server 2014 using timestamp as reference

Currently I have a table after using PIVOT() with the format and data values as follows 目前,在将PIVOT()与以下格式和数据值一起使用后,我有一个表

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. 我需要将具有最新值的记录1,2,3合并到一行中,其格式将是这样。

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 : 在不了解pivot查询本身的情况下,也许最简单的方法是关联子查询或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 如何在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 无论如何,如果不可能,并且假设您也为此要求使用SQL Server 2014,则可以在当前方法上使用LAST_VALUE

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM