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

会有一股神秘感。 提交于 2020-01-12 14:26:08

问题


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 with one of the columns is a huge string. And I am counting the number of rows matching particular patterns:

SELECT m.str, count(*)
FROM MESA m
WHERE m.str LIKE '%FRUIT%'
AND (m.str LIKE '%APPLE%' OR m.str LIKE '%ORANGE%')

So let's assume the result of this query is:

FRUIT..afsafafasfa...RED_APPLE 20

FRUIT..afsafafasfa...YELLOW_APPLE 12

FRUIT..afsafafasfa...GREEN_APPLE 3

FRUIT..afsafafasfa...PURPLE_ORANGE 4

FRUIT..afsafafasfa...RED_ORANGE 45

But I want my results to be:

APPLE 35

ORANGE 49


Is this possible to do? If so, how so? : )

Comments and code snippets are much appreciated.

PS: Of course the query and the results are more complicated than the above example. I just wrote it like for the sake of simplicity to explain.

Cheers..


回答1:


Sure:

WITH Fruits AS (
    SELECT 
        CASE 
           WHEN m.str LIKE '%APPLE%' THEN 'Apple'
           WHEN m.str LIKE '%ORANGE%' THEN 'Orange' 
        END AS FruitType           
    FROM MESA m
    WHERE m.str LIKE '%FRUIT%')
SELECT FruitType, COUNT(*) 
FROM Fruits
WHERE FruitType IN ('Apple', 'Orange')
GROUP BY FruitType;



回答2:


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 ...




回答3:


SELECT count(*) AS 'Apples'
FROM MESA m
WHERE m.str LIKE '%FRUIT%'
AND m.str LIKE '%APPLE%'

SELECT count(*) AS 'Oranges'
FROM MESA m
WHERE m.str LIKE '%FRUIT%'
AND m.str LIKE '%ORANGE%'

Would that work?




回答4:


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



回答5:


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 ||'%'.



来源:https://stackoverflow.com/questions/3299330/sql-is-it-possible-to-group-by-according-to-like-functions-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!