Search code examples
sqlvoltdb

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


Is it possible to split a column into the two columns when executing query based on filter value.

For example, given database schema

x varchar
a integer
b 0,1

I can execute a query

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

That will end up with

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

Now I want to calc distinct sum for a if b is equal to 0 and 1

So the result set should be

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

Of cause I can execute

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

But that will require additional application side transformation


Solution

  • In most databases, you can do conditional aggregation:

    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 appears to support case (here), so this should work.