Alonso
Alonso

Reputation: 251

substring_index function does not exist error

I have a table named 'data' which stores the email addresses and city of users and I want to find the most popular email domain. I use the following query to to find the rows which have maximum value. exapmple for my table:

Email           Name        City
[email protected]     John    California
[email protected]       Leo sydney
[email protected]      Ross    NY
[email protected]      Ronny   Canberra
[email protected]       Monty   London
[email protected]      Jim washington
[email protected]      Finn    Las vegas

I have computed the answer using this query

  select  x.city, x.No_of_People from (select e.city, count(e.city) as No_of_People from data e group by  e.city) x
where  x.No_of_People = (select max(x2.No_of_People) from (select e2.city, count(e2.city) as No_of_People from data e2 group by         e2.city) x2)

But i don't wan't to use limits as it does not return multiple rows. So I have used the following query using this answer

    select
  x.substring_index (email,'@',-1),
  x.No_of_Users
from
  (select
    e.substring_index (email,'@',-1), 
    count(e.substring_index (email,'@',-1)) as No_of_Users
  from
    data e
  group by 
    e.substring_index (email,'@',-1)) x
where
  x.No_of_Users = 
    (select
      max(x2.No_of_Users)
    from
      (select
         e2.substring_index (email,'@',-1),
         count(e2.substring_index (email,'@',-1)) as No_of_Users
      from
         data e2
      group by 
        e2.substring_index (email,'@',-1)) x2)

The query that i'm using is giving this error "FUNCTION e2.substring_index does not exist". help me.

Upvotes: 0

Views: 3882

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271151

Your syntax is wrong for using the function. The alias goes on the column, not the function. For instance, your last subquery should use this syntax:

  from (select substring_index(e2.email,'@',-1) as strind,
               count(substring_index(e2.email,'@',-1)) as No_of_Users
        from data e2
        group by substring_index (e2.email,'@',-1)
       ) x2

I also named the first column, so you can refer to it outside the subquery if you want.

To count the number of occurrences in a string, use this trick:

(length(e2.email) - length(replace(e2.email, '@', '')) as numAmpersands

Upvotes: 3

Related Questions