Update two columns with same SUM and Group BY

I want to update with the value in this query, but it's saying errors... and i'm trying to minimize it.

update p
set a10=t.sumgc
from temp as p inner join 
(SELECT     SUM(gcsanad) as sumgc,good_gr
fROM         dbo.fgoodgroup 
WHERE     (flag = 2)
group by good_gr ) as t
on t.good_gr=p.a3
,
a11=t.sumgc
from temp as p inner join 
(SELECT     SUM(gcsanad) as sumgc,good_gr
fROM         dbo.fgoodgroup 
WHERE     (flag = 4) 
group by good_gr ) as t
on t.good_gr=p.a3

Upvotes: 1

Views: 257

Answers (1)

Pranay Rana
Pranay Rana

Reputation: 176916

try like this

update p
set a10=t.sumgc
,
a11=t.sumgc1

from temp as p inner join 
(SELECT 
  (case when  flag = 2 then   SUM(gcsanad)  end) as sumgc,
  (case when  flag = 4 then   SUM(gcsanad)  end) as sumgc1
fROM         dbo.fgoodgroup 
group by good_gr ) as t
on t.good_gr=p.a3

Upvotes: 1

Related Questions