Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Output data without duplicates by maximum date

I have two tables: Test1 and Test2.

Test1:

Cat Dog Date
123412 1111111 2023-01-01 12:01:01
123412 2222222 2023-01-01 13:02:01

Test2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Cat Parrot Date
123412 1412111 2023-01-01 14:01:01
123412 2242222 2023-01-01 15:02:01

The script for joining these tables looks like this:

select 

test1.cat
test2.cat

from Test1 test1

left join Test2 test2 on test2.cat=test1.cat

It is necessary to make sure that only 1 cat is displayed for the maximum date (without duplicates). Using a subquery or conditions inside a JOIN or WHERE.

I tried to do this subquery:

select 

test1.cat
test2.cat

from Test1 test1

left join Test2 test2 on test2.cat=test1.cat 

and test2.date = (select max(test2.date) from Test2) 

where test1.date = (select max(test1.date) from Test1) 

but nothing happened, it displays emptiness

I apologize if my explanations are unclear

>Solution :

Have you tried something like this?

SELECT Cat,MAX(date) as maxDate
FROM (
   SELECT test1.cat, test1.date
   FROM   Test1 test1
   UNION
   SELECT test2.cat, test2.date
   FROM   Test1 test2
)
GROUP BY Cat.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading