根据查询中的过滤值将一列拆分为两列

Split a column into two columns based on filter value in query

根据过滤值执行查询时是否可以将一列拆分为两列

例如,给定的数据库架构

x varchar
a integer
b 0,1

我可以执行查询

select x , sum(a) as asum from t group by x

最后会是

 x | asum 
---+-----
...| ...

如果 b 等于 0 和 1,现在我想计算 a 的不同总和

所以结果集应该是

 x | as for x = 0 | as for x = 1
---+--------------+---------------
...|    ...       |  ...

因为我可以执行

select x , sum(a) as asum, b as as from t group by x, b

但这将需要额外的应用程序端转换

在大多数数据库中,您可以进行条件聚合:

select x,
       sum(case when b = 0 then a else 0 end) as a_0,
       sum(case when b = 1 then a else 0 end) as a_1
from t
group by x;

Voltdb 似乎支持 case (here),所以这应该有效。