滞后和领导分区和计数

时间:2018-02-28 18:25:12

标签: sql-server

以下是我的示例数据:

DECLARE @a TABLE (id integer, indiv varchar(20), status varchar(20), date date)
INSERT @a VALUES
('649598','System','Reassign','2017/09/08'),
('649598','Walker','Download','2017/09/09'),
('649598','System','Reassign','2017/09/10'),
('649598','Kruger','Download','2017/09/11'),
('649598','System','Reassign','2017/09/12'),
('649598','System','Reassign','2017/09/17'),
('649598','Kruger','Download','2017/09/18'),
('649598','System','Reassign','2017/10/01'),
('649598','Hazle','Download','2017/11/02'),
('649598','System','Reassign','2017/11/05'),
('649598','Jones','Download','2017/11/06'),
('649598','System','Reassign','2017/12/01'),
('649598','Don','Download','2017/12/02'),
('506013','Topps','Download','2017/11/06'),
('506013','System','Reassign','2017/12/01'),
('506013','Goss','Download','2017/12/02');

从之前的帖子中,我没有包含id并获得了返回上一个和下一个indiv的状态行重新分配的结果。 我如何打破身份证? 最后,我想(1)计算一个ID具有重新分配状态的时间,以及(2)计算一个个体重新分配的时间。

例如,ID 649598 Kruger将它重新分配给他两次,从他那里重新分配了两次。

select * 
     , (select top 1 l.indiv from @a l where l.status <> 'Reassign' and l.date < a.date order by l.date desc) as lag
     , (select top 1 l.indiv from @a l where l.status <> 'Reassign' and l.date > a.date order by l.date asc)  as lead
from @a a 
where a.status  = 'Reassign' 
union all 
select a.*, null, null 
from @a a
where a.status <> 'Reassign' 
order by id, date




SELECT *
       ,Prev = CASE WHEN a.status <> 'Download' THEN
                  (SELECT TOP 1 indiv FROM @a WHERE [date] < a.[date] AND status <> 'Reassign' ORDER BY [date] DESC) END
       ,Next = CASE WHEN a.status <> 'Download' THEN
                  (SELECT TOP 1 indiv FROM @a WHERE [date] > a.[date] AND status <> 'Reassign' ORDER BY [date]) END
FROM @a a

1 个答案:

答案 0 :(得分:0)

这很有用。?

    DECLARE @a TABLE (id integer, indiv varchar(20), status varchar(20), date date)
    INSERT @a VALUES
    ('649598','System','Reassign','2017/09/08'),
    ('649598','Walker','Download','2017/09/09'),
    ('649598','System','Reassign','2017/09/10'),
    ('649598','Kruger','Download','2017/09/11'),
    ('649598','System','Reassign','2017/09/12'),
    ('649598','System','Reassign','2017/09/17'),
    ('649598','Kruger','Download','2017/09/18'),
    ('649598','System','Reassign','2017/10/01'),
    ('649598','Hazle','Download','2017/11/02'),
    ('649598','System','Reassign','2017/11/05'),
    ('649598','Jones','Download','2017/11/06'),
    ('649598','System','Reassign','2017/12/01'),
    ('649598','Don','Download','2017/12/02'),
    ('506013','Topps','Download','2017/11/06'),
    ('506013','System','Reassign','2017/12/01'),
    ('506013','Goss','Download','2017/12/02');

    SELECT * ,
    LEAD(indiv) OVER(partition by id order by date ) as ReassignedTo,
    LAG(indiv) OVER(partition by id order by date ) as ReassignedFROM
    into #tmpReassign
    FROM @a
    order by id desc,date


    select id,reassignedTo,Count(*) ReassignedToCount ,
    Count(*) ReassignedFROMCount
    from #tmpReassign 
    where status='Reassign'
    Group by id,reassignedTo


    drop table #tmpReassign