Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
¿Cómo AGRUPAR POR el mismo valor en diferentes filas con condición? (MYSQL)

tengo esta tabla:

 +-----------+-----------+---------------------+ | id | member_id | date | +-----------+-----------+---------------------+ | 1 | 2 | 2020-07-27 21:53:46 | | 2 | 1 | 2020-07-27 22:03:58 | | 3 | 1 | 2020-07-27 22:09:16 | | 4 | 1 | 2020-07-27 22:11:33 | | 5 | 2 | 2020-07-27 22:12:21 | -----------------------------------------------

Y me gustaría GROUP BY algo como esto:

 +-----------+-----------+---------------------+ | id | member_id | date | +-----------+-----------+---------------------+ | 1 | 2 | 2020-07-27 21:53:46 | | 2 | 1 | 2020-07-27 22:03:58 | | 5 | 2 | 2020-07-27 22:12:21 | -----------------------------------------------

Pero cuando pongo GROUP BY member_id me devuelve dos filas (1, 2)

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Entiendo esto como un problema de lagunas e islas, en el que desea mostrar las filas cuyo member_id de miembro es diferente a la fila "anterior".

Aquí hay un enfoque usando funciones de ventana (disponibles en MySQL 8.0): lag() da el member_id en la fila anterior, que luego podemos comparar con el valor en la fila actual:

 select id, member_id, date from ( select t.*, lag(member_id) over(order by date) lag_member_id from mytable t ) t where not lag_member_id <=> member_id order by date

Demostración en DB Fiddlde :

identificación | id_miembro | fecha               
-: | --------: | :------------------
 1 | 2 | 2020-07-27 21:53:46
 2 | 1 | 2020-07-27 22:03:58
 5 | 2 | 2020-07-27 22:12:21
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!