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
 SQL Server Development (2000)
 Totaling a column

Author  Topic 

SamC
White Water Yakist

3467 Posts

Posted - 2002-11-22 : 09:02:44
A stored procedure lists time spent reading a page.

SELECT ModuleSequence, ModuleTitle, Minutes
FROM CourseVisitations
WHERE UserID = @UserID
ORDER BY CourseID, ModuleSequence

I'd like to return a last row with a total minutes, ModuleTitle set to 'total' and modulesequence NULL.

It looks like I'll need to repeat the query in a Union All.

Union ALL
SELECT NULL, 'Total Minutes', Sum(Minutes)
FROM (SELECT Minutes from Coursevisitations where userid=@Userid) A

Is there a simpler way to get the total ?

Sam

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-11-22 : 09:08:46
Have you tried WITH ROLLUP?

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=20740
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=21159

Books Online has some more examples too.

Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2002-11-22 : 14:10:36
Rob,

I've played (a little) with Rollup.

Rollup wants to total all the columns, some of which are text.

Isn't this a problem?

Sam

Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2002-11-22 : 14:55:15
I think you need to add GROUP BY's and aggregate formulas to use ROLLUP. Don't remember. But i think that's how it knows what to roll-up, and using which formula.


- Jeff
Go to Top of Page

Scott
Posting Yak Master

145 Posts

Posted - 2002-11-25 : 04:10:50
Just pop a:
compute sum(minutes) as [Total Minutes]
under your select.
NOTE: ADO will now bring through 2 recordsets one with the select and one with the total.

Go to Top of Page
   

- Advertisement -