Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 基于日期筛选表行_Sql_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

Sql 基于日期筛选表行

Sql 基于日期筛选表行,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我有以下数据集: 这指定了每个设施的任务周期,“cycleperiod”列指定了周期的开始和结束日期 任务由单独的列指定,指定startdate和completeion状态,如so date~status 状态为“0”表示未完成,“1”表示已完成 现在,我想筛选行,以便为我获得的每个设施: 当前周期 上一个周期至少有一个任务未完成 如果下一个周期在当前日期的3天内开始,则为下一个周期 到目前为止,我这样做是为了得到每个设施的当前周期,但无法找出每个设施的逻辑 select * from

我有以下数据集:

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

任务由单独的列指定,指定startdate和completeion状态,如so date~status

状态为“0”表示未完成,“1”表示已完成

现在,我想筛选行,以便为我获得的每个设施:

当前周期

上一个周期至少有一个任务未完成

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

到目前为止,我这样做是为了得到每个设施的当前周期,但无法找出每个设施的逻辑

   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); 

我们将非常感谢您在这方面提供的任何帮助。

我不能100%确定您想做什么,但如果我正确理解您的意思,这里有一个开始:

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() )
基本上,将所需的三组数据合并在一起

要获取下一个和上一个CyclePeriod,请将表连接到自身

为了只得到一个结果,beofre和after可以使用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

你能给我看一下原来的桌子吗?