根据开始日期创建成本期 - PL / SQL

时间:2015-09-01 03:37:37

标签: sql oracle oracle11g lead

Oracle 11g

我想根据新费用的开始日期为每个费用设置结束日期

对于以下数据,请设置结束日期

输入:

Part number Site Cost           Start Date  End Date

000118-X    SR  8.926035461     10/1/2011   
000118-X    SR  10.7990022141   12/1/2011   
000118-X    SR  10.79900221     10/1/2012   
000118-X    SR  11.1574523796   12/1/2012   
000118-X    SR  11.15745238     10/1/2013   
000118-X    SR  14.054703823    12/1/2013   
000118-X    SR  16.4416037192   12/1/2014

所需输出:结束日期是下一条记录的开始日期-1

Part number Site Cost           Start Date  End Date

000118-X    SR  8.926035461     10/1/2011   11/30/2011
000118-X    SR  10.7990022141   12/1/2011   09/30/2012
000118-X    SR  10.79900221     10/1/2012   11/31/2012
000118-X    SR  11.1574523796   12/1/2012   09/30/2013
000118-X    SR  11.15745238     10/1/2013   11/30/2013
000118-X    SR  14.054703823    12/1/2013   11/30/2014
000118-X    SR  16.4416037192   12/1/2014   

2 个答案:

答案 0 :(得分:2)

您需要使用 LEAD()分析功能

有关架构设置,请参阅SQL Fiddle Demo

.container > .image {
    max-width: 40%;
    min-width: 40%;
    min-height: 100px;
    float:right;
    margin-top:72px;
}

答案 1 :(得分:1)

首先,您要为每个元素设置一个行号

然后加入以匹配每一行与下一行。
并将一天减去日期

SQL Fiddle Demo

with cost as (
    SELECT         
        ROW_NUMBER() OVER (ORDER BY start_date) as R, t.*
    FROM testSQL t
)
SELECT 
    c1.part_number ,
    c1.site,
    c1.cost,  
    c1.start_date,
    c2.start_date - 1
FROM cost c1
left join cost c2 on c1.r = c2.r - 1