SQL: Is it possible to 'group by' according to 'like' function's results?

前端 未结 5 1412
暖寄归人
暖寄归人 2021-02-20 13:44

I am using Oracle SQL and I want to group some different rows that \'like\' function results. To elaborate with an example:


Let\'s assume I have a table MESA w

5条回答
  •  醉话见心
    2021-02-20 14:34

    Something like this?

    SELECT Fruit,
          SUM(counter)
     FROM ( SELECT CASE 
                       WHEN m.str LIKE '%APPLE%'
                           THEN 'APPLE'
                       ELSE 'ORANGE'
                   END AS Fruit
                   COUNT(*) AS counter
              FROM MESA m 
             WHERE m.str LIKE '%FRUIT%' 
               AND (m.str LIKE '%APPLE%' OR m.str LIKE '%ORANGE%')
             GROUP BY m.str
          )
    GROUP BY Fruit
    

提交回复
热议问题