-1

I have the following example set of data in SQL:enter image description here

I need to select for RequestType = 1 and ToRoleID = 1 the maximum step where there is no record with other ToRoleID.

Example: If I filter by RequestType = 1 and ToRoleID = 1, this should give me only the row with Request_ID = 5.

Can I do it only with partition, JOIN?

Thank you.

Radoslav.Ivanov
  • 413
  • 3
  • 16
  • 1
    I'm not exactly clear about the expected results. You're not showing a row where `Request_ID = 5` - do you mean row 5 in the result set shown, where `Request_ID = 18`? I also don't follow what you mean by "maximum step where there is no record with other `ToRoleID`. – LDMJoe Jan 22 '16 at 19:43

1 Answers1

0

This might be what you're wanting.

DECLARE @RequestType INT = 1,
        @ToRoleID INT = 1

SELECT  * 
FROM    (   SELECT  *,
                    MAX(ToRoleID) OVER (PARTITION BY Request_ID) MaxRole
            FROM    myTable
            WHERE   me.RequestType = @RequestType) mt
WHERE   MaxRole = @ToRoleID
JamieD77
  • 13,796
  • 1
  • 17
  • 27