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

169
Views
Manejo de fechas en MySQL. Número total de proyectos diferentes completados

Tengo una tabla en una versión anterior de MySQL 5.x como esta:

 +---------+------------+------------+ | Task_ID | Start_Date | End_Date | +---------+------------+------------+ | 1 | 2015-10-15 | 2015-10-16 | | 2 | 2015-10-17 | 2015-10-18 | | 3 | 2015-10-19 | 2015-10-20 | | 4 | 2015-10-21 | 2015-10-22 | | 5 | 2015-11-01 | 2015-11-02 | | 6 | 2015-11-17 | 2015-11-18 | | 7 | 2015-10-11 | 2015-10-12 | | 8 | 2015-10-12 | 2015-10-13 | | 9 | 2015-11-11 | 2015-11-12 | | 10 | 2015-11-12 | 2015-11-13 | | 11 | 2015-10-01 | 2015-10-02 | | 12 | 2015-10-02 | 2015-10-03 | | 13 | 2015-10-03 | 2015-10-04 | | 14 | 2015-10-04 | 2015-10-05 | | 15 | 2015-11-04 | 2015-11-05 | | 16 | 2015-11-05 | 2015-11-06 | | 17 | 2015-11-06 | 2015-11-07 | | 18 | 2015-11-07 | 2015-11-08 | | 19 | 2015-10-25 | 2015-10-26 | | 20 | 2015-10-26 | 2015-10-27 | | 21 | 2015-10-27 | 2015-10-28 | | 22 | 2015-10-28 | 2015-10-29 | | 23 | 2015-10-29 | 2015-10-30 | | 24 | 2015-10-30 | 2015-10-31 | +---------+------------+------------+

Si End_Date de las tareas son consecutivas, entonces forman parte del mismo proyecto. Estoy interesado en encontrar el número total de diferentes proyectos completados.

Si hay más de un proyecto que tiene el mismo número de días de finalización, ordene por la Start_Date de inicio del proyecto.

Para estos pocos registros de muestra, el resultado esperado sería:

 2015-10-15 2015-10-16 2015-10-17 2015-10-18 2015-10-19 2015-10-20 2015-10-21 2015-10-22 2015-11-01 2015-11-02 2015-11-17 2015-11-18 2015-10-11 2015-10-13 2015-11-11 2015-11-13 2015-10-01 2015-10-05 2015-11-04 2015-11-08 2015-10-25 2015-10-31

Estoy un poco atascado con esto. Realmente apreciaria cualquier ayuda. Gracias.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Esto responde, y responde correctamente, la versión original de esta pregunta.

Hmmmm. . . Creo que puedes usar variables. La forma más sencilla es generar un número secuencial y luego restar este valor para obtener una constante para las filas adyacentes a partir de la fecha:

 select min(start_date), max(end_date) from (select t.*, (@rn := @rn + 1) as rn from (select t.* from tasks t order by end_date) t cross join (select @rn := 0) params ) t group by (end_date - interval rn day);

Aquí hay un db<>fiddle.

over 4 years ago · Santiago Trujillo Report

0

La siguiente consulta debería funcionar:

 select tmp.projectid, date_sub(max(tmp.ed2), interval max(tmp.projectdays) day) start_date, max(tmp.ed2) end_date, max(tmp.projectdays) No_Of_ProjectDays from ( select t1.task_id tid1, t1.start_date sd1, t1.end_date ed1, t2.task_id tid2, t2.start_date sd2, t2.end_date ed2, case when datediff(t2.start_date, ifnull(t1.start_date,'1000-01-01')) != 1 then (@pid := @pid + 1) else (@pid := @pid) end as ProjectId, case when datediff(t2.start_date, ifnull(t1.start_date,'1000-01-01')) != 1 then (@pdays := 1) else (@pdays := @pdays + 1) end as ProjectDays from tasks t1 right join tasks t2 on t2.task_id = t1.task_id + 1 cross join (select @pid :=1, @pdays := 1) vars ) tmp group by tmp.projectid order by max(tmp.projectdays), start_date

Encuentre la demostración aquí .

EDITAR: he realizado cambios en la consulta y el enlace de acuerdo con la nueva muestra de datos. Por favor échale un vistazo.

over 4 years ago · Santiago Trujillo Report

0

Es un pequeño problema complicado, pero la consulta a continuación funciona bien.

Construye dos tablas, una con Start_Date y otra con End_Date que NOT IN End_Date y Start_Date respectivamente de la tabla de Projects , y consulta estas tablas Start_Date WHERE Start_Date < End_Date agrupando por Start_Date usando aggregate function MIN con End_Date para obtener un Proyecto completo.

DATEDIFF(MIN(End_Date), Start_Date) para calcular project_duration y poder ordenar por project_duration .

 SELECT Start_Date, MIN(End_Date) AS End_Date, DATEDIFF(MIN(End_Date), Start_Date) AS project_duration FROM (SELECT Start_Date FROM Projects WHERE Start_Date NOT IN (SELECT End_Date FROM Projects)) a, (SELECT End_Date FROM Projects WHERE End_Date NOT IN (SELECT Start_Date FROM Projects)) b WHERE Start_Date < End_Date GROUP BY Start_Date ORDER BY project_duration ASC, Start_Date ASC;

Rendimiento esperado

 +------------+------------+---------------+ | Start_Date | End_Date | project_duration | +------------+------------+---------------+ | 2015-10-15 | 2015-10-16 | 1 | | 2015-10-17 | 2015-10-18 | 1 | | 2015-10-19 | 2015-10-20 | 1 | | 2015-10-21 | 2015-10-22 | 1 | | 2015-11-01 | 2015-11-02 | 1 | | 2015-11-17 | 2015-11-18 | 1 | | 2015-10-11 | 2015-10-13 | 2 | | 2015-11-11 | 2015-11-13 | 2 | | 2015-10-01 | 2015-10-05 | 4 | | 2015-11-04 | 2015-11-08 | 4 | | 2015-10-25 | 2015-10-31 | 6 | +------------+------------+---------------+
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!