Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to sum up values of a row in a single column using MySQL. I have tried all the below codes but nothing seems to work, i need help please

What I have tried:

SELECT employee.dept,employee.loc, employee.work, 
COUNT(IF(employee_info.emp_date = '2021-11-01', employee_info.emp_date, NULL)) AS a,
 COUNT(IF(employee_info.emp_date = '2021-11-02', employee_info.emp_date, NULL)) AS b,
  COUNT(IF(employee_info.emp_date = '2021-11-02', employee_info.emp_date, NULL)) AS c, SUM("a"+"b"+"c") AS total FROM employee_info INNER JOIN employee ON employee_info.employee_number = employee.employee_number GROUP BY employee.dept WITH ROLLUP


SELECT employee.dept,employee.loc, employee.work, 
COUNT(IF(employee_info.emp_date = '2021-11-01', employee_info.emp_date, NULL)) AS a,
 COUNT(IF(employee_info.emp_date = '2021-11-02', employee_info.emp_date, NULL)) AS b,
  COUNT(IF(employee_info.emp_date = '2021-11-02', employee_info.emp_date, NULL)) AS c, SUM('a'+'b'+'c') AS total FROM employee_info INNER JOIN employee ON employee_info.employee_number = employee.employee_number GROUP BY employee.dept WITH ROLLUP


SELECT employee.dept,employee.loc, employee.work, 
COUNT(IF(employee_info.emp_date = '2021-11-01', employee_info.emp_date, NULL)) AS a,
 COUNT(IF(employee_info.emp_date = '2021-11-02', employee_info.emp_date, NULL)) AS b,
  COUNT(IF(employee_info.emp_date = '2021-11-02', employee_info.emp_date, NULL)) AS c, 'a'+'b'+'c' AS total FROM employee_info INNER JOIN employee ON employee_info.employee_number = employee.employee_number GROUP BY employee.dept WITH ROLLUP
Posted
Updated 16-Jan-22 2:52am

Do it as a subquery:
SQL
SELECT Dept, Loc, Work, a, b, c, a + b + c  AS [Total]
FROM (SELECT employee.dept AS Dept, employee.loc AS Loc, employee.work AS Work, 
COUNT(IF(employee_info.emp_date = '2021-11-01', employee_info.emp_date, NULL)) AS a,
 COUNT(IF(employee_info.emp_date = '2021-11-02', employee_info.emp_date, NULL)) AS b,
  COUNT(IF(employee_info.emp_date = '2021-11-02', employee_info.emp_date, NULL)) AS c,  FROM employee_info INNER JOIN employee ON employee_info.employee_number = employee.employee_number GROUP BY employee.dept WITH ROLLUP)
 
Share this answer
 
Comments
[no name] 16-Jan-22 9:00am    
Thank you for your answer, but it seems to me the solution your provided is for SQL Server, while am using MYSQL. Well i tried it but it keeps giving me error 'ALL CREATED TABLES NEED TO HAVE AN ALIAS', i tried creating alias for the tables but no solution.
Thank you
You can also use your query like:

SELECT employee.dept,employee.loc, employee.work, 
(COUNT(IF(employee_info.emp_date = '2021-11-01', employee_info.emp_date, NULL)) +
COUNT(IF(employee_info.emp_date = '2021-11-02', employee_info.emp_date, NULL)) +
COUNT(IF(employee_info.emp_date = '2021-11-02', employee_info.emp_date, NULL))) AS total 

FROM employee_info 
	INNER JOIN employee ON employee_info.employee_number = employee.employee_number 
GROUP BY employee.dept WITH ROLLUP
 
Share this answer
 
Comments
[no name] 16-Jan-22 8:57am    
Yeah thank you it worked fine, but the solution is a little bit long, you have another approach please
M Imran Ansari 16-Jan-22 9:00am    
Pleasure! Yeah, you can also use scaler function which return the totals.

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