March 13, 2013 at 7:15 am
I know this is a simple question. However, I'm not sure where to start. I'm new to SQL and would appreciate any help offered.
How would I sum the following in example 1 and have it display as shown in example 2?
--Example 1
Expense_Id Expense Price Expense_By
1 Coffee 5.00 Jim
2 Coffee 3.00 Jim
3 Coffee 3.00 Mike
4 GAS 3.00 Jim
5 GAS 3.00 Jim
6 GAS 3.00 Mike
7 GAS 3.00 Mike
--Example 2
Expense_By ExpenseTotal_Price
Jim GAS 6.00
Mike GAS 6.00
Jim Coffee 8.00
Mike Coffee 3.00
March 13, 2013 at 7:21 am
You need an aggregate function, specifically SUM in this case.
http://msdn.microsoft.com/en-us/library/ms187810.aspx
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 13, 2013 at 8:22 am
--Think this should do it---
select
Expense_by
, Expense
,SUM(Price) as 'Total Price'
from -- Your table name --
group by Expense_by
, Expense
March 13, 2013 at 8:24 am
Yes, that's right.
Here's a helpful instructional video on aggregation:
http://www.youtube.com/watch?v=428B57dOxcE
It might be helpful for you to take 25 minutes and watch it so you can understand aggregation.
March 15, 2013 at 3:35 am
Hi,
This will solve you problem.
SELECT
ESPENSE_BY,
EXPENSE,
SUM(PRICE)
FROM
TABLE_NAME -----use table name
GROUP BY
ESPENSE_BY,EXPENSE
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy