连续行之间的日期差异

时间:2016-10-19 05:36:58

标签: sql oracle

我有一个包含以下结构的表

RetailerCode   Orderdate
1001           01-JUL-16
1001           04-JUL-16
1001           06-JUL-16

我想要这个输出

1001      10/9/2011 - 12/9/2011     2 days
1001      12/9/2011 - 20/9/2011     8 days
1001      20/9/2011                 NA

1 个答案:

答案 0 :(得分:1)

看起来像简单的分析功能

select RetailerCode, tO_char(Orderdate,'DD/mm/YYYY')||nvl2(next_Orderdate,' - '||tO_char(next_Orderdate,'DD/mm/YYYY'),'') Orderdate_diff, nvl(to_char(next_Orderdate - Orderdate),'NA') count_days from 
(select RetailerCode, Orderdate,  
                      lead(Orderdate) over (partition by RetailerCode order by Orderdate) next_Orderdate from YOUR_TABLE
 )