Home > Back-end >  How to get the earliest date for a number of groups- my query is only returning one date?
How to get the earliest date for a number of groups- my query is only returning one date?

Time:11-28

I am working with Dbeaver, and trying to write a query that returns the earliest date for a number of groups. Here is my code:

select MIN(date) as start_date 
from identity 
where data_file_group_id in (42, 43, 2134); 

My issue is that the above returns ONE date, and I can see why. However, I am trying to get a separate date for each of the data_file_group_id's. Hence 3 dates in total. Any ideas how I would do this?

The constraint is that I would like to run it in ONE query, and cannot use three queries to do this.

CodePudding user response:

You should aggregate by the data_file_group_id:

SELECT data_file_group_id, MIN(date) AS start_date
FROM identity
WHERE data_file_group_id IN (42, 43, 2134)
GROUP BY data_file_group_id;
  • Related