Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Group By Running Total

Status
Not open for further replies.

HulaGirl

Programmer
Apr 6, 2002
20
US
Hi all,
I am trying to find out the highest positive creditdebit balance and the date it occured for each customer in the account table. I can determine the amount of the highest positive creditdebit balance for each customer using the following codes, but not the date it occured. I can 't group by transdate since it is unique for each transaction. Please HELP. Thanks...

Code...
Code:
SELECT t.customerid, MAX(t.runningtotal)
FROM (SELECT d2.customerid
	, d2.transdate
	,runningtotal = (SELECT SUM(d1.creditdebit) 
			FROM account D1 
			WHERE D1.transdateDate <= D2.transdate
				AND D1.customerid = D2.customerid) 
	FROM account D2
	) t
GROUP BY t.customerid
ORDER BY t.customerid
 
give this a try..................

select x.id,x.run,y.transdateDate from
(SELECT t.customerid as id, MAX(t.runningtotal as run)
FROM (SELECT d2.customerid
, d2.transdate
,runningtotal = (SELECT SUM(d1.creditdebit)
FROM account D1
WHERE D1.transdateDate <= D2.transdate
AND D1.customerid = D2.customerid)
FROM account D2
) t
GROUP BY t.customerid) as X , account y
where x.id = y.customerid and x.run = y.runningtotal
order by
x.id,x.run,y.transdateDate

[ponder]
 
Thanks ClaireHsu,
But runningtotal is an alias not a field in table &quot;account&quot;.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top