Skip to content
Advertisement

SQL extract only latest modified record AND correct days count IN 3 table join

This query needs to do 2 things it’s not doing at the moment:

  1. If a h.name is strictly equal to other h.name, it should only select the latest record (where the h.modify_at is the most recent). Currently it selects all “h”).

  2. The following line: (EXTRACT(epoch FROM (SELECT (NOW() - date_cible)))/86400)::INT AS retard should return the number of days before or above the date_cible in number of days. At the moment it does give an int as number of days, however it’s incorrect.

I’m basically being onboarded in a project nearly finished and have to work with entities as they are, any help is greatly appreciated.

:docId is a Symfony parameter.

Sample data from this query:

Sample correct output:

So essentially I need the correct output, with all the column requested from my own statement.

Advertisement

Answer

You need to fix your JOIN conditions. Learn to interleave the ON clauses and to put the conditions in the correct ON clause. If you want no filtering (as suggested by the LEFT JOIN), you need to use it consistently.

Then to get one row, use DISTINCT ON:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement