Reputation: 47
I am trying to complete an assignment on Microsoft SQL Management Server using the following instructions:
I have completed steps 1-2, but when I get to step 3 I run into a problem. Here is my step 3:
CREATE VIEW VIEW3
AS
SELECT s.STORE_CODE, s."STORE_NAME", COUNT(*) AS EMPLOYEES_COUNT
FROM EMPLOYEE e join STORE s ON s.STORE_CODE = e.STORE_CODE
GROUP BY e.STORE_CODE;
I keep getting the same error when I try to execute it:
Column 'STORE.STORE_CODE' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
What do I need to change?
Upvotes: 0
Views: 846
Reputation: 2715
Each table or view column in any nonaggregate expression in the list must be included in the GROUP BY
list
You need to add STORE_NAME
column in the group by list
CREATE VIEW VIEW3
AS
SELECT s.STORE_CODE, s."STORE_NAME", COUNT(*) AS EMPLOYEES_COUNT
FROM EMPLOYEE e
JOIN STORE s ON s.STORE_CODE = e.STORE_CODE
GROUP BY s.STORE_CODE, s."STORE_NAME";
Upvotes: 2