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) A socialist is someone who will give you the shirt off *someone else's* back.