I am performing a query that shows a list of skills and users, and highlights which skills are not applicable to certain users.The statement is:SELECT sg.Group, s.Skill, sgu.userID, u.name, u.surname, ut.notApplicableFROM [tbl_groups-skills] sgs INNER JOIN [tbl_groups-Users] sgu ON sgs.groupID = sgu.groupID INNER JOIN [tbl_groups] sg on sgs.groupID = sg.groupID INNER JOIN [tbl_Skills] s ON sgs.skillID = s.skillID INNER JOIN [tbl_Users] u ON u.userID = sgu.userID LEFT OUTER JOIN [tbl_UserTargetLevels] ut ON ut.sgsID = sgs.linkID AND ut.userID = u.userID INNER JOIN @tbl_users u2 ON u2.userID = sgu.userID INNER JOIN @tbl_sgsIds sgs2 ON sgs2.sgsID = sgs.linkID
and produces this result set:Group Skill userID name surname notApplicable---------------------------------------------------------------------------------------------Corporate Governance Business Risk Management 1240 Jon Bevan NULLCorporate Governance Corporate Governance 1240 Jon Bevan 1Corporate Governance Business Risk Management 1199 Donn Smith NULLCorporate Governance Corporate Governance 1199 Donn Smith 1Corporate Governance Business Risk Management 1250 Terry Hobbs 1Corporate Governance Corporate Governance 1250 Terry Hobbs 1Corporate Governance Corporate Governance 1250 David May NULLCorporate Governance Corporate Governance 1250 David May NULL
I need to modify the statement so that users that have a value of 1 for all notApplicable skills would be removed from the results. So, in the results above, Terry Hobbs would be removed from the results because that user has notApplicable against both skills shown. The other users would remain as they only have a single notApplicable value, or zero notApplicable values.I've written a version that works, but I think my new query is extremely long-winded. My approach basically converts the notApplicable bit values to integers, then compares the SUM of them against a second column of integers with the value of 1. I'd appreciate any uber-yaks giving me some pointers on how I might accomplish this in a better way please!SELECT SUM(c.countInt), SUM(c.notApplicable), c.userID, c.name, c.surnameFROM ( SELECT sg.Group, s.Skill, sgu.userID, u.name, u.surname, CAST(ut.notApplicable AS INT) as notApplicable, 1 as countInt FROM [tbl_groups-skills] sgs INNER JOIN [tbl_groups-Users] sgu ON sgs.skillGroupID = sgu.skillGroupID INNER JOIN [tbl_groups] sg on sgs.skillGroupID = sg.skillGroupID INNER JOIN [tbl_Skills] s ON sgs.skillID = s.skillID INNER JOIN [tbl_Users] u ON u.userID = sgu.userID LEFT OUTER JOIN [tbl_UserTargetLevels] ut ON ut.sgsID = sgs.linkID AND ut.userID = u.userID INNER JOIN @tbl_users u2 ON u2.userID = sgu.userID INNER JOIN @tbl_sgsIds sgs2 ON sgs2.sgsID = sgs.linkID ) cGROUP BY c.userID, c.name, c.surnameHAVING SUM(c.notApplicable) = SUM(c.countInt)