MySQL - Combining multiple WHEN conditions in CASE

前端 未结 1 1899
醉梦人生
醉梦人生 2021-01-01 16:26

I’m trying to combine 2 rows of results into 1 using the below CASE clause. ‘<26’ should only appear once, and the results should be combined.

SELECT  
          


        
相关标签:
1条回答
  • 2021-01-01 17:08

    how about this?

    SELECT  CASE 
              WHEN org.size IN (0, 1) THEN '<26'
              WHEN org.size = 2 THEN '26-50'
              WHEN org.size = 3 THEN '51-100'
              WHEN org.size = 4 THEN '101-250'
              WHEN org.size = 5 THEN '251-500'
              WHEN org.size = 6 THEN '501-1000'
              WHEN org.size = 7 THEN '1001-5000'
            ELSE '5000+' END AS Size, ....
    

    The problem is that you are grouping the records org.size causing <26 at two different groups since they are originally 0 and 1.

    This will work,

    GROUP BY CASE 
              WHEN org.size IN (0, 1) THEN '<26'
              WHEN org.size = 2 THEN '26-50'
              WHEN org.size = 3 THEN '51-100'
              WHEN org.size = 4 THEN '101-250'
              WHEN org.size = 5 THEN '251-500'
              WHEN org.size = 6 THEN '501-1000'
              WHEN org.size = 7 THEN '1001-5000'
            ELSE '5000+' END
    
    0 讨论(0)
提交回复
热议问题