Thread: Problem: Joining each column of a "one row query" with the result of a "many row query"
Problem: Joining each column of a "one row query" with the result of a "many row query"
From
"Christian Hofmann"
Date:
Hello, I have a query giving me the column_names with an unknown amount of rows. This query is dynamically created by a pgsql function with the following query inside: select column_name from information_schema.columns where table_schema='my_schema' and table_name='my_table' and "column_name" <> 'not_this_column1' and "column_name" <> 'not_this_column2' and "column_name" <> 'not_this_column3' Now I get some rows like: column_name ------------ column_name1 column_name2 column_name3 . . . There is an other query with one row result inside my pgsql function making use of the result from the above query. The Part "column_name1, column_name2, column_name3" the pgsql function can create on its own: For example: select column_name1, column_name2, column_name3, ... from my_schema.my_table where my_column1='abc' and my_column2=5 Now I get ONE row like this: column_name1 | column_name2 | column_name3 | ... -------------+--------------+--------------+---- value_clm1 | value_clm2 | value_clm3 | ... In pgsql I do not get the column name in this query, right? That's sad, because it would nearly solve my problem. I need to find a way to get the result of the query in this way: column_name | Value -------------+----------- column_name1 | value_clm1 column_name2 | value_clm2 column_name3 | value_clm3 . . . In my opinion there are these ways? 1. combining both querys with a join, subselect or something like this? 2. Getting Postgres-SQL to write the column name into one row, how? Can someone give me a hint to solve this problem? Or maybe someone has a codesniplet? Thank you for your help, Christian