Search code examples
sqlintersystems-cache

SQL adding two columns and group by count


I want to add two columns and group by the count. For example say I have the following table:

ID -------- value1 ---------- value2    
A ------------ 2 -----------------3    
B ------------ 1 -----------------4    
c ------------ 2 -----------------2   
D ------------ 3 -----------------3   
E ------------ 2 -----------------1
F ------------ 1 -----------------3

Ff you count the sum (select value1 + value 2) in each rows you will get 5,5,4,6,3,4.

I would like to get the following result.

5 ------ 2    
4 ------ 2   
3 ------ 1   
6 ------ 1

Solution

  • You can just do:

    select (val1 + val2), count(*)
    from t
    group by (val1 + val2)
    order by count(*) desc;