根据最近的日期窗口选择结果

问题描述

我有一个sql Server表,如下所示。我想根据上述分组,按名称和参加考试的地点分组,按日期升序作为分区。

现在提供了例如4天的可配置窗口。在下表中,如果第一次考试的日期是 02/01/2019(2月1日)-已取得其分数,并且在接下来的4天之内已重新获得的任何其他测试分数将不予考虑。如果记录也位于已经排除的项目示例行ID-4的4天窗口内,则该记录也应排除在外。

对此逻辑的任何sql语句都表示赞赏。

CREATE TABLE test(  
    [recordid] int IDENTITY(1,1) PRIMARY KEY,[name] [nvarchar](25) NULL,[testcentre] [nvarchar](25) NULL,[testdate] [smalldatetime] NOT NULL,[testscore] [int],[Preferred_Output] [int],[Result] [nvarchar](75) NULL
)

GO
INSERT INTO test
           (
    [name],[testcentre],[testdate],[testscore],[Preferred_Output],[Result]  )
    VALUES
('George','bangalore',' 02/01/2019',1,'Selected as first item -grouped by name and location'),('George',' 02/02/2019','ignore as within 4 days'),' 02/04/2019',' 02/06/2019',3,'ignore as within 4 days from already ignored item -04-02-2019'),' 02/15/2019',2,'Selected as second item -grouped by name and location'),' 02/18/2019',5,'ignore as within 4 days of prevIoUs'),'Pune',4,'Selected as third item'),6,' 02/19/2019',7,' 02/20/2019',8,'ignore as within 4 days of prevIoUs')

GO
select * from test
GO



+----------+--------+------------+------------+-----------+------------------+
| recordid |  name  | testcentre |  testdate  | testscore | Preferred_Output |
+----------+--------+------------+------------+-----------+------------------+
|        1 | George | bangalore  | 02/01/2019 |         1 |                1 |
|        2 | George | bangalore  | 02/02/2019 |         0 |                0 |
|        3 | George | bangalore  | 02/04/2019 |         1 |                0 |
|        4 | George | bangalore  | 02/06/2019 |         3 |                0 |
|        5 | George | bangalore  | 02/15/2019 |         2 |                2 |
|        6 | George | bangalore  | 02/18/2019 |         5 |                0 |
|        7 | George | Pune       | 02/15/2019 |         4 |                3 |
|        8 | George | Pune       | 02/18/2019 |         6 |                0 |
|        9 | George | Pune       | 02/19/2019 |         7 |                0 |
|       10 | George | Pune       | 02/20/2019 |         8 |                0 |
+----------+--------+------------+------------+-----------+------------------+

解决方法

我认为不需要为此进行递归查询。您想比较连续记录中的日期,所以这是一种空白问题,需要确定每个岛的起点。

窗口函数可以做到这一点:

select t.*,case when lag_testdate is null or testdate > dateadd(day,4,lag_testdate)
        then testscore
        else 0
    end new_core
from (
    select t.*,lag(testdate) over(partition by name,testcentre order by testdate) lag_testdate
    from test t
) t

Demo on DB Fiddle