Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to select latest row when ID has one or more rows and each ID row has unique from_date and two_date? This is in a MS SQL 2008 R2 which I am attempting to write this query.

$
0
0

I have a equipment table and the equipment has a coding for each place / location or custody it has had during its life. I need to select the latest (newest) row for each piece of equipment by getting the newest from_date and to_date field combination.

The following is an example. I know how to get MAX date for one column but not with two columns (from and to DATES). Any suggestions or examples would be great. I know with other db languages there are GREATEST functions. Not able yet to find the options. 

ID

NAME

FROM_DATE

TO_DATE

21

TNK101

01/01/2013

03/15/2013

21

TNK101

03/16/2013

05/05/2013

21

TNK101

05/06/2013

12/31/2075

34

BNK201

03/15/2013

12/31/2075

104

CRL309

01/01/2013

03/15/2013

104

CRL309

03/01/2013

05/25/2013

104

CRL309

05/26/2013

11/15/2013

104

CRL309

11/16/2013

12/31/2075


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>