Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 如何在红移中创建包含列作为值的列?_Amazon Web Services_Amazon Redshift - Fatal编程技术网

Amazon web services 如何在红移中创建包含列作为值的列?

Amazon web services 如何在红移中创建包含列作为值的列?,amazon-web-services,amazon-redshift,Amazon Web Services,Amazon Redshift,我有如下数据: id start_date end_date col1 col2 col3 col4 col5 issue_2017-09 2017-09-18 2017-09-30 true true true true false 我想将数据转换为以下格式: id start_date end_date new_col issue_2017-09

我有如下数据:

id                  start_date   end_date   col1   col2   col3   col4   col5
issue_2017-09       2017-09-18  2017-09-30  true   true   true   true   false
我想将数据转换为以下格式:

id                 start_date    end_date     new_col
issue_2017-09      2017-09-18    2017-09-30   {'col1', 'col2', 'col3', 'col4'}
新的_col是从列[col1、col2、col3、col4、col5]中创建的,这些列是正确的。
另外,我正在使用红移。

我能够通过以下查询解决此问题:

select id, start_date , end_date, listagg(col_name, ', ') as new_col
from (
   select id, start_date, end_date, col1 as val, 'col1' as col_name
   from table
   union all
   select id, start_date, end_date, col2 as val, 'col2' as col_name
   from table
   union all
   select id, start_date, end_date, col3 as val, 'col3' as col_name
   from table
   union all
   select id, start_date, end_date, col4 as val, 'col4' as col_name
   from table
   union all
  select id, start_date, end_date, col5 as val, 'col5' as col_name
   from table
) t
where val is True
group by id, start_date, end_date  

我可以通过以下查询解决此问题:

select id, start_date , end_date, listagg(col_name, ', ') as new_col
from (
   select id, start_date, end_date, col1 as val, 'col1' as col_name
   from table
   union all
   select id, start_date, end_date, col2 as val, 'col2' as col_name
   from table
   union all
   select id, start_date, end_date, col3 as val, 'col3' as col_name
   from table
   union all
   select id, start_date, end_date, col4 as val, 'col4' as col_name
   from table
   union all
  select id, start_date, end_date, col5 as val, 'col5' as col_name
   from table
) t
where val is True
group by id, start_date, end_date  

这是另一种方法

select
  id, start_date, end_date, 
  '{' + 
  case when col1 then '''col1''' else '' end +
  case when col2 then case when col1 then ', ''col2''' else '''col2''' end else '' end +
  case when col3 then case when (col1 or col2) then ', ''col3''' else '''col3''' end else '' end +
  case when col4 then case when (col1 or col2 or col3) then ', ''col4''' else '''col4''' end else '' end +
  case when col5 then case when (col1 or col2 or col3 or col4) then ', ''col5''' else '''col5''' end else '' end +
  '}' as new_col 
 from table01;

这是另一种方法

select
  id, start_date, end_date, 
  '{' + 
  case when col1 then '''col1''' else '' end +
  case when col2 then case when col1 then ', ''col2''' else '''col2''' end else '' end +
  case when col3 then case when (col1 or col2) then ', ''col3''' else '''col3''' end else '' end +
  case when col4 then case when (col1 or col2 or col3) then ', ''col4''' else '''col4''' end else '' end +
  case when col5 then case when (col1 or col2 or col3 or col4) then ', ''col5''' else '''col5''' end else '' end +
  '}' as new_col 
 from table01;