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 a conclusion 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 
+3
source share
3 answers

Try it!

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

Example in SQL Fiddle.

+2
source
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
+1
source

All Articles