TOP 3 DISTINCT

  • Hi

    Can someone help to get the TOP 3 distinct output based on the below input? ID column is identity column

    Input

    StatusID

    ----------------

    Active1291070

    Active1242253

    Active1227193

    Active948452

    Suspended-Occupancy698055

    Active565330

    Suspended-Payment Pending545343

    Output

    StatusID

    Active1291070

    Suspended-Occupancy698055

    Suspended-Payment Pending545343

    Thanks

    Shuaib

  • Do you want the largest ID for each status?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Yep, I want to pick the largest ID for each status.

    Thanks

    Shuaib

  • Try this.

    SELECT TOP 3 Status, MaxID AS ID

    FROM

    (SELECT Status, MAX(ID) AS MaxID FROM SomeTable GROUP BY Status) sub

    ORDER BY Status

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • This query works for me.

    Thanks

    Shuaib

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply