Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select row with max value when duplicate rows exist in SQL Server

Tags:

sql

sql-server

I have table like this

DocumentID        | MasterStepID | StepNumber | RoleID | UserID     | Status
JIEP/TT/07/000174 | Approval1    |          1 |   NULL | 0006100022 |      1    
JIEP/TT/07/000174 | Approval1    |          2 |     12 | 0006199013 |      3    
JIEP/TT/07/000174 | Approval1    |          3 |     13 | 0006106426 |      3
JIEP/TT/07/000174 | Approval1    |          5 |     18 | 0006100022 |      3
JIEP/TT/07/000174 | Approval1    |          6 |     16 | 0006104115 |      6

I expect result like this

JIEP/TT/07/000174 | Approval1 | 1 | NULL | 0006100022 | 1
JIEP/TT/07/000174 | Approval1 | 5 |   18 | 0006100022 | 3
JIEP/TT/07/000174 | Approval1 | 6 |   16 | 0006104115 | 6

I try this query but it's return not like what I expect

select  *
from    WF_Approval sr1
where not exists
        (
        select  *
        from    WF_Approval sr2 
        where   sr1.DocumentID = sr2.DocumentID and 
                (
                    sr1.StepNumber < sr2.StepNumber
                )
        )and MasterStepID = 'Approval1'
like image 311
Selalu_Ingin_Belajar Avatar asked Oct 14 '12 05:10

Selalu_Ingin_Belajar


2 Answers

You're basically just missing a status comparison since you want one row per status;

SELECT *
FROM WF_Approval sr1
WHERE NOT EXISTS (
    SELECT *
    FROM  WF_Approval sr2 
    WHERE sr1.DocumentID = sr2.DocumentID AND 
          sr1.Status = sr2.Status AND                  # <-- new line
          sr1.StepNumber < sr2.StepNumber
) AND MasterStepID = 'Approval1'

or rewritten as a JOIN;

SELECT *
FROM WF_Approval sr1
LEFT JOIN WF_Approval sr2
  ON sr1.DocumentID = sr2.DocumentID 
 AND sr1.Status = sr2.Status
 AND sr1.StepNumber < sr2.StepNumber
WHERE sr2.DocumentID IS NULL
  AND sr1.MasterStepID = 'Approval1';

SQLfiddle with both versions of the query here.

like image 162
Joachim Isaksson Avatar answered Nov 14 '22 21:11

Joachim Isaksson


This should be faster than a self-join because (most probably) only a single scan over the table is required.

select DocumentID, 
       MasterStepID, 
       StepNumber, 
       RoleID,
       UserID , 
       Status
from (
    select wf.*
           row_number() over (partition by wf.status order by wf.stepnumber desc) as rn
    from  WF_Approval wf
) t
where rn = 1
order by StepNumber
like image 25
a_horse_with_no_name Avatar answered Nov 14 '22 23:11

a_horse_with_no_name