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 2005 Forums
 Transact-SQL (2005)
 Sum rows with condition

Author  Topic 

MadJoker
Starting Member

2 Posts

Posted - 2009-06-28 : 09:32:32
Hi,

I have the following problem: I have a table that looks something like this:

Date User Actions
01/01/09 1 10
02/02/09 1 23
04/04/10 1 12
05/05/10 2 15

What I want to do is create a view that shows how many actions a user made per year. So the expected result would be something like this:

Year User Actions
09 1 33
10 1 12
10 2 15

I know how to get the year out of the date. So my idea was to simply sum up the values in every row where year and user a the same, but I have no idea how to do this, so any help would be appreciated.

MadJoker

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-28 : 09:41:25
[code]
select [year] = datepart(year, [Date]),
User,
Actions = sum(Action)
from table
group by datepart(year, [Date]), User
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

MadJoker
Starting Member

2 Posts

Posted - 2009-06-28 : 10:25:05
Thanks man, worked perfectly. The group by function was, what I was looking for.
Go to Top of Page
   

- Advertisement -