Find the earliest and latest dates of specified records from a table using SQL

I have a table (in MS SQL 2005) with a choice of dates. I want to be able to use the WHERE clause to return a group of them, and then return which date is the earliest of one column and which one is the last of another column. Here is an example table:

ID StartDate  EndDate    Person
1  01/03/2010 03/03/2010 Paul
2  12/05/2010 22/05/2010 Steve
3  04/03/2101 08/03/2010 Paul

So, I want to return all entries where Person = 'Paul'. But return something like (the earliest) StartDate = 01/03/2010 (from record ID 1) and (last) EndDate = 08/03/2010 (from record ID 3).

Thanks in advance

+3
source share
2 answers

min max, . :

select min(StartDate), max(EndDate)
from data
where Person = 'Paul'

SQL, .

+8

group by. , , where, :

select person, min(StartDate), max(EndDate)
from data
group by person

select person, min(StartDate), max(EndDate)
from data
where person ='Paul'
group by person

select person, min(StartDate), max(EndDate)
from data
group by person
having person ='Paul'
+2

Source: https://habr.com/ru/post/1738456/


All Articles