sql - Can I alias multiple columns? How? -
i'm using pyodbc , postgres.
can alias multiple columns?
here's description of problem:
data structure:
data id | c1 | c2 ------------- 1 | 11 | 12 2 | 21 | 22
notation: c column
dictionary id | key | value ---------------- 1 | k1 | v11 1 | k2 | v12 2 | k1 | v21 2 | k2 | v22
notation: k key, v value
you can think of k1 , k2 2 more columns. data structure way because it's changing. didn't design it, have go it.
i can't figure out sql query give me following (most importantly, row, can access k1 , k2 columns name):
data id | c1 | c2 | k1 | k2 ------------------------- 1 | 11 | 12 | v11 | v12 2 | 21 | 22 | v21 | v22
the problem keep running if alias tables, sql result contain 2 "key" columns dictionary table, meaning can't control column access of two, if alias rows, can't control tables being referenced inside sql statement.
the fix i'm thinking alias 2 columns:
select * data full join dictionary a1,a2,a3 on data.id = a1 full join dictionary a4,a5,a6 on data.id = a4 a2 = k1 , a5 = k2
notation: alias
the result of theoretically like
data id | c1 | c2 | a3 | a6 ------------------------- 1 | 11 | 12 | v11 | v12 2 | 21 | 22 | v21 | v22
note a's technically here, 3 , 6 ones i'm interested in
you can alias entire table, example dictionary d1
. refer column names in table d1.col1
. example:
select d.id , d.c1 , d.c2 , d1.value a3 , d2.value a6 data d left join dictionary d1 on data.id = d1.id , d1.key = 'k1' left join dictionary d2 on data.id = d2.id , d2.key = 'k2'
Comments
Post a Comment