与Redshift(PostgreSQL)的数据透视表

时间:2017-07-27 09:04:37

标签: sql pivot amazon-redshift

我正面临着Redshift的挑战: 我正在尝试动态地将行移动到列中并按计数聚合,但是我注意到数据透视表功能只能从PostgreSQL 9中获得。

有关如何执行以下操作的任何想法吗?

 index   fruit     color 
 1       apple     red           
 2       apple     yellow           
 2       banana    blue           
 2       banana    blue           
 3       banana    blue     
 3       banana    green     
 3       pear      green     
 3       pear      red           

为:

 index   red       yellow    blue    green 
 1       1         0         0       0
 2       0         1         2       0
 3       1         0         1       2

基本上,对每个id的颜色的出现进行分组和计数(水果不是那么重要,虽然我稍后会将其用作过滤器)。

注意:我可能还想稍后进行二进制转换(即0表示0,1表示> 0)

编辑:如果以上情况不可能,那么可以采取任何方式吗?

 index   color     count     
 1       red       1        
 1       yellow    0           
 1       blue      0
 1       green     0 
 2       red       0        
 2       yellow    1           
 2       blue      2
 2       green     0
 3       red       1         
 3       yellow    0           
 3       blue      1
 3       green     2

(蓝色,黄色,蓝色和绿色应该是动态的)

2 个答案:

答案 0 :(得分:1)

对于编辑,您可以

select x.index, x.color, sum(case when y.index is not null then 1 else 0 end) as count
from 
((select index
from [table]
group by index
order by index) a
inner join 
(select color
from [table]
group by color
order by color) b
on 1 = 1) x
left outer join
[table] y
on x.index = y.index
and x.color = y.color
group by x.index, x.color
order by x.index, x.color

答案 1 :(得分:0)

如果Redshift中没有PIVOT,那么您可以随时使用标准数据透视查询:

SELECT
    index,
    SUM(CASE WHEN color = 'red'    THEN 1 ELSE 0 END) AS red,
    SUM(CASE WHEN color = 'yellow' THEN 1 ELSE 0 END) AS yellow,
    SUM(CASE WHEN color = 'blue'   THEN 1 ELSE 0 END) AS blue,
    SUM(CASE WHEN color = 'green'  THEN 1 ELSE 0 END) AS green
FROM yourTable
GROUP BY index