0

I want to ask about simple sum query. In this question, I attached a picture to let you guys see my table. Now, I want to calculate the Januari's Target + Februari's Target = Februari's Target_YtD What I have got so far is something like this

SELECT SUM(Target) as 'Target_YtD' from revenue where Bulan =
"Januari" or Bulan = "Februari"

But the query above is only produce the final result of the calculation, and what I want is, I want to put the result in the "revenue" table (which is for Februari's Target_YtD column to be exactly). I really appreciate for those who can help me to figure out. Thank you

Mohit S
  • 13,723
  • 6
  • 34
  • 69

2 Answers2

0

I suppose that revenue table contains a column named 'Target_YtD' and you want to update this field with sum function for Januari and Februari target values

Use something like that

update revenue
set Target_YtD=(select SUM(target) from revenue where Bulan in ('Januari', 'Februari'))
where Bulan='Februari'
Carlos Leyva
  • 101
  • 2
0

While Carlos's answer is fully logical and correct, it won't work. More information here.

A quote from the answer in the link:

That is, if you're doing an UPDATE/INSERT/DELETE on a table, you can't reference that table in an inner query (you can however reference a field from that outer table...)

This is the updated solution, based on Carlos's answer:

update revenue
set Target_YtD=(select SUM(target) from (select * from revenue) as r where r.Bulan in ('Januari', 'Februari'))
where Bulan='Februari'
Community
  • 1
  • 1
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41