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

前端 未结 5 1453
暖寄归人
暖寄归人 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条回答
  •  -上瘾入骨i
    2021-02-20 14:33

    I would do it this way -- only requires a single change to add additional types of fruit.

    WITH fruits AS (
      SELECT 'APPLE' fruit FROM DUAL
      UNION ALL
      SELECT 'ORANGE' fruit FROM DUAL
    )
    SELECT fruit, count(*)
    FROM MESA m, fruits
    WHERE m.str LIKE '%FRUIT%'
    AND m.str LIKE '%' || fruits.fruit || '%'
    GROUP BY fruit
    

    If your strings are reliably in the format you showed in your sample data, I would consider changing the predicate to one condition, WHERE m.str LIKE 'FRUIT%' || fruits.fruit ||'%'.

提交回复
热议问题