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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 SUM() with WHERE clause

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-05-06 : 09:42:25
NITIN writes "Table Structure

ACNO DATE DEBITAMOUNT CREDITAMOUNT
A001 01/04/04 2000 0.00
N001 01/04/04 0.00 3000
A001 10/04/04 1500 0.00
A002 10/04/04 2500 0.00
N001 15/04/04 0.00 3500
A002 10/04/04 0.00 4500

SELECT ACNO,SUM(DEBITAMOUNT) AS DAMT, SUM(CREDITAMOUTN) AS CAMT
FROM TRANS WHERE DATE < 15/04/04 GROUP BY ACNO

in the above sample select statement date clause returns null values with one row. i want SUM() working with date ranges with the help of WHERE clause.

from the above table i want to sum DEBITAMOUNT and CREDITAMOUNT with a date range. the result should be like this below :-

ACNO DEBITAMOUNT CREDITAMOUNT
A001 3500 0.00
N001 0.00 3000
A002 2500 4500

Thanks and Regards
Nitin Khanna"

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2004-05-06 : 09:59:03
SELECT ACNO,SUM(DEBITAMOUNT) AS DAMT, SUM(CREDITAMOUTN) AS CAMT
FROM TRANS WHERE DATE < '15-Apr-2004' GROUP BY ACNO
Go to Top of Page
   

- Advertisement -