AxOn
AxOn

Reputation: 101

using of GROUP BY with condition, how?

I imagine how i can solve my task:

SELECT row, val1, val2, SUM(1) AS total
FROM mess
if(row < 0){ GROUP BY val1, val2 } if(row > 0) {GROUP BY val1} 

I need to use two different GROUP BY in one SELECT, is it possable?

Upvotes: 1

Views: 52

Answers (2)

Uri Goren
Uri Goren

Reputation: 13672

Try this:

SELECT row, val1, val2, SUM(1) AS total
FROM mess
GROUP BY if(row>=0,val1,val1+','+val2)

Upvotes: 0

StanislavL
StanislavL

Reputation: 57381

SELECT row, val1, val2, SUM(1) AS total, 
    CASE WHEN row<0 THEN val2 ELSE 0 END as group2
FROM mess
GROUP BY val1, group2

If row>0 all the values will be the same so group result will count val1 only

Upvotes: 1

Related Questions