简体   繁体   English

如何找到按日期排序的两个连续行,包含特定值?

[英]How to find two consecutive rows sorted by date, containing a specific value?

I have a table with the following structure and data in it:我有一个表,其中包含以下结构和数据:

| ID    | Date          | Result    |
|----   |------------   |--------   |
| 1     | 30/04/2020    | +         |
| 1     | 01/05/2020    | -         |
| 1     | 05/05/2020    | -         |
| 2     | 03/05/2020    | -         |
| 2     | 04/05/2020    | +         |
| 2     | 05/05/2020    | -         |
| 2     | 06/05/2020    | -         |
| 3     | 01/05/2020    | -         |
| 3     | 02/05/2020    | -         |
| 3     | 03/05/2020    | -         |
| 3     | 04/05/2020    | -         |

I'm trying to write an SQL query (I'm using SQL Server) which returns the date of the first two consecutive negative results for a given ID.我正在尝试编写一个 SQL 查询(我正在使用 SQL 服务器),它返回给定 ID 的前两个连续否定结果的日期。 For example, for ID no.例如,对于 ID 号。 1, the first two consecutive negative results are on 01/05 and 05/05. 1,前两个连续的负面结果是在 01/05 和 05/05。 The first two consecutive results for ID No. 2 are on 05/05 and 06/05. ID 号 2 的前两个连续结果分别在 05/05 和 06/05。 The first two consecutive negative results for ID No. 3 are on on 01/05 and 02/05. ID 号 3 的前两个连续阴性结果分别在 01/05 和 02/05 出现。

So the query should produce the following result:所以查询应该产生以下结果:

| ID    | FirstNegativeDate     |
|----   |-------------------    |
| 1     | 01/05                 |
| 2     | 05/05                 |
| 3     | 01/05                 |

Please note that the dates aren't necessarily one day apart.请注意,日期不一定相隔一天。 Sometimes, two consecutive negative tests may be several days apart.有时,两个连续的阴性测试可能相隔几天。 But they should still be considered as "consecutive negative tests".但它们仍应被视为“连续阴性测试”。 In other words, two negative tests are not 'consecutive' only if there is a positive test result in between them.换句话说,两个阴性测试只有在它们之间有阳性测试结果时才“连续”。

How can this be done in SQL?如何在 SQL 中做到这一点? I've done some reading and it looks like maybe the PARTITION BY statement is required but I'm not sure how it works.我已经阅读了一些内容,看起来可能需要 PARTITION BY 语句,但我不确定它是如何工作的。

This is a gaps-and-island problem, where you want the start of the first island of '-' s that contains at least two rows.这是一个间隙和孤岛问题,您希望从'-'的第一个孤岛开始,它至少包含两行。

I would recommend lead() and aggregation:我会推荐lead()和聚合:

select id, min(date) first_negative_date
from (
    select t.*, lead(result) over(partition by id order by date) lead_result
    from mytable t
) t
where result = '-' and lead_result = '-'
group by id

Use LEAD or LAG functions over ID partition ordered by your Date column.在按日期列排序的 ID 分区上使用 LEAD 或 LAG 函数。 Then simple check where LEAD/LAG column is equal to Result.然后简单检查 LEAD/LAG 列等于 Result 的位置。 You'll need also to filter the top ones.您还需要过滤顶部的。

The image attached just shows what LEAD/LAG would return所附图像仅显示了 LEAD/LAG 将返回的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM