3 Replies - 824 Views - Last Post: 05 January 2009 - 12:13 PM Rate Topic: -----

#1 macman007  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 57
  • Joined: 11-November 07

add up the cost column

Posted 05 January 2009 - 10:11 AM

Hi,

I am using SQL 2005 and need help with a calculation.

Basically, amongst other things each record has an initials column and a cost column. I need to run through the table and add up the figures, which would finaly display the total cost for the user - for example:

I would like the query to be displayed as follows:

(example)

initials cost
AD 58
PD 350
JG 75
JB 150

I have used the following test query but cannot work out how to add up the figures and only show the total.

use pd;
select initials,cost from tblmain where [initials] ='AD';

AD 8.00
AD 0.00
AD 6.00
AD 44.00

Any help is much appreciated.

:blink:

ps - I am using the smallmoney data type.

This post has been edited by macman007: 05 January 2009 - 10:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: add up the cost column

#2 P4L  Icon User is offline

  • Your worst nightmare
  • member icon

Reputation: 34
  • View blog
  • Posts: 2,789
  • Joined: 07-February 08

Re: add up the cost column

Posted 05 January 2009 - 10:25 AM

Mac,

Try this

Select Sum(cost)
From tblmain 
where [initials] ='AD';


Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: add up the cost column

Posted 05 January 2009 - 10:26 AM

Try using the SUM Function, like this

select 
		  initials, SUM(cost) 
from 
		  tblmain 
where 
		   [initials] ='AD' 
GROUP BY  
		   initials;



Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#4 macman007  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 57
  • Joined: 11-November 07

Re: add up the cost column

Posted 05 January 2009 - 12:13 PM

Just what I wanted! - A very big thank you to both PsychoCoder and P4L!!!!!!!!!!!!!!

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1