0

i tried to unpivot a table with many columns (unknown number) into a table with four columns.

My query looks like this:

INSERT INTO measurement (timestamp, sensor, value)
SELECT d."Timestamp", 
       ( SELECT s.id FROM sensor s WHERE s.name = columns.column_name),
       ( SELECT columns.column_name from sensor_list where "Timestamp" = d."Timestamp")     
FROM   sensor_list d, 
       (SELECT column_name FROM information_schema.columns WHERE table_name = 'sensor_list' AND column_name <> 'Timestamp' ORDER BY ordinal_position) columns

This doesnt work. The Errormessage i receive is:

"ERROR: >>value<< has type numeric, but the expression is of type  information_schema.sql_identifier"

Is there any chance to get it work by converting "columns.column_name" to string or something else like with quote_ident() ?

All i tried did not work. Thank you very much.

1 Answer 1

1

If the datatype of the column Value is numeric, the code will give error. Do you want to assign column name or column

Sign up to request clarification or add additional context in comments.

1 Comment

I think i want to use column_name. As sensor_list looks like: Timestamp | Sensor1 | Sensor2 | Sensor3 | ... | Sensor 999 | ------------------------------------------------------------- time | value | value | ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.