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
 list of accounts

Author  Topic 

ayyoob1234
Starting Member

3 Posts

Posted - 2013-06-11 : 11:57:29
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
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-06-11 : 12:06:23
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

52326 Posts

Posted - 2013-06-11 : 12:48:33
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
   

- Advertisement -