我有下表
表A
ASSIGNMENT_DT START_DATE END_DATE CLT_ID C_ID First_Service_Date FIRST_SERVICE_DATE2
2/13/15 1/26/18 1/29/18 1 1 1/30/18
3/14/16 1/26/18 1/29/18 1 1 1/30/18
1/30/18 1/26/18 1/29/18 1 1 NULL
查询:
,CASE WHEN FIRST_SERVICE_DATE IS NOT NULL THEN FIRST_SERVICE_DATE
WHEN FIRST_SERVICE_DATE IS NULL AND ASSIGNMENT_DT > START_DATE THEN NULL
WHEN FIRST_SERVICE_DATE IS NULL AND ASSIGNMENT_DT < START_DATE THEN LAST_VALUE(TRUNC(ASSIGNMENT_DT)) OVER (PARTITION BY CLT_ID
ORDER BY CLIENTID ,ASSIGNMENT_DT RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
ELSE TO_DATE('9999/01/01', 'yyyy/mm/dd')
END FIRST_SERVICE_DATE2
我需要什么: First_Service_Date2列每个Clt_id应该有一个Agg值。 因此,对于该示例,当End_Date为Not null时,请使用小于开始日期的先前assignment_dt
当end_date不为null时,表示在start_Date之后的LEAD赋值日期。
应该是
First_Service_Date
3/14/16
答案 0 :(得分:0)
我想我会将查询拆开并执行UNION。
select a.*,
FIRST_SERVICE_DATE as FIRST_SERVICE_DATE2
from TableA a
where FIRST_SERVICE_DATE IS NOT NULL
OR ASSIGNMENT_DT > START_DATE
union all
select a.*,
LAST_VALUE(TRUNC(ASSIGNMENT_DT)) OVER (PARTITION BY CLT_ID) as FIRST_SERVICE_DATE2
from TableA a
where FIRST_SERVICE_DATE IS NULL
AND ASSIGNMENT_DT < START_DATE
;
(我猜这里,因为你没有分享你的整个查询,但我认为这可以解决这个问题。)