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
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,257 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 109.8K 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