简体   繁体   English

根据当前和上一个记录值更新字段/列

[英]Update a Field/Column based on Current and Previous Record Value

I need assistance with updating a field/column "IsLatest" based on the comparison between the current and previous record. 我需要基于当前记录与先前记录之间的比较来更新字段/列“ IsLatest”的帮助。 I'm using CTE's syntax and I'm able to get the current and previous record but I'm unable updated field/column "IsLatest" which I need based on the field/column "Value" of the current and previous record. 我正在使用CTE的语法,并且能够获取当前和以前的记录,但是我无法根据当前和先前的记录的字段/列“值”更新需要的字段/列“ IsLatest”。

Example

Current Output 电流输出

Dates                   Customer    Value   IsLatest
2010-01-01 00:00:00.000    1            12  1

Dates                   Customer    Value   IsLatest
2010-01-01 00:00:00.000    1            12  0
2010-01-02 00:00:00.000    1            30  1

Dates                   Customer    Value   IsLatest
2010-01-01 00:00:00.000    1            12  0
2010-01-02 00:00:00.000    1            30  0
2010-01-03 00:00:00.000    1            13  1

Expected Final Output 预期最终产量

Dates                   Customer    Value   ValueSetId IsLatest
2010-01-01 00:00:00.000    1            12     12          0
2010-01-01 00:00:00.000    1            12     13          0
2010-01-01 00:00:00.000    1            12     14          0

2010-01-02 00:00:00.000    1            30     12          0
2010-01-02 00:00:00.000    1            30     13          0
2010-01-02 00:00:00.000    1            30     14          0

2010-01-03 00:00:00.000    1            13     12          0
2010-01-03 00:00:00.000    1            13     13          0
2010-01-03 00:00:00.000    1            13     14          0

2010-01-04 00:00:00.000    1            14     12          0
2010-01-04 00:00:00.000    1            14     13          0
2010-01-04 00:00:00.000    1            14     14          1
;WITH a AS
(
SELECT 
  Dates Customer Value, 
  row_number() over (partition by customer order by Dates desc, ValueSetId desc) rn
FROM @Customers)
SELECT Dates, Customer, Value, case when RN = 1 then 1 else 0 end IsLatest
FROM a

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

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