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

294
Views
Obtenga la fecha MIN variable según el marcador en la fila

Estoy usando MySQL con phpMyAdmin , a continuación se muestra mi tabla de datos:

 +--------+---------+-----------------+ | userID | context | time | +--------+---------+-----------------+ | 111 | | 7/1/2021 | | 111 | | 7/16/2019 | | 111 | Reset | 7/15/2019 | | 222 | | 7/9/2020 | | 222 | Reset | 7/8/2020 | | 333 | Reset | 5/11/2020 | | 333 | | 5/10/2020 | | 444 | | 9/8/2020 | +--------+---------+-----------------+

Estoy buscando una consulta SELECT que me proporcione el tiempo MIN mayor o igual a la fecha en la que se registra un Restablecimiento en la columna de context . Si no existe ningún marcador Restablecer en la columna de context , me gustaría que se incluyera el resultado.

Entonces, para la tabla anterior, espero el resultado:

 +--------+-----------------+ | userID | time | +--------+-----------------+ | 111 | 7/15/2019 | | 222 | 7/8/2020 | | 333 | 5/11/2020 | | 444 | 9/8/2020 | +--------+-----------------+

Lo intenté :

 SELECT * FROM foo as a WHERE ((SELECT MIN(a.time) FROM foo) >= (SELECT Max(a.time) FROM foo where context = 'Reset')) group by userID

no produce mi salida esperada, pero devuelve:

 +--------+-----------+ | userID | time | +--------+-----------+ | 111 | 7/1/2021 | <--- wrong | 222 | 7/8/2020 | | 333 | 5/11/2020 | +--------+-----------+
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Usar funciones de ventana:

 select t.*, coalesce(next_time, time) as imputed_time from (select t.*, sum(context = 'reset') over (partition by user_id) as cnt_reset, min(case when context is null then time end) over (partition by userid order by time rows between current row and unbounded following) as next_time from t ) t where cnt_reset = 0 or context = 'reset';
over 4 years ago · Santiago Trujillo Report

0

Según el resultado esperado, puede intentar la siguiente consulta:

 SELECT userID, MAX(time) FROM YOUR_TABLE WHERE context = 'RESET' GROUP BY userID UNION ALL SELECT userID, MAX(time) FROM YOUR_TABLE WHERE context IS NULL GROUP BY userID ORDER BY userID
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!