sql - Redshift SQL - 跳过的序列
问题描述
我正在处理申请人管道数据,需要计算进入管道/漏斗每个阶段的申请人的数量。如果申请人跳过一个阶段,我无论如何都需要将他们计入该阶段。以下是该数据如何查找一位申请人的示例:
Stage name | Entered on
Application Review | 9/7/2018
Recruiter Screen | 9/10/2018
Phone Interview | blank
Interview | 9/17/2018
Interview 2 | 9/20/2018
Offer | blank
这是表格的样子:
CREATE TABLE application_stages (
application_id bigint,
stage_id bigint,
entered_on timestamp without time zone,
exited_on timestamp without time zone,
stage_name character varying
);
在这个例子中,我想通过面试 2(包括跳过/空白电话面试阶段)计算申请审查,但不包括报价。我将如何用 SQL 编写上述内容?(数据存储在 Amazon Redshift 中。使用 SQL 工作台进行查询。)
另外,如果我可以在我的问题中添加任何其他内容以使问题/解决方案更清晰,请告诉我。
解决方案
这是我最终得到的 SQL。感谢您的想法,@AlexYes!
select stage_name,
application_stages.application_id, entered_on,
case when entered_on is NULL then lead(entered_on,1)
ignore nulls
over
(PARTITION BY application_stages.application_id order by case stage_name
when 'Application Review' then 1
when 'Recruiter Screen' then 2
when 'Phone Interview' then 3
when 'Interview' then 4
when 'Interview 2' then 5
when 'Offer' then 6
when 'Hired' then 7 end) else entered_on end as for_count, exited_on
from application_stages
我意识到上面的 SQL 没有给我计数,但我正在 Tableau 中进行计数。很高兴有上述格式,以防我需要对新的“for_count”字段进行其他计算。
推荐阅读
- azure - Azure 时间触发器功能不会按小时触发
- c - 如何修复 C 程序中的编译错误“WinMain”?
- unity3d - 按钮点击动画
- javascript - 如何在 $.each ajax 中设置 href 值
- firebase - 如何使用 addListenerForsingleValueEvent 在 Firebase 身份验证中存储用户电子邮件地址
- git - gitattributes -merge 用于一个特定目录(现有仓库)
- angular - 查询字符串返回url
- php - 如何在 TWIG 中访问以特殊字符开头的变量
- python - 我如何使用python重置游戏中的随机选择选择
- php - 使用 php 显示 csv 文件时出现“警告:foreach 提供的无效”