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
 General SQL Server Forums
 New to SQL Server Programming
 Troubles with SUM

Author  Topic 

ScottBot
Starting Member

16 Posts

Posted - 2011-02-10 : 12:05:32
I have a table with the following fields Item_ID, itemName and costPerItem.

Using the costPerItem field I run a calculation to find a weekly cost based on a 5 day week and generate the answer in a new column (which I name weeklyTotal). Is there a way to add all the values in the weeklyTotal column to find a grand total of all items together using SUM?

If this was an existing column in the table it would work fine, as this column(weeklyTotal) is generated I can't get it to work.

Anyone with a work around or a better way?

Cheers

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-02-10 : 12:13:07
can you come up with some sample data and the required output format of that data?
Go to Top of Page

ScottBot
Starting Member

16 Posts

Posted - 2011-02-11 : 05:26:02
itemName | costPerItem |Cast(costPerItem * 5 AS decimal(6 , 2)) AS || weeklyTotal

wood | £5.00 | || £25.00
steel | £15.00 | || £75.00
plastic | £2.00 | || £10.00
==============
grandTotal £110.00
==============

the result I am looking for would be grandTotal (wood + steel + plastic) = £110.00

hope this is more clear
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-02-11 : 05:42:20
considering the table structure as following

Item_ID, itemName and costPerItem
1, Wood, 5.00
2, Steel, 15.00
3, Plastic, 2.0


If this is how your table structure and data is placed then check the below query


Select Sum(isnull(CostPerItem,0))*5 AS WeeklyTotalForAllItems
From TableName
Go to Top of Page

ScottBot
Starting Member

16 Posts

Posted - 2011-02-11 : 07:26:45
That has worked a charm. Appreciate your time and knowledge sharing.

Thank you very much
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-02-11 : 07:37:01
you are welcome ..

Cheers
MIK
Go to Top of Page
   

- Advertisement -