How to get earliest of one column and get latest of another column in MSSQL
up vote
-1
down vote
favorite
When I run this query :
select * from customertravel where PersonID in (1,2,7)
I get this :

But I want to filter results to be like this :

How should I modify that query to get first arrive date and last leave date? I know I need to do some group by but I couldn't figure how to use it.. Thnkas in advance..
sql
add a comment |
up vote
-1
down vote
favorite
When I run this query :
select * from customertravel where PersonID in (1,2,7)
I get this :

But I want to filter results to be like this :

How should I modify that query to get first arrive date and last leave date? I know I need to do some group by but I couldn't figure how to use it.. Thnkas in advance..
sql
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
When I run this query :
select * from customertravel where PersonID in (1,2,7)
I get this :

But I want to filter results to be like this :

How should I modify that query to get first arrive date and last leave date? I know I need to do some group by but I couldn't figure how to use it.. Thnkas in advance..
sql
When I run this query :
select * from customertravel where PersonID in (1,2,7)
I get this :

But I want to filter results to be like this :

How should I modify that query to get first arrive date and last leave date? I know I need to do some group by but I couldn't figure how to use it.. Thnkas in advance..
sql
sql
edited Nov 10 at 15:35
Salman A
171k65328413
171k65328413
asked Nov 6 at 14:10
jason
1,5352168120
1,5352168120
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
Supposing your ArriveDate and LeaveDate are dates and not strings or so, you could try the following:
SELECT PersonID,
Name,
City,
MIN(ArriveDate),
MAX(LeaveDate)
FROM customertravel
WHERE PersonID IN (1, 2, 7)
GROUP BY PersonID,
Name,
City;
add a comment |
up vote
2
down vote
You need to group by all columns except the dates, and find MIN and MAX. Just in case your dates are in German format you need to convert them before calculating aggregate:
SELECT
PersonID,
Name,
City,
MIN(CONVERT(DATE, ArriveDate, 104)) AS FirstArriveDate,
MAX(CONVERT(DATE, LeaveDate, 104)) AS LastLeaveDate
FROM customertravel
WHERE PersonID IN (1, 2, 7)
GROUP BY PersonID, Name, City
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Supposing your ArriveDate and LeaveDate are dates and not strings or so, you could try the following:
SELECT PersonID,
Name,
City,
MIN(ArriveDate),
MAX(LeaveDate)
FROM customertravel
WHERE PersonID IN (1, 2, 7)
GROUP BY PersonID,
Name,
City;
add a comment |
up vote
4
down vote
accepted
Supposing your ArriveDate and LeaveDate are dates and not strings or so, you could try the following:
SELECT PersonID,
Name,
City,
MIN(ArriveDate),
MAX(LeaveDate)
FROM customertravel
WHERE PersonID IN (1, 2, 7)
GROUP BY PersonID,
Name,
City;
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Supposing your ArriveDate and LeaveDate are dates and not strings or so, you could try the following:
SELECT PersonID,
Name,
City,
MIN(ArriveDate),
MAX(LeaveDate)
FROM customertravel
WHERE PersonID IN (1, 2, 7)
GROUP BY PersonID,
Name,
City;
Supposing your ArriveDate and LeaveDate are dates and not strings or so, you could try the following:
SELECT PersonID,
Name,
City,
MIN(ArriveDate),
MAX(LeaveDate)
FROM customertravel
WHERE PersonID IN (1, 2, 7)
GROUP BY PersonID,
Name,
City;
answered Nov 6 at 14:24
Robert Kock
3,6171616
3,6171616
add a comment |
add a comment |
up vote
2
down vote
You need to group by all columns except the dates, and find MIN and MAX. Just in case your dates are in German format you need to convert them before calculating aggregate:
SELECT
PersonID,
Name,
City,
MIN(CONVERT(DATE, ArriveDate, 104)) AS FirstArriveDate,
MAX(CONVERT(DATE, LeaveDate, 104)) AS LastLeaveDate
FROM customertravel
WHERE PersonID IN (1, 2, 7)
GROUP BY PersonID, Name, City
add a comment |
up vote
2
down vote
You need to group by all columns except the dates, and find MIN and MAX. Just in case your dates are in German format you need to convert them before calculating aggregate:
SELECT
PersonID,
Name,
City,
MIN(CONVERT(DATE, ArriveDate, 104)) AS FirstArriveDate,
MAX(CONVERT(DATE, LeaveDate, 104)) AS LastLeaveDate
FROM customertravel
WHERE PersonID IN (1, 2, 7)
GROUP BY PersonID, Name, City
add a comment |
up vote
2
down vote
up vote
2
down vote
You need to group by all columns except the dates, and find MIN and MAX. Just in case your dates are in German format you need to convert them before calculating aggregate:
SELECT
PersonID,
Name,
City,
MIN(CONVERT(DATE, ArriveDate, 104)) AS FirstArriveDate,
MAX(CONVERT(DATE, LeaveDate, 104)) AS LastLeaveDate
FROM customertravel
WHERE PersonID IN (1, 2, 7)
GROUP BY PersonID, Name, City
You need to group by all columns except the dates, and find MIN and MAX. Just in case your dates are in German format you need to convert them before calculating aggregate:
SELECT
PersonID,
Name,
City,
MIN(CONVERT(DATE, ArriveDate, 104)) AS FirstArriveDate,
MAX(CONVERT(DATE, LeaveDate, 104)) AS LastLeaveDate
FROM customertravel
WHERE PersonID IN (1, 2, 7)
GROUP BY PersonID, Name, City
answered Nov 6 at 14:37
Salman A
171k65328413
171k65328413
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53173606%2fhow-to-get-earliest-of-one-column-and-get-latest-of-another-column-in-mssql%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown