根据日期过滤表格行

时间:2017-01-10 13:24:53

标签: sql sql-server sql-server-2008 tsql

我有以下数据集:

enter image description here

这指定了每个设施的任务周期,“周期”和“周期”。列指定循环的开始和结束日期,

并且使用指定startdate和completeion状态的各个列指定任务,如此(date~status),

' 0'在状态意味着不完整和' 1'意味着完整。

现在我想过滤行,以便我得到的每个设施:

  1. 当前周期

  2. 前一个周期,至少有一个任务不完整

  3. 如果下一个周期以3天的当前日期开始,则为下一个周期

  4. 到目前为止,我已经做到这一点,以获得每个设施的当前周期,但无法找出

    的逻辑
       select *  from ##finalTempTable1 
       where Cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, 0, 11))) as date) >=   cast(getdate() as date)
       and cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, CHARINDEX(''o'',CyclePeriod,0) + 2, 11))) as date) <= cast(getdate() as date); 
    

    在这方面的任何帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

我不是百分之百确定你想要做什么但是如果我理解你的话,这里是首发:

select 
    cycleperiod
from 
    ##finalTempTable1 
where 
    Cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, 0, 11))) as date) >=   cast(getdate() as date)
    and cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, CHARINDEX(''o'',CyclePeriod,0) + 2, 11))) as date) <= cast(getdate() as date)
    and right(cycleperiod) = '0'
UNION

select 
    max(b.cycleperiod)
from 
    ##finalTempTable1 as a 
    inner join (
        select 
            b.CyclePeriod
        from
            ##finalTempTable1 as b
        WHERE 
            and right(cycleperiod) = '0'
    )as b
        ON Cast(LTRIM(RTRIM(SUBSTRING(b.CyclePeriod, 0, 11))) as date) < Cast(LTRIM(RTRIM(SUBSTRING(b.CyclePeriod, 0, 11))) as date) 
where 
    Cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, 0, 11))) as date) >=   cast(getdate() as date)
    and cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, CHARINDEX(''o'',CyclePeriod,0) + 2, 11))) as date) <= cast(getdate() as date)

UNION

select 
    min(b.cycleperiod)  
from 
    ##finalTempTable1 as a 
    inner join (
        select 
            b.CyclePeriod
        from
            ##finalTempTable1 as b
        WHERE 
            and right(cycleperiod) = '0'
    )as b
        ON Cast(LTRIM(RTRIM(SUBSTRING(b.CyclePeriod, 0, 11))) as date) > Cast(LTRIM(RTRIM(SUBSTRING(b.CyclePeriod, 0, 11))) as date) 
where 
    Cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, 0, 11))) as date) >=   cast(getdate() as date)
    and cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, CHARINDEX(''o'',CyclePeriod,0) + 2, 11))) as date) <= cast(getdate() as date)
    and Cast(LTRIM(RTRIM(SUBSTRING(b.CyclePeriod, 0, 11))) as date) <= <= dateadd(d,3,getdate() )

基本上联合你想要的三组数据。

获取下一个和上一个循环周期将表连接到自身

只获得一个结果,并且在您可以使用max和min之后,因为它看起来没有重叠的句点,并且您的日期格式化为按字母排序有效。

如果你想要的不仅仅是循环周期列,那么再次将这个结果连接到表中,或者使用另一个临时表,或者像这样:

select 
    d.*
from 
    ##finalTempTable1  as d
    inner join(
    select 
        cycleperiod
    from 
        ##finalTempTable1 
    where 
        Cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, 0, 11))) as date) >=   cast(getdate() as date)
        and cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, CHARINDEX(''o'',CyclePeriod,0) + 2, 11))) as date) <= cast(getdate() as date)
        and right(cycleperiod) = '0'
    UNION

    select 
        max(b.cycleperiod)
    from 
        ##finalTempTable1 as a 
        inner join (
            select 
                b.CyclePeriod
            from
                ##finalTempTable1 as b
            WHERE 
                and right(cycleperiod) = '0'
        )as b
            ON Cast(LTRIM(RTRIM(SUBSTRING(b.CyclePeriod, 0, 11))) as date) < Cast(LTRIM(RTRIM(SUBSTRING(b.CyclePeriod, 0, 11))) as date) 
    where 
        Cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, 0, 11))) as date) >=   cast(getdate() as date)
        and cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, CHARINDEX(''o'',CyclePeriod,0) + 2, 11))) as date) <= cast(getdate() as date)

    UNION

    select 
        min(b.cycleperiod)  
    from 
        ##finalTempTable1 as a 
        inner join (
            select 
                b.CyclePeriod
            from
                ##finalTempTable1 as b
            WHERE 
                and right(cycleperiod) = '0'
        )as b
            ON Cast(LTRIM(RTRIM(SUBSTRING(b.CyclePeriod, 0, 11))) as date) > Cast(LTRIM(RTRIM(SUBSTRING(b.CyclePeriod, 0, 11))) as date) 
    where 
        Cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, 0, 11))) as date) >=   cast(getdate() as date)
        and cast(LTRIM(RTRIM(SUBSTRING(CyclePeriod, CHARINDEX(''o'',CyclePeriod,0) + 2, 11))) as date) <= cast(getdate() as date)
        and Cast(LTRIM(RTRIM(SUBSTRING(b.CyclePeriod, 0, 11))) as date) <= <= dateadd(d,3,getdate() )
    ) as c
        on d.cycleperiod = c.CyclePeriod