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
 Old Forums
 CLOSED - General SQL Server
 Add rows together based on same column value

Author  Topic 

kieran5405
Yak Posting Veteran

96 Posts

Posted - 2007-05-15 : 06:58:13
Hi,

I am trying to get this working in a sql query. I have the below query which gets the username and adds two column values together to get a total amount.

SELECT NTuser, Convert(VARCHAR(100), sum(TravelAmount + SubAmount), 1) as 'TotalAmount' FROM Expenses

However, there will be many rows for the same username so what I need to do is return only one row per username with the total of all the users 'TotalAmount'.

Any help much appreciated.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-05-15 : 07:00:59
[code]
SELECT NTuser, sum(TravelAmount + SubAmount) as 'TotalAmount'
FROM Expenses
group by NTuser
[/code]

Why do you want to convert the amount to string ?


KH

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-05-15 : 07:08:10
See if there is a difference in execution plan

SELECT NTuser, sum(TravelAmount) + sum(SubAmount) as 'TotalAmount'
FROM Expenses
group by NTuser


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

kieran5405
Yak Posting Veteran

96 Posts

Posted - 2007-05-15 : 07:22:02
sorry Peso...that of course works. i got myself confused and started messing with different ways of doin this thinking it was a lot more complicated than it was...

khtan - i was changing it to a string to put it in a declared value in code.

thanks for replying so quick. i dupe posted this as when i posted it first i saw that the forum was closed. sorry about that.
Go to Top of Page
   

- Advertisement -