I want to generate report in phpmysql in such a way that first detail rows display and in the bottom , sum of that detail row display on the basis of group by statement.
Select id,
Emp-name,
department,
salary,
sum(salary) as total sum for each dept
from Employee
group by department
I tried the following thing
Select id,
Emp-name,
department,
salary,
(select sum(salary) from Employee) as total sum for each dept
from Employee
group by department
desired out put :
Employee Name | Department | Salary |
---|---|---|
Umar | IT | 50000 |
ali | IT | 40000 |
sub total | IT | 90000 |
waseem | HR | 25000 |
waqar | HR | 15000 |
sub total | HR | 40000 |
2
Answers
Your query is grouping by department which will give the total per department and the name and salary of the first employee (which is useless to you here).
You would need to do a separate query to get totals per dept, but in this case it’s better to just order by dept and then do a subtotal per dept when the dept changes. e.g. (pseudo code)
Here’s what you are looking for: