以下是示例数据,预期输出在预期结果列中
请告诉我如何实现它
我试过了,但没有得到预期的结果
ID | 类型 | 标题 | 日期 | PersonID | 预期结果 |
---|---|---|---|---|---|
3656 | 所有者 | 身份验证 | 8/17/2020 | 5538 | N |
3688 | 所有者 | 状态 | 8/17/2020 | 5538 | N |
4536 | 打电话 | 信息 | 8/17/2020 | 5538 | N |
4366 | 所有者 | 身份验证 | 8/17/2020 | 6838 | 是 |
4379 | 所有者 | 状态 | 8/17/2020 | 6838 | 是 |
4554 | 所有者 | S 状态 | 8/17/2020 | 6838 | 是 |
4494 | 所有者 | 身份验证 | 9/27/2020 | 3666 | N |
4503 | 所有者 | 状态 | 9/27/2020 | 3666 | N |
4731 | 回答 | 打电话 | 9/27/2020 | 3666 | N |
5030 | 回答 | 信息 | 9/27/2020 | 3666 | N |
8642 | 所有者 | 身份验证 | 9/27/2020 | 3666 | 是 |
2635 | 所有者 | 身份验证 | 9/27/2020 | 5981 | 是 |
2640 | 所有者 | 状态 | 9/27/2020 | 5981 | 是 |
1234 | TEM | 私人 | 10/22/2020 | 9989 | 空 |
1235 | 差距 | 滞后 | 10/22/2020 | 9989 | 空 |
1236 | 损失 | 损失 | 10/22/2020 | 9989 | 空 |
1237 | 打电话 | 状态 | 10/22/2020 | 9989 | 空 |
答案 0 :(得分:0)
您可以使用窗口函数,假设排序基于 id
-- 特别是 first_value()
。
您对返回值的描述不完整。但想法是:
select t.*,
(case when first_type = 'OWNER' and first_type = last_type
then 'Y'
when first_type = 'OWNER' and first_title = 'AUTHENTICATION'
then ? -- not specified in question
when first_type = 'OWNER'
then 'N'
when sum(case when type = 'OWNER' then 1 else 0 end) over (partition by personid, date) = 0
then NULL
else ? -- other cases not specified
end) as expected_results
from (select t.*,
first_value(type) over (partition by personid, date order by id) as first_type,
first_value(title) over (partition by personid, date order by id) as first_title,
first_value(type) over (partition by personid, date order by id desc) as last_type
from t
) t