I have a table that stores several items for the state, and I want to get an account for each state in accordance with specific conditions. I wrote this query:
SELECT
State_ID,
State_Name,
State_All= CASE WHEN type1=1 AND type2=1 THEN COUNT(Id) END
State_w= CASE WHEN type1=2 AND type2=1 THEN COUNT(Id) END
State_s= CASE WHEN type1=2 AND type2=2 THEN COUNT(Id) END
FROM
tblStates
but I get this error:
Column 'State_ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
When I added the sentence GROUP BYfor State_ID, I again got the error above for State_Name, and when the State_Name condition was added GROUP BY, I got an error for State_All, State_w, State_s.
I do not have a column State_All, State_w, State_s in my table.
How can I get an invoice according to specific conditions without use CURSORS?
source
share