'Improving SQL Query to check condition
Currently, the sql code that I have is to check for the three fee_code records do any of them have election_code = NA.
I'm trying to figure out how to change the code to check that all of the 3 records have election_code = NA.
Original Query:
DECLARE @fee_code1 tinyint = 1
,@fee_code2 tinyint = 2
,@fee_code3 tinyint = 3
,@election_code tinyint = 0
IF EXISTS(SELECT *
FROM Product
WHERE
product_id = @product_id
AND fee_code in (@fee_code1, @fee_code2, @fee_code3)
AND election_code = @election_code)
The solution I have is to do a count check, but I know count has a performance impact and there's probably a better way to do it. What's a better way to do it?
Count solution:
IF ((SELECT COUNT(*)
FROM Product
WHERE
product_id = @product_id
AND fee_code in (@fee_code1, @fee_code2, @fee_code3)
AND election_code = @election_code) = 3)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|