首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MSSQL中查找空applicant_id的行

在MSSQL中查找空applicant_id的行
EN

Stack Overflow用户
提问于 2022-01-05 13:14:57
回答 2查看 81关注 0票数 2

我需要在首次填充(日期)的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

https://prnt.sc/264ofhg

表为:

代码语言:javascript
复制
| 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

代码语言:javascript
复制
| row_id  | applicant_id  | web_user_id   | date |
| ------- | ------------- | ------------- | ---- |
| 6       | null          | 23            | 2025 |
| 8       | null          | 23            | 2027 |
| 15      | null          | 90            | 2025 |

用于创建表的SQL

代码语言:javascript
复制
create table dbo.tabl (
row_id int,
applicant_id int,
web_user_id int,
"date" int
);
代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-05 13:36:02

这是我尝试过的,也是一个简单的查询。你也可以试试这个

代码语言:javascript
复制
   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";
票数 2
EN

Stack Overflow用户

发布于 2022-01-05 14:37:23

代码语言:javascript
复制
**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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70593492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档