Select query to determine unique values

  • SELECT

    GrpID, GrpName,

    ISNULL(NULLIF(MAX(CASE WHEN Spc <> 'Mixed' THEN Spc ELSE '' END), ''), 'Mixed') AS Spc

    FROM #mytable

    GROUP BY

    GrpID, GrpName

    HAVING

    MAX(CASE WHEN Spc = 'Mixed' AND PrvName = '' THEN 1 ELSE 0 END) = 1 AND

    COUNT(DISTINCT CASE WHEN Spc <> 'Mixed' THEN Spc END) < 2

    ORDER BY

    GrpID

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

Viewing post 16 (of 15 total)

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