我有源表,如下所示。
考虑到今天的日期为2014年4月23日,我需要按Clmn1值分组, 如果我找到多个记录,那么我需要优先考虑&01; 01/01 / 0001'结束日期值,否则我需要优先考虑12/31/9999结束日期值。
Clmn1 startdate enddate Value
P1 4-Jan-14 12/31/9999 scott
P1 23-Apr-14 01/01/0001 robert
P2 4-Mar-14 12/31/9999 sachin
P2 23-Apr-14 01/01/0001 leo
P3 1-Apr-14 12/31/9999 Mark
P4 7-JUly-14 01/01/0001 james
目标表: -
Clmn1 ValueCurrent FutureValue
P1 robert na
P2 leo na
P3 Mark na
P4 na james
答案 0 :(得分:0)
看起来很简单。您基本上得到clmn1
的每个值的最低结束日期行:
with tmp as (
select
clmn1
,startdate
,enddate
,value
,rank() over (partition by clmn1 order by enddate) as rank
from
YOUR_TABLE
)
select
clmn1
,case
when startdate <= current date
then value
else null end as valuecurrent
,case
when startdate > current date
then value
else null end as futurevalue
from
tmp
where
rank = 1;