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)
 Newbie Question - Can I total values in a SELECT?

Author  Topic 

lanejc
Starting Member

11 Posts

Posted - 2002-04-18 : 10:01:55
I need to run a SELECT statement against a table and then total the values based on certain key values in the table. For Example:

Columns: Job#, Order#, Unit Cost

I need to get the total Unit Cost value of all jobs matching a certain Job#. So if my table looks like this:

Job# Order# UnitCost
001 001 50.00
001 002 30.00
002 001 10.00
002 002 30.00


Then my data returned needs to be :

Job# TotalCost
001 80.00
002 40.00


I know this is probably simple, but I'm kind of new to SQL and up until now I've just been returning all the values and doing the totals on my end, but I'm guessing it would be faster to do the total on the other end.

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2002-04-18 : 10:06:34
Here 'tis...

SELECT Job, Sum(UnitCost) as TotalCost
FROM <tablename>
GROUP BY Job





Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2002-04-18 : 10:08:22
look at the GROUP BY function


Select Job#, sum(UnitCost) from mytable
group by Job#


Edit...included too many columns in original Group By statement

Edited by - AndrewMurphy on 04/18/2002 10:59:50
Go to Top of Page

lanejc
Starting Member

11 Posts

Posted - 2002-04-18 : 10:08:39
I knew it was probably a simple thing, thanks a bunch.

Go to Top of Page
   

- Advertisement -