Home Forums SQL Server 2012 SQL Server 2012 - T-SQL Looking For A Way to Check Data Spread Across Multiple Records; Looping Most Likely Involved RE: Looking For A Way to Check Data Spread Across Multiple Records; Looping Most Likely Involved

  • Using HAVING eliminates a pass of the table and makes it easier to specify different conditions.

    SELECT mt.*

    FROM@myTable mt

    INNER JOIN (

    SELECT ColumnA

    FROM @myTable

    GROUP BY ColumnA

    HAVING MAX(CASE WHEN ColumnB = 'Col2spec' THEN 1 ELSE 0 END) = 1 AND

    MAX(CASE WHEN ColumnC = 'Col3spec' THEN 1 ELSE 0 END) = 1

    ) AS mt_matches ON mt_matches.ColumnA = mt.ColumnA

    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".