Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL MAX date with WHERE IN Clause

I will give 2 examples, 1st one that works, and the 2nd with what I want to do:

SELECT P.ProjName, D.update_date, SUM(E.pass), SUM(E.fail)
  FROM execution AS E INNER JOIN ( 
    daily AS D INNER JOIN project AS P ON D.project_ID = P.ID )
    ON E.daily_ID = D.ID
  WHERE D.project_id = 25 AND D.update_date = ( 
    SELECT MAX(update_date) FROM daily WHERE project_id = 25; )
  GROUP BY P.ProjName, D.update_date;

This will work for individual projects. It gives me the total of Pass and Fails for the latest day of a given project (here, 25). I would like to grab a group of projects using an WHERE IN clause. For example (but does not work):

SELECT P.ProjName, D.update_date, SUM(E.pass), SUM(E.fail)
  FROM execution AS E INNER JOIN ( 
    daily AS D INNER JOIN project AS P ON D.project_ID = P.ID )
    ON E.daily_ID = D.ID 
  WHERE D.project_id IN (25,26,28,29,30,31)
  GROUP BY P.ProjName, D.update_date
  HAVING MAX(D.update_date);

This will result in every date getting pulled for each of the projects. There are 3 tables involved, 'project' which is linked by 'daily', which is linked by 'execution'. So, for every project, there is multiple daily data, and each day has multiple execution data. I realize the HAVING MAX probably needs a comparison, but to what? Perhaps a modified JOIN will help.

Thanks, Keith

like image 610
dropper Avatar asked Oct 29 '25 08:10

dropper


1 Answers

It's a bit tricky trying to do this without knowing the database but try this:

SELECT P.ProjName, D.update_date, SUM(E.pass), SUM(E.fail)
  FROM execution AS E INNER JOIN ( 
    daily AS D INNER JOIN project AS P ON D.project_ID = P.ID )
    ON E.daily_ID = D.ID
  WHERE D.project_id IN (25,26,28,29,30,31) AND D.update_date = ( 
    SELECT MAX(update_date) FROM daily WHERE project_id = D.project_id)
  GROUP BY P.ProjName, D.update_date;

I really have no idea if this is close to anything you'd want.

like image 65
Rick Kuipers Avatar answered Oct 30 '25 23:10

Rick Kuipers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!