user3255246
user3255246

Reputation: 45

SQL Server 2008 - Aggregate totals

I am new to SQL, this is what I am trying to figure out. Any help will be appreciated.

This is what I have as Input.

Number   Start        End          Age
PRB1     1/2/2014     1/2/2014      0
PRB1     1/2/2014     1/3/2014      1
PRB1     1/3/2014     1/6/2014      1
PRB2     1/13/2014    1/14/2014     1
PRB3     1/9/2014     1/9/2014      0 
PRB4     1/22/2014    1/22/2014     0
PRB4     1/25/2014    1/28/2014     1

This is the output I am expecting

Number      Age
PRB1         2
PRB2         1
PRB3         0
PRB4         1

Upvotes: 1

Views: 77

Answers (4)

Nagaraj S
Nagaraj S

Reputation: 13484

It will work

SELECT number, 
       Sum(age) AS Age 
FROM   tablename 
GROUP  BY number 

SQL FIDDLE

Upvotes: 3

Ajay
Ajay

Reputation: 6590

Try this :

SELECT Number, SUM(Age) AS Age FROM Test GROUP BY Number

See GROUP BY in more details :

http://www.w3schools.com/sql/sql_groupby.asp

Upvotes: 0

A.J
A.J

Reputation: 382

You need to use the Sum Aggregate function:

SELECT 
    NUMBER
    ,SUM(AGE) AS AGE
FROM 
    MYTABLE
GROUP BY 
    NUMBER

Upvotes: 1

CRAFTY DBA
CRAFTY DBA

Reputation: 14925

Just group by number, sum by age.

select number, sum(age) as total
from my_table
group by number

Upvotes: 1

Related Questions