Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
select d.dept_num, d.dept_name, e.emp_num as "MANAGER", count(*) as "TOTAL EMPLOYEE SALARY"
from LGDEPARTMENT d, LGEMPLOYEE e, LGSALARY_HISTORY sh
where d.DEPT_NUM = e.DEPT_NUM and sh.emp_num = d.emp_num and
e.EMP_TITLE = 'MANAGER' and sh.SAL_END is NULL
group by d.dept_num, d.dept_name, e.emp_num;

What I have tried:

select d.dept_num, d.dept_name, e.manager_id, count(*) as "Total Employee Salary"
from LGDEPARTMENT d, lgemployee e, lgsalary_history sh
where d.dept_num = e.dept_num and sh.emp_num = d.emp_num
group by d.dept_num, d.dept_name;
Posted
Updated 8-May-22 19:24pm
Comments
The Other John Ingram 8-May-22 22:29pm    
i would start by removing the where statement and see what you get. Then add back the where statement test one at a time.
Graeme_Grant 8-May-22 23:34pm    
Definitely... bring it back to the basic query, then gradually build it back up. As part of this process, check the data returned at each step. This will ensure for the next step that there will be a result.

This is called debugging.

1 solution

You won't get any rows because that query won't run: you will get an error message from SQL Server that
"Column 'e.manager_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."


And even if it did, it's wouldn't produce what you wanted because a basic SELECT from two tables produces all combinations of those tables.
Assume you have two tables, TableA and TableB Each has one column (TableA.A and TableB.B) and each have two rows:
TableA:
A1
A2

TableB:
B1
B2
A simple SELECT query with both tables will give you four rows:
SQL
SELECT A.A, B.B FROM TableA A, TableB B

A1 B1
A2 B1
A1 B2
A2 B"
Because SQL doesn't know any better, so it tried hard to give you what you want.

GROUP BY won't reduce that because you will get the error message I mentioned above: GROUP BY is there to aggregate values, not combine tables!
See here: SQL GROUP By and the "Column 'name' is invalid in the select list because..." error[^]

Instead, what you need to do is JOIN tables together: SQL Joins[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900