SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL script , how to use SUM?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

gim00_us
Starting Member

2 Posts

Posted - 07/24/2013 :  11:40:57  Show Profile  Reply with Quote
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

547 Posts

Posted - 07/24/2013 :  11:48:54  Show Profile  Reply with Quote

SELECT SUM (salary) as [Total Salary]
FROM employees 
HAVING SUM(salary) > 25000; 
Go to Top of Page

gim00_us
Starting Member

2 Posts

Posted - 07/24/2013 :  12:21:31  Show Profile  Reply with Quote
wow, thank you. i feel so stupid
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 07/24/2013 :  12:54:20  Show Profile  Reply with Quote
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

India
22765 Posts

Posted - 07/28/2013 :  11:44:49  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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

Edited by - madhivanan on 07/28/2013 11:47:39
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000