user2841280
user2841280

Reputation:

Error querying SQL Server

This is my table

create table #t(id int, amt int)

insert into #t values(1, 40), (1, 20), (1, 10), (2, 100), (2, 120), (2, 400)

I need output like this!

id  amt
1    70
1    70
1    70
2    620
2    620
2    620

I tried

SELECT
    id, SUM(amt) 
FROM
    #t 
GROUP BY
    id 

Upvotes: 3

Views: 75

Answers (3)

Vikram Jain
Vikram Jain

Reputation: 5588

select a.id, c.amt from #t as a
left outer join 
(SELECT b.id, SUM(b.amt) as amt FROM   #t as b  GROUP BY  id ) as c on c.id=a.id

Upvotes: 1

Andomar
Andomar

Reputation: 238296

select  id
,       sum(amt) over (partition by id)
from    #t

Example at SQL Fiddle.

Upvotes: 2

vhadalgi
vhadalgi

Reputation: 7189

Try This !

select id,sum(amt) over(partition by id) as amt from #t

Upvotes: 3

Related Questions