Skip to content
Advertisement

How can I “Update Select” some values in a column with a subquery?

I am using Apache Derby DB (SQL) version 10.14 and this is the Derby Reference Manual: https://db.apache.org/derby/docs/10.14/ref/refderby.pdf

I am trying to create a star schema and am currently working on the region dimension table.

These are the tables I am using: DWH_PRICE_PAID_RECORDS enter image description here

DWH_POSTCODES enter image description here

And DIM_REGION enter image description here

First I inserted Town(unique), County and District:

enter image description here

Town has to be unique because I dont have anything else as identifier to match/link the tables. The DIM_REGION table now has 938 unique town records and I want to “UPDATE” the remaining columns “Latitude”, “Longitude” and “COUNTRY_STRING”. The DWH_POSTCODES table has 1637 unique town records which means the towns are different! Only 532 towns are matching and only those shall be updated in the DIM_REGION table.

I’m starting with just one column “Latitude” and this is what I would expect it to look like after the Update statement: enter image description here

but when I do that I get this error message (the error message is german and I had to translate, sorry):

I don’t understand why. If I commit just the select statement:

I get exactly what I expected to get and thats the whole column with all latitudes.

Thank you very much!

Advertisement

Answer

IBM Db2 and Apache Derby DB are completely different products. So, remove the db2 tag.
You provided absolutely non-matched lists of towns in both tables. Was it so hard to prepare matched data sets?

If you don’t have duplicates in the dwh_postcodes.town column, then update should look like this (may be with the same subselect in the EXISTS predicate in WHERE):

Otherwise you have to use some aggregate function (or the fetch first clause if Derby supports it) to make this subselect return only 1 row as shown commented out in the example above.

Alternatively, you could do 1 INSERT instead of INSERT and UPDATE to fill all 4 columns:

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