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

185
Views
Seleccione la fila donde la columna es diferente de la fila anterior

Estoy tratando de escribir una consulta para rastrear los cambios de una columna booleana en una tabla. La mesa se ve así

 ClientHistory ------------------------------------------------------------- | id | client_id | client_name | is_active | Timestamp | ------------------------------------------------------------- | 1 | 1 |Example Client| True | 06/15/2020| ------------------------------------------------------------- | 2 | 1 |Client Change | True | 06/16/2020| ------------------------------------------------------------- | 3 | 1 |Client Change | False | 06/17/2020 |

Entonces, lo que querría es la fila 3 donde is_active cambió a falso. Luego, después de eso, querría la siguiente fila donde cambió a verdadero nuevamente.

Esto es lo que probé:

 SELECT a.* FROM client_history AS a WHERE a.is_active <> ( SELECT b.is_active FROM client_history AS b WHERE a.client_id = b.client_id AND a.timestamp > b.timestamp ORDER BY b.timestamp DESC LIMIT 1 )

Entonces, la subconsulta está tratando de obtener la fila anterior del mismo client_id al obtener la marca de tiempo más reciente antes. Luego, en la consulta, verifique si is_active no es igual a is_active de la fila anterior. Pero esto no está funcionando como estaba previsto. Espero que, al activar activo/inactivo, debería estar alternando en esta consulta, pero no es así. ¿Alguien tiene algún consejo?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

¡Usa las funciones de la ventana!

 select ch.* from (select ch.*, lag(is_active) over (partition by client_id order by timestamp) as prev_is_active from client_history ch ) ch where is_active <> prev_is_active;
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!