Skip to content
Advertisement

Why does my “INSERT INTO” Statement not work?

I am working with Apache derbyDB (db2) and SQL Workbench as environment. I have 10k records of data and I want to create a star schema with dimension tables and a fact table.

dwh_price_paid_records = data table
dm_price_paid_records = data mart table (facts table)
dim_time =  data mart table (dimension)

DWH_PRICE_PAID_RECORDS.TRANSACTION_ID = VARCHAR(50)
DWH_PRICE_PAID_RECORDS.PRICE = INTEGER
DWH_PRICE_PAID_RECORDS.DATE_OF_TRANSFER = DATE
DIM_TIME.DATE_ID = Primary key of dim_time (autoincrement start with 1 increment by 1)
DM_PRICE_PAID_RECORDS.DATE_ID = Foreign key NOT NULL from DIM_TIME

In the Facts table (DM_PRICE_PAID_RECORDS) I only want to have the the primary key, the price (fact) and the ID, to have a relation to the dimension table but its not working.

INSERT INTO DM_PRICE_PAID_RECORDS (TRANSACTION_ID, PRICE, DATE_ID) VALUES (SELECT TRANSACTION_ID FROM DWH_PRICE_PAID_RECORDS, SELECT PRICE FROM DWH_PRICE_PAID_RECORDS, SELECT DATE_ID FROM DIM_TIME join DWH_PRICE_PAID_RECORDS on DIM_TIME.DATE = DWH_PRICE_PAID_RECORDS.DATE_OF_TRANSFER WHERE DIM_TIME.DATE = 
DWH_PRICE_PAID_RECORDS.DATE_OF_TRANSFER);

I wouldve expected this as an outcome:

TRANSACTION_ID | PRICE | DATE_ID

1| 2324 | 423

2 | 52315 | 234

But I get an error in german language saying something like: “VALUES-clause must have atleast one element. Emtpy elements are not allowed”

“VALUES-Klausel muss mindestens ein Element enthalten. Leere Elemente sind nicht zulässig.”

Thank you so much! Flippi

Advertisement

Answer

I think you want INSERT . . . SELECT:

INSERT INTO DM_PRICE_PAID_RECORDS (TRANSACTION_ID, PRICE, DATE_ID)
    SELECT ppr.TRANSACTION_ID, ppr.PRICE, t.DATE_ID
    FROM DWH_PRICE_PAID_RECORDS ppr JOIN
        DIM_TIME t 
        ON t.DATE = ppr.DATE_OF_TRANSFER ;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement