【问题标题】:SQL how to count how many months have passed since certain statusSQL如何计算自某个状态以来已经过去了多少个月
【发布时间】:2015-10-14 22:46:45
【问题描述】:

我有一个包含项目、周期和状态的表格,以及它们的周期值。我的目标是在 period_leaving_approved_status 列中显示自某个项目上次处于“已批准状态”(或显示此状态更改的时间段)以来已经过去了多少个月。例如,对于 201005 到 201008 期间 - 我需要显示 '201005' 值,但对于 201011-201012 期间 - '201011' 值。我设法标记了状态更改的行,但我不知道如何将条件应用于以下行。我的示例查询:

with subq as (
select 123 as project,  201002 as period,   'Approved' as status from dual
union all
select 123 as project,  201003 as period,   'Approved' as status from dual
union all
select 123 as project,  201004 as period,   'Approved' as status from dual
union all
select 123 as project,  201005 as period,   'Pending Close' as status from dual
union all
select 123 as project,  201006 as period,   'Pending Close' as status from dual
union all
select 123 as project,  201007 as period,   'Closed' as status from dual
union all
select 123 as project,  201008 as period,   'Closed' as status from dual
union all
select 123 as project,  201009 as period,   'Approved' as status from dual
union all
select 123 as project,  201010 as period,   'Approved' as status from dual
union all
select 123 as project,  201011 as period,   'Closed' as status from dual
union all
select 123 as project,  201012 as period,   'Closed' as status from dual
union all
select 123 as project,  201101 as period,   'Approved' as status from dual
union all
select 123 as project,  201102 as period,   'Approved' as status from dual
union all
select 123 as project,  201112 as period,   'Approved' as status from dual
union all
select 123 as project,  201301 as period,   'Pending Close' as status from dual
union all
select 123 as project,  201302 as period,   'Closed' as status from dual
union all
select 123 as project,  201203 as period,   'Closed' as status from dual
)

select project, 
      period, 
      status,
      case when lag(status, 1, null) OVER (ORDER BY period)='Approved' 
      AND lag(status, 1, null) OVER (ORDER BY period) NOT IN(status) then period end as period_leaving_approved_status
      from subq

【问题讨论】:

    标签: sql oracle status lag


    【解决方案1】:

    我会使用聚合来解决这个问题:

    select project,
           max(case when status = 'Approved' then period end) as ApprovedPeriod
    from subq;
    

    如果你想要时间跨度,那么像这样:

    select project,
           months_between(max(case when status = 'Approved' then period end),
                          sysdate) as monthsSinceApproved,
           max(case when status = 'Approved' then period end) as ApprovedPeriod
    from subq
    group by project;
    

    编辑:

    如果您希望在所有行上累积此信息,请使用窗口函数:

    select s.*,
           max(case when status = 'Approved' then period end) over
               (partition b project order by period) as LastApprovedPeriod
    from subq s;
    

    【讨论】:

    • 你看,这总是只需要最后一个批准的时间。但例如 period=201005 的值应该是 201004 而不是 201112
    • @user3014914 。 . .问题指出:“由于某些项目最后处于'已批准状态'”。
    • @Gordon 也许 OP 想在每行中添加与该时期相关的信息?
    猜你喜欢
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多