Count occurrences followed by a value in a table

2

Hello, I have a table that has a value ID and other values among which is nombre .

###############
# ID # Nombre #
###############
# 4  # Juan   #
###############
# 3  # Juan   #
###############
# 2  # Juan   #
###############
# 1  # Martin #
###############
# 0  # Juan   #
###############

Is it possible through a MySql query to obtain the occurrences followed by the most recent name (in this case Juan)?

That is to say that from the most recent name entered in the table (in this case Juan ) its occurrences are counted until another value interrupts the count (in this case Martin ).

In the example table the returned value should be 3 because the last name entered in the table was John and had 3 occurrences before the name Martin appeared.

    
asked by Cristofer Fuentes 17.03.2017 в 05:51
source

2 answers

1

Let's imagine that this is your table:

select * from nombres order by id;

id  nombre
0   Juan
1   Martin
2   Juan
3   Juan
4   Juan
5   Juan
6   Martin
7   Juan
8   Juan
9   Juan

Then the last name comes out of the query

(select nombre from nombres order by id desc limit 1)

You can obtain, in a query, the maximum ID of the table next to the maximum value other than the last name:

select max(id) maximo_distinto, 
       (select max(id) from nombres) as ultimo_id 
from nombres 
where nombre !=(select nombre 
                from nombres 
                order by id desc 
                limit 1);

And that returns

maximo_distinto ultimo_id
6               9

So the desired result is obtained from the subtraction between the two.

Now, what happens when someone removes an intermediate value?

id  nombre
0   Juan
1   Martin
2   Juan
3   Juan
4   Juan
5   Juan
6   Martin
8   Juan
9   Juan

The result should be 2, but with the query I showed you, the subtraction of the IDs would continue saying 3. You have to try another solution.

For example, you can select all the records where the ID is greater than the largest ID other than the last name (it is already sounding gimmicky)

select * 
from nombres 
where id >( select max(id) maximo_distinto 
            from nombres 
            where nombre !=( select nombre 
                             from nombres 
                             order by id DESC
                             limit 1));

And that returns

id  nombre
9   Juan
8   Juan
7   Juan

Why it's a matter of doing

select count(*) as juanes
from nombres 
where id >( select max(id) maximo_distinto 
            from nombres 
            where nombre !=( select nombre 
                             from nombres 
                             order by id DESC
                             limit 1));

And with that you get

juanes
3
    
answered by 17.03.2017 / 16:56
source
1

Try this Query

SELECT COUNT(*) FROM Tabla 
WHERE id NOT IN
(
SELECT id
FROM Tabla AS Tabla_1
WHERE nombre <> (
                  SELECT nombre FROM Tabla 
                  WHERE Tabla.id > Tabla_1.id 
                  ORDER BY id ASC 
                  LIMIT 1
                )
);

To see the example working click here

    
answered by 17.03.2017 в 08:21