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
 list of accounts
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ayyoob1234
Starting Member

3 Posts

Posted - 06/11/2013 :  11:57:29  Show Profile  Reply with Quote
hi

i have to extract a list of accounts where balance < 10000

a/c_id start_dt end_dt balance
B12345 01-01-2007 25-06-2007 15000
B12345 26-05-2007 01-10-2007 15000
B12345 02-10-2007 31-12-2099 25000

my code is as follow :

select a/c_id, sum(balance)
from table
where sum(balance) > 10000
group by a/c_id

plzzz help

James K
Flowing Fount of Yak Knowledge

3651 Posts

Posted - 06/11/2013 :  12:06:23  Show Profile  Reply with Quote
quote:
Originally posted by ayyoob1234

hi

i have to extract a list of accounts where balance < 10000

a/c_id start_dt end_dt balance
B12345 01-01-2007 25-06-2007 15000
B12345 26-05-2007 01-10-2007 15000
B12345 02-10-2007 31-12-2099 25000

my code is as follow :

select a/c_id, sum(balance)
from table
where sum(balance) > 10000
group by a/c_id

plzzz help


T-SQL syntax does not allow you to put an aggregate such as SUM in the WHERE clause. You have to use a HAVING clause like shown below:
select a/c_id, sum(balance)
from table
group by a/c_id
HAVING sum(balance) > 10000
BTW, did you want to get accounts with balances LESS than 10000 or MORE than 10000? The query is looking for MORE than, but you said LESS than in the text of your posting.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/11/2013 :  12:48:33  Show Profile  Reply with Quote
Hmm...how can balance be summated like this? Isnt balance field containing the balance as on that that date?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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.05 seconds. Powered By: Snitz Forums 2000