我需要在首次填充(日期)的min(日期)之后,由web_user_id找到applicant_id空值。
例如,对于web_user_id 23,我们将在row_id =3之后找到空applicant_id,因为它首先用min(date)填充applicant_id
对于web_user_id 90,我们将在row_id = 11之后找到空applicant_id,因为它首先用min(date)填充applicant_id
表为:
| row_id | applicant_id | web_user_id | date |
| ------- | ------------- | ------------- | ---- |
| 1 | null | 23 | 2020 |
| 2 | null | 23 | 2021 |
| 3 | 77 | 23 | 2022 |
| 4 | 77 | 23 | 2023 |
| 5 | 77 | 23 | 2024 |
| 6 | null | 23 | 2025 |
| 7 | 77 | 23 | 2026 |
| 8 | null | 23 | 2027 |
| 9 | 77 | 23 | 2028 |
| 10 | null | 90 | 2020 |
| 11 | 55 | 90 | 2021 |
| 12 | 55 | 90 | 2022 |
| 13 | 55 | 90 | 2023 |
| 14 | 55 | 90 | 2024 |
| 15 | null | 90 | 2025 |
| 16 | 55 | 90 | 2026 |
| 17 | 55 | 90 | 2027 |条件是:选择min( date )、applicant_id、row_id、web_user_id,在此日期之后,我需要找到applicant_id为空的行
因此,我将拥有以下表:https://prnt.sc/264om6u
| row_id | applicant_id | web_user_id | date |
| ------- | ------------- | ------------- | ---- |
| 6 | null | 23 | 2025 |
| 8 | null | 23 | 2027 |
| 15 | null | 90 | 2025 |用于创建表的SQL
create table dbo.tabl (
row_id int,
applicant_id int,
web_user_id int,
"date" int
);insert into dbo.tabl values
(1, null, 23, 2020),
(2, null, 23, 2021),
(3, 77, 23, 2022),
(4, 77, 23, 2023),
(5, 77, 23, 2024),
(6, null, 23, 2025),
(7, 77, 23, 2026),
(8, null, 23, 2027),
(9, 77, 23, 2028),
(10, null, 90, 2020),
(11, 55, 90, 2021),
(12, 55, 90, 2022),
(13, 55, 90, 2023),
(14, 55, 90, 2024),
(15, null, 90, 2025),
(16, 55, 90, 2026),
(17, 55, 90, 2027);发布于 2022-01-05 13:36:02
这是我尝试过的,也是一个简单的查询。你也可以试试这个
Select applicant_id, row_id, web_user_id,min("date")
from tabl
where applicant_id is null
and "date" > 2024
group by applicant_id, row_id, web_user_id,"date";发布于 2022-01-05 14:37:23
**This request was written in a rush. But I think it will help you.**
Select *
from (Select *
from (Select t.*,
LAG(applicant_id) OVER(PARTITION BY WEB_USER_ID ORDER BY
t."date") LAG_,
ROW_NUMBER() OVER(PARTITION BY t.WEB_USER_ID Order BY
t."date") RN
from tabl_test t)
where not (applicant_id is null and lag_ is null))
where applicant_id is nullhttps://stackoverflow.com/questions/70593492
复制相似问题