Amazon Redshift: Selecting columns based on a list of columns present in a table

烂漫一生 提交于 2019-12-31 05:21:29

问题


Is it possible in any way (without scripting) to aggregate columns which are stored in a table cell.

Basically what i am trying to achieve here is that if i have the following table

Table 1
KEYNAME    | COLUMN_NAME
COUNT_USER | USER_ID
SUM_HOURS  | HOURS_WATCHED

Can i do something like

select SUM((select column_name from TABLE1 where KEYNAME = "SUM_HOURS")) FROM MAIN_TABLE

I have the option to script in python via which i know how to achieve this. But i am curious to know if this is possible in any version of SQL or not Please let me know if the information is insufficient.


回答1:


No, you cannot use a column name that comes from the result of another query.

You would need to do this in your own code, sending the result to Amazon Redshift as a complete query. (That is, run one query to obtain the column names, then run a second query with those column names inserted.)



来源:https://stackoverflow.com/questions/50964523/amazon-redshift-selecting-columns-based-on-a-list-of-columns-present-in-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!