Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL script , how to use SUM?

Author  Topic 

gim00_us
Starting Member

2 Posts

Posted - 2013-07-24 : 11:40:57
I would like to know how to use a criterion on this example. I want to know only when the total salary is at a certain amount

SELECT SUM (salary) as Total Salary
FROM employees
WHERE Total Salary > 25000; ---this is where i am having issue

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-07-24 : 11:48:54
[CODE]
SELECT SUM (salary) as [Total Salary]
FROM employees
HAVING SUM(salary) > 25000;
[/CODE]
Go to Top of Page

gim00_us
Starting Member

2 Posts

Posted - 2013-07-24 : 12:21:31
wow, thank you. i feel so stupid
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-07-24 : 12:54:20
quote:
Originally posted by gim00_us

wow, thank you. i feel so stupid



Don't worry we all have those moments, if any consolation look at this: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=187073

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-07-28 : 11:44:49
Note that You cannot directly use alias name in the WHERE or HAVING clause. Another method is

SELECT Total_Salary FROM
(
SELECT SUM (salary) as Total_Salary
FROM employees
) as t
WHERE Total_Salary > 25000;

Also you may want to group it by employee name. Do you want to just know the total salary of all employees is greater than 25000?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -