3

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 
0

3 Answers 3

3

Try This !

select id,sum(amt) over(partition by id) as amt from #t
Sign up to request clarification or add additional context in comments.

Comments

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

Example at SQL Fiddle.

1 Comment

@ta.speot.is: I think SQL Server 2008 and later
1
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.