2

I have 2 tables:

Table A

 Section | measure_name   |
---------+----------------|
 A       |      col1      |
 B       |      col2      |
 C       |      col3      |

Table B

 Facility | col1| col2| col3| 
----------+-----+-----+-----+
 NY       |  5  | 50  |  90 |

I want to write a query which selects measure_name of Table A from Table B.

1
  • 2
    Can you show the desired results based on the sample data you've posted? Commented Nov 21, 2018 at 18:13

1 Answer 1

2

It seems you want to get the the values from Table B for the columns corresponding to each measure_name

If yes, you use UNNEST() to convert the Table B into rows and then join it with table A.

with c AS
( SELECT 
       unnest(array['col1', 'col2', 'col3']) AS colname,
       unnest(array[col1, col2, col3]) AS value
FROM B
) 
SELECT a.*,c.value FROM A 
JOIN C ON a.measure_name = c.colname;

This gives you

Section measure_name value
A       col1          5
B       col2         50
C       col3         90

Demo

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

Comments

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.