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

FIRST_VALUE & LAST_VALUE

$
0
0

Given the following sample data:

AverageRate CurrencyDate
1.0001            2001-09-03
1.0001            2001-09-04
1.0002            2001-09-05
1.0002            2001-09-06
1.0005            2001-09-07
1.0005            2001-09-08
1.0005            2001-09-09
1.0001            2001-09-10
1.0002            2001-09-11
1.0002            2001-09-12

How can I, using FIRST_VALUE and LAST_VALUE, return the desired output:

AverageRate From            To
1.0001            2001-09-03   2001-09-04
1.0002            2001-09-05   2001-09-06
1.0005            2001-09-07   2001-09-09
1.0001            2001-09-10   2001-09-10
1.0002            2001-09-11   2001-09-12

...?

I know how to do this using min/max, self joins and row_number() with partition by, but surely with 2012 there is a simpler (and more optimized) solution which only requires one select statement.  Are my expectations too much?


Viewing all articles
Browse latest Browse all 23857

Trending Articles



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