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

前端 未结 5 1447
暖寄归人
暖寄归人 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:26

    Another variant of David Markle answer:

    SELECT 
      fruit_name,
      count(1)  as fruit_count
    FROM (
      SELECT 
        CASE 
          WHEN m.str LIKE '%APPLE%' THEN 'Apple'
          WHEN m.str LIKE '%ORANGE%' THEN 'Orange' 
        END                                         as fruit_name
      FROM 
        MESA m
      WHERE 
        m.str LIKE '%FRUIT%'
        AND 
        (m.str LIKE '%APPLE%' OR m.str LIKE '%ORANGE%')
    )
    GROUP BY 
      fruit_name  
    

    Same thing, but only 1 CASE required, which simplifies support ...

提交回复
热议问题