CurlieQ1034
CurlieQ1034

Reputation: 13

SQL Partition Dates

I have data such as this:

ID|From Date  |To Date
1 |01/01/2014 |04/01/2015
1 |04/01/2015 |01/01/2016
1 |01/01/2016 |12/31/2016

But I would like this as the end result:

ID|From Date  |To Date
1 |01/01/2014 |12/31/2016

I tried partitioning, but I'm not quite familiar with how it works. Any suggestions would be greatly appreciated.

Thanks!

Upvotes: 0

Views: 53

Answers (1)

mschmidt
mschmidt

Reputation: 60

I'm not sure what should happen for each row, but if all you need is the oldest start date and newest end date for each ID...

SELECT ID
   , MIN([From Date]) AS [From Date]
   , MAX([To Date]) AS [To Date]
   FROM Table
GROUP BY ID

This will get you

1 | 01/01/2014 | 12/31/2016

Upvotes: 2

Related Questions