Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.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 server 重复标志,直到其更改为其他状态_Sql Server_Business Intelligence - Fatal编程技术网

Sql server 重复标志,直到其更改为其他状态

Sql server 重复标志,直到其更改为其他状态,sql-server,business-intelligence,Sql Server,Business Intelligence,输入数据采用这种格式 当前查询 输出 这意味着,将创建案例aaa,并在2017-01-01更改状态1,并且在2017-01-04更改为状态2之前,案例aaa将保持该状态。在2017-01-07变为状态3之前,它仍然保持该状态。然后一直持续到2017年1月9日 要求:我需要在其他日期重复标记“1”,直到状态再次更改 期望输出 尝试一下: declare @data table (case_id varchar(10), new_value varchar(10), old_value varcha

输入数据采用这种格式

当前查询

输出

这意味着,将创建案例aaa,并在2017-01-01更改状态1,并且在2017-01-04更改为状态2之前,案例aaa将保持该状态。在2017-01-07变为状态3之前,它仍然保持该状态。然后一直持续到2017年1月9日

要求:我需要在其他日期重复标记“1”,直到状态再次更改

期望输出

尝试一下:

declare @data table (case_id varchar(10), new_value varchar(10), old_value varchar(10), start_date date, end_date date)

insert into @data
values ('aaa','status1',null,'2017-01-01','2017-01-03'),
       ('aaa','status2','status1','2017-01-04','2017-01-06'),
       ('aaa','status3','status2','2017-01-07','2017-01-08'),
       ('aaa','closed','status3','2017-01-09',null)

;with drange as
(
    select case_id, min(start_date) dstart, max(start_date) as dend
    from @data
    group by case_id
), dates as
    (
        select case_id, dstart, dend
        from drange
        union all
        select case_id, dateadd(day,1,dstart), dend
        from dates
        where dstart<dend
    )

select d1.case_id, 
       d2.dstart as snapshot_date, 
       case when d1.new_value = 'status1' and (d1.start_date=d2.dstart) then 1 else 0 end as is_created,
       case when d1.new_value = 'closed' then 1 else 0 end as is_closed,
       case when d1.new_value = 'status1' then 1 else 0 end as is_status1,
       case when d1.new_value = 'status2' then 1 else 0 end as is_status2,
       case when d1.new_value = 'status3' then 1 else 0 end as is_status3
from @data d1
join dates d2 on d1.case_id=d2.case_id and d2.dstart between d1.start_date and isnull(d1.end_date,d1.start_date)
order by d1.case_id, d2.dstart

这难道不是对一开始写这些行的过程的控诉吗?该过程不应该理解它需要保留标志,直到下一个状态更改吗?我没有使用这种格式放置数据。您尝试了什么?请展示您的努力,我们不是一个代码编写服务。从我的角度来看,我这里只有很少的信息,您告诉我您可以控制如何将数据插入表中,那么您为什么要将不正确的数据插入表中?为什么不先插入适当的is_状态标志?你的实际目标是什么?您是否正在尝试更正一开始错误插入的数据?您需要支撑两端:现有数据和新数据。如果插入进程继续以不正确的状态插入行,则错误出现在插入进程中。无论插入快照记录还是调用这些记录,都应确定上一个快照日期的状态,然后在当前所有状态都处于关闭状态时,在插入的行上切换该状态。
select 
     case_id
    ,snapshot_date
    ,max(cast(is_status1 as int))
    ,max(cast(is_status2 as int))
    ,max(cast(is_status3 as int))
    (
    select * from 
   (select  
    c.case_id,
    cast(pp.start_date as date) as snapshot_date, 
    case when pp.new_value  like 'Closed%' then 1 else 0 end                    as is_closed,  -- 0 as is_closed
    case when pp.new_value  = 'status1' then 1 else 0 end             as is_status1,
    case when pp.new_value  = 'Status2' then 1 else 0 end       as is_status2,
    case when pp.new_value  = 'status3' then 1 else 0 end              as is_status3
    from [case] c 
    left join CaseEvent pp  on (c.case_id=pp.case_id)
    where event_start_date is not null )t 
    where 
    (  is_status1 =1
    or is_status2= 1
    or is_status3 = 1  
    )
    )t
    group by case_id,snapshot_date;
id       snapshot_date is_created is_closed is_status1  is_status2  is_status3 

aaa      2017-01-01       1          0          1           0           0          
aaa      2017-01-02       0          0          0           0           0          
aaa      2017-01-03       0          0          0           0           0          
aaa      2017-01-04       0          0          0           1           0          
aaa      2017-01-05       0          0          0           0           0          
aaa      2017-01-06       0          0          0           0           0          
aaa      2017-01-07       0          0          0           0           1          
aaa      2017-01-08       0          0          0           0           0          
aaa      2017-01-09       0          1          0           0           0
case_id  snapshot_date is_created is_closed is_status1  is_status2  is_status3 

aaa      2017-01-01       1          0          1           0           0          
aaa      2017-01-02       0          0          1           0           0          
aaa      2017-01-03       0          0          1           0           0          
aaa      2017-01-04       0          0          0           1           0          
aaa      2017-01-05       0          0          0           1           0          
aaa      2017-01-06       0          0          0           1           0          
aaa      2017-01-07       0          0          0           0           1          
aaa      2017-01-08       0          0          0           0           1          
aaa      2017-01-09       0          1          0           0           0
declare @data table (case_id varchar(10), new_value varchar(10), old_value varchar(10), start_date date, end_date date)

insert into @data
values ('aaa','status1',null,'2017-01-01','2017-01-03'),
       ('aaa','status2','status1','2017-01-04','2017-01-06'),
       ('aaa','status3','status2','2017-01-07','2017-01-08'),
       ('aaa','closed','status3','2017-01-09',null)

;with drange as
(
    select case_id, min(start_date) dstart, max(start_date) as dend
    from @data
    group by case_id
), dates as
    (
        select case_id, dstart, dend
        from drange
        union all
        select case_id, dateadd(day,1,dstart), dend
        from dates
        where dstart<dend
    )

select d1.case_id, 
       d2.dstart as snapshot_date, 
       case when d1.new_value = 'status1' and (d1.start_date=d2.dstart) then 1 else 0 end as is_created,
       case when d1.new_value = 'closed' then 1 else 0 end as is_closed,
       case when d1.new_value = 'status1' then 1 else 0 end as is_status1,
       case when d1.new_value = 'status2' then 1 else 0 end as is_status2,
       case when d1.new_value = 'status3' then 1 else 0 end as is_status3
from @data d1
join dates d2 on d1.case_id=d2.case_id and d2.dstart between d1.start_date and isnull(d1.end_date,d1.start_date)
order by d1.case_id, d2.dstart