I have gotten this to work but I would like to know if there is a better way of writing this. Basically, I have a result set that returns data in groups. I would like to exclude the groups where it contains a certain value. For this example, I want to exclude all groups that has a value of '2'. I have gotten it to work but wonder if there is a way of using HAVING to filter out a group or a better way.
DROP TABLE IF EXISTS #TEMPTest
SELECT * INTO #TEMPTest FROM
(
SELECT 'Group A' AS 'Group_Name', '1' AS 'This_Value'
UNION
SELECT 'Group A' AS 'Group_Name', '2' AS 'This_Value'
UNION
SELECT 'Group A' AS 'Group_Name', '3' AS 'This_Value'
UNION
SELECT 'Group B' AS 'Group_Name', '4' AS 'This_Value'
UNION
SELECT 'Group B' AS 'Group_Name', '5' AS 'This_Value'
UNION
SELECT 'Group B' AS 'Group_Name', '6' AS 'This_Value'
) T1
SELECT * FROM #TEMPTest
--This works
SELECT * FROM #TEMPTest
WHERE Group_Name NOT IN
(
SELECT Group_Name FROM #TEMPTest
WHERE This_Value = '2'
GROUP BY Group_Name
)
This did not work. Looking for a more simple form to filter out group.
SELECT Group_Name FROM #TEMPTest
GROUP BY Group_Name, This_Value
HAVING This_Value NOT IN('2')
Jeff, I would like to exclude the GROUP that contains the STRING '2' in any of its row. The field This_Value is a string value, sorry if it looks like a numeric field. Therefore, I can't use the SUM.
Yosiasz, I have Group A and Group B in my sample data (above). So when any of those groups have a value (in this case '2'), I would like to exclude from seeing that group in the result set.