我的数据看起来像这样。我需要在每次重新分配时找到上次和下次下载的内容。
System Reassign 2017-09-08
Walker Download 2017-09-09
System Reassign 2017-09-10
Kruger Download 2017-09-11
System Reassign 2017-12-01
我在这篇文章中取得了一些成功,但在一些行上却没有。使用下面的示例数据/查询如何获得我需要的结果?
Lag() with conditon in sql server
DECLARE @a TABLE (indiv varchar(20), status varchar(20), date date)
INSERT @a VALUES
('System','Reassign','2017/09/08'),
('Walker','Download','2017/09/09'),
('System','Reassign','2017/09/10'),
('Kruger','Download','2017/09/11'),
('System','Reassign','2017/09/12'),
('System','Reassign','2017/09/17'),
('Kruger','Download','2017/09/18'),
('System','Reassign','2017/10/01'),
('Hazle','Download','2017/11/02'),
('System','Reassign','2017/11/05'),
('Jones','Download','2017/11/06'),
('System','Reassign','2017/12/01'),
('Don','Download','2017/12/02');
; WITH a AS (SELECT *,
ROW_NUMBER() OVER(ORDER BY [date] ASC) x,
ROW_NUMBER() OVER(PARTITION BY case when [status] = 'Download' then 1 end ORDER BY [date] ASC) y
FROM @a)
, b AS (SELECT *,
--x-y as groupxy,
ROW_NUMBER() OVER(PARTITION BY x-y ORDER BY x ASC) rank_asc
FROM a)
SELECT *
,CASE WHEN [status] = 'Reassign'
THEN
LAG([indiv],rank_asc) OVER(ORDER BY x)
END AS [Previous Download Indiv]
from b
order by [date]
这些是我的预期结果......
indiv status date Prev Download Indiv Next Download Indiv
System Reassign 2017-09-08 NULL NULL
Walker Download 2017-09-09 NULL NULL
System Reassign 2017-09-10 Walker Kruger
Kruger Download 2017-09-11 NULL NULL
System Reassign 2017-09-12 Kruger Kruger
System Reassign 2017-09-17 Kruger Kruger
Kruger Download 2017-09-18 NULL NULL
System Reassign 2017-10-01 Kruger Hazle
Hazle Download 2017-11-02 NULL NULL
System Reassign 2017-11-05 Hazle Jones
Jones Download 2017-11-06 NULL NULL
System Reassign 2017-12-01 Jones Don
Don Download 2017-12-02 NULL NULL
答案 0 :(得分:0)
DECLARE @a TABLE (indiv varchar(20), status varchar(20), date date)
INSERT @a VALUES
('System','Reassign','2017/09/08'),
('Walker','Download','2017/09/09'),
('System','Reassign','2017/09/10'),
('Kruger','Download','2017/09/11'),
('System','Reassign','2017/09/12'),
('System','Reassign','2017/09/17'),
('Kruger','Download','2017/09/18'),
('System','Reassign','2017/10/01'),
('Hazle','Download','2017/11/02'),
('System','Reassign','2017/11/05'),
('Jones','Download','2017/11/06'),
('System','Reassign','2017/12/01'),
('Don','Download','2017/12/02');
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 date;
答案 1 :(得分:0)
使用相关describe 'YAML nodes files' do
Dir.glob('hieradata/nodes/*.yaml').each do |f|
data = YAML::load_file(f)
it "#{f} should contain a node YAML key that matches the filename" do
expect(data.has_key?('profile::base::node_filename')).to be true
expect(data['profile::base::node_filename']).
to eq File.basename(f).sub('.yaml','')
end
end
end
Subquery