Apache Hive (version 1.2.1000.2.6.5.0-292)
I have a table, A, that has a large number of columns. I'm trying to select only the columns that I need from A and the columns I want live in a key-value pair table, B. (Example below). I can query B to get the columns I need, but I'm struggling to put the output of this sql query as the columns of the query used in A. Is there a way to do this in one sql query? I can write a python program to do this to create the SQL, but I'd rather have it in just one query for simplicity to the end-user.
DDL for tables
create table A (
a1 string,
a2 string,
a3 string,
b1 string,
b2 string,
b3 string,
)
create table B (
key string,
value string,
)
Data in table B (key value table). It should be noted that the data in the column value
cannot be inferred upon by the corresponding value in column key
. I have written them as a1,a2
for simplicity.
key,value
a,a1
a,a2
a,a3
b,b1
b,b2
b,b3
Query to get the correct columns = select value from B where key='a'
When you merge the results from this query with the Table A query you should get this sql statement
select a1,a2,a3 from A
As you can see, we are trying to derive the columns used in Table A
My first attempt doesn't work:
select
(select value from B where key='a')
from A
What's the right way to do this?
Thanks in advance!