Skip to content
Advertisement

Update Multiple Values in a SQL query based on row values

Current Table

Is there a possible way only use SQL Update query to update July Data to August data? E.g. The Value of B in July should be updated to 12. Dimension Key is the primary key and Name is the “durable” key. CURSOR is allowed to use but not preferred.

Advertisement

Answer

You must join the table to itself to set different records of it in relation.

If you want to do this for corresponding days of the whole month you change the query like this

for a whole year you would write

for all the years:

However, this simple approach does not work if the months are in different years. And also the months have a different number of days. Since I do not know your real use case I am not going into more detail here.

Instead of writing SET A.Value = B.Value you can simply write SET Value = B.Value since the target table is specified in UPDATE A.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement