SQL Query to find the previous row

Mansoor Mohammed 61 Reputation points
2021-12-08T18:24:09.48+00:00

156034-image.png

I want to update the transaction colum in the table that has previous transaction(TansactionSequence minus one) from Y to E.
Ex:Update Transaction colum 5 and 10 to E
Note: 1. TansactionSequence with 1 is always Y, and can have multiple Transaction Sequence numbers.
2. SequenceNo is a primary Key

SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Viorel 125.7K Reputation points
    2021-12-08T18:54:49.107+00:00

    It seems that you did not give all of details. Try this statement:

    update T
    set Transactions = 'E'
    from 
    (
        select *, lag(Transactions) over (partition by CreditCardNumber order by TransactionSequence) as pt 
        from MyTable
    ) T
    where pt = 'E'
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.