Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 576
someone Avatar asked Jul 21 '10 12:07

someone


People also ask

How do I group results in SQL?

The SQL GROUP BY Statement The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

Can we use GROUP BY and WHERE in same query?

GROUP BY clause is used with the SELECT statement. In the query, GROUP BY clause is placed after the WHERE clause.

What happens when you use an aggregate function with a GROUP BY clause?

The GROUP BY clause is normally used along with five built-in, or "aggregate" functions. These functions perform special operations on an entire table or on a set, or group, of rows rather than on each row and then return one row of values for each group.

What are the rules for GROUP BY SQL?

The GROUP BY Clause SQL is used to group rows with same values. The GROUP BY Clause is used together with the SQL SELECT statement. The SELECT statement used in the GROUP BY clause can only be used contain column names, aggregate functions, constants and expressions.


2 Answers

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;
like image 108
Dave Markle Avatar answered Oct 19 '22 20:10

Dave Markle


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

like image 41
ThinkJet Avatar answered Oct 19 '22 19:10

ThinkJet