在 MSSQL 中查找 null applicant_id 的行
Find rows where is null applicant_id in MSSQL
我需要在 applicant_id 首次填充的最小(日期)之后 web_user_id 找到 null applicant_id
例如,对于 web_user_id 23,我们会在 row_id = 3 之后找到 null applicant_id,因为它首先用 min(date)[ 填充 applicant_id
对于web_user_id 90我们会在row_id = 11之后找到null applicant_id,因为它首先用min(date)
填充applicant_id
table 是:
| 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 |
条件是:
select 分钟(日期),applicant_id,row_id,web_user_id
在此日期之后,我需要找到 applicant_id 为 null
的行
因此我想要这个 table:
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 用于创建 table
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);
这是我尝试过的,也是一个简单的查询。你也可以试试这个
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";
**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 null
我需要在 applicant_id 首次填充的最小(日期)之后 web_user_id 找到 null applicant_id
例如,对于 web_user_id 23,我们会在 row_id = 3 之后找到 null applicant_id,因为它首先用 min(date)[ 填充 applicant_id
对于web_user_id 90我们会在row_id = 11之后找到null applicant_id,因为它首先用min(date)
填充applicant_idtable 是:
| 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 |
条件是: select 分钟(日期),applicant_id,row_id,web_user_id 在此日期之后,我需要找到 applicant_id 为 null
的行因此我想要这个 table: 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 用于创建 table
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);
这是我尝试过的,也是一个简单的查询。你也可以试试这个
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";
**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 null