2

I have a log table where multiple rows have been included with ADD status ("status = 0" means "add").

How to remove status = 0 but it must be unrepeated value. For example each transaction id (tid) must have only one value with status = 0.

For example;

id tid       tblname           type     status 
1   101     x                   U              0
2   101     x                   U              0
3   102     x                   U              0
4   102     x                   U              0
5   102     x                   U              0

Must return:

id  tid     tblname       type           status 
1   101     x            U              0
3   102     x            U              0

How can I do that?

4

1 Answer 1

2

With a cte and ROW_NUMBER:

WITH x AS (SELECT rn = Row_number() 
                       OVER( 
                         partition BY tid 
                         ORDER BY id) 
         FROM   dbo.log
         WHERE  status = 0 
                AND tblname = 'X' 
                AND type = 'U') 
DELETE FROM  x 
WHERE  rn > 1 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.