招聘程序中的SQL回填日期

时间:2018-10-27 00:12:02

标签: sql

我正在收集渠道数据,但很多数据缺少渠道上游阶段的日期戳(招聘人员跳过或未正确记录通过渠道的候选移动)

我可以按步骤及其顺序获取数据,我的目标是回填缺少带有录入日期的阶段上游的录入日期(我认为回填可以与下一阶段的日期相同,有日期)

关于如何使用SQL做到这一点的任何想法?

我创建了一些示例数据:

CREATE TABLE stages (
job_id INT,
application_id INT,
stage_name VARCHAR,
stage_order INT,
entered_on DATE,
exited_on DATE
)
;

INSERT INTO stages (job_id, application_id, stage_name, stage_order, entered_on, exited_on)
VALUES (8339915, 24342, 'Application Review', 0, '2015-06-06', '2015-06-22'),
(8339915, 24342, 'Hiring Manager Review', 1, NULL, NULL),
(8339915, 24342, 'Recruiter Phone Screen', 2, '2015-06-07', '2015-06-22'),
(8339915, 24342, 'Phone Interview', 3, NULL, NULL),
(8339915, 24342, 'Phone Interview 2', 4, '2015-06-22', '2015-07-06'),
(8339915, 24342, 'Face to Face', 5, '2015-07-06', '2015-07-24'),
(8339915, 24342, 'Face to Face 2', 6, NULL, NULL),
(8339915, 24342, 'Offer', 7, NULL, NULL),
(8339915, 24342, 'Hired', 1000, NULL, NULL)
;

SELECT *
FROM stages
ORDER BY job_id ASC, application_id ASC, stage_order

1 个答案:

答案 0 :(得分:0)

此代码将有助于满足您的要求,但是中间连接会很麻烦,如果您的阶段数很少,则可以正常工作。让我知道是否有任何担忧。

Select A.job_id,A.application_id,a.stage_name,a.stage_order,isnull(a.entered_on,ent.entered_on),isnull(a.exited_on,exi.exited_on)

from stages A

left join

(select * from(select *, row_number() over(partition by job_id,application_id,s1 order by s1,s2 desc) rn from(
select job_id,application_id,stage_name,a.stage_order as s1,b.stage_order as s2,isnull(a.entered_on,b.entered_on) as entered_on from stages A
inner join (select distinct stage_order,entered_on from stages where entered_on is not null
)b on A.stage_order>=b.stage_order
where a.entered_on is null) j)k
where K.rn=1)Ent on Ent.job_id=a.job_id and ent.application_id=a.application_id and ent.s1=a.stage_order

left join(
select * from(select *, row_number() over(partition by job_id,application_id,s1 order by s1,s2 desc) rn from(
select job_id,application_id,stage_name,a.stage_order as s1,b.stage_order as s2,isnull(a.exited_on,b.exited_on) as exited_on from stages A
inner join (select distinct stage_order,exited_on from stages where exited_on is not null
)b on A.stage_order>=b.stage_order
where a.exited_on is null) j)k
where K.rn=1)Exi on Exi.job_id=a.job_id and exi.application_id=a.application_id and exi.s1=a.stage_order
相关问题