I have a table of answers like this:
ID ItemID Answer
1 1 yes
2 1 no
3 1 yes
4 1 yes
5 2 no
6 2 yes
7 3 yes
I'd like to be able to provide an array of ItemId and compute the number of yes answers - number of no answers for each ItemID in the array.
I can do this for an individual item like this:
SELECT (SELECT count(ID) FROM table WHERE ItemID= <id> AND Answer = 'Yes') - (SELECT count(ID) FROM table WHERE ItemID= <id> AND Answer = 'No') AS difference
but how can I adapt this to work for multiple ids in one query?
My expected output, given the input [1,2] would look like
ItemID Difference
1 2
2 0