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 Function

Author  Topic 

evanburen
Posting Yak Master

167 Posts

Posted - 2006-04-12 : 10:51:50
If I have a stored procedure like this, do you know how I would return a SUM of tc.dc_TotalCompanyCompensation? In other words, I want a sum of tc.dc_TotalCompanyCompensation for all records returned in this recordset. Thanks.

CREATE PROCEDURE sp_DirCompProfile
(
@IDDir Int
)
AS
SELECT
tds.FinancialExpert,
tds.DirOutside,
tds.DateRetiring,
tds.DirSince,
tds.DirStatus,
tds.DirTenure,
tc.BAStatus,
tc.CIK,
tc.CompID,
tc.CompanyName,
tc.Ticker,
tc.dc_TotalCompanyCompensation,
FROM TCompanies AS tc INNER JOIN TDirectorships AS tds ON tc.Ticker = tds.Ticker
WHERE tds.IDDir=@IDDir
GO

nr
SQLTeam MVY

12543 Posts

Posted - 2006-04-12 : 11:26:20
SELECT
tds.FinancialExpert,
tds.DirOutside,
tds.DateRetiring,
tds.DirSince,
tds.DirStatus,
tds.DirTenure,
tc.BAStatus,
tc.CIK,
tc.CompID,
tc.CompanyName,
tc.Ticker,
tc.dc_TotalCompanyCompensation,
total = (select sum(tc.dc_TotalCompanyCompensation) from TCompanies AS tc INNER JOIN TDirectorships AS tds ON tc.Ticker = tds.Ticker WHERE tds.IDDir=@IDDir)
FROM TCompanies AS tc INNER JOIN TDirectorships AS tds ON tc.Ticker = tds.Ticker
WHERE tds.IDDir=@IDDir

Or you could get the resultset into a temp table and do the sum on that or use a common table expression.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2006-04-13 : 04:05:44
The best way is to calculate sum on the client side, not in sql.
Go to Top of Page
   

- Advertisement -