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

Sql server 根据最近的日期窗口选择结果,sql-server,datetime,window-functions,recursive-query,gaps-and-islands,Sql Server,Datetime,Window Functions,Recursive Query,Gaps And Islands,我有一个SQL Server表,如下所示。我想按姓名和考试地点分组,按日期升序,作为基于上述分组的分区 现在提供了一个可配置的窗口,例如:4天。如果第一次测试日期为 2019年1月2日(2月1日)-对其进行评分,在接下来的4天窗口内重新获得的任何其他考试分数将不予考虑。如果记录也在已排除项目示例行id-4的4天窗口内,则也应排除该记录 非常感谢用于此逻辑的任何SQL语句 CREATE TABLE test( [recordid] int IDENTITY(1,1) PRIMARY K

我有一个SQL Server表,如下所示。我想按姓名和考试地点分组,按日期升序,作为基于上述分组的分区

现在提供了一个可配置的窗口,例如:4天。如果第一次测试日期为 2019年1月2日(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,1,'Selected as first item -grouped by name and location'),
('George','bangalore',' 02/02/2019',0,0,'ignore as within 4 days'),
('George','bangalore',' 02/04/2019',1,0,'ignore as within 4 days'),
('George','bangalore',' 02/06/2019',3,0,'ignore as within 4 days from already ignored item -04-02-2019'),
('George','bangalore',' 02/15/2019',2,2,'Selected as second item -grouped by name and location'),
('George','bangalore',' 02/18/2019',5,0,'ignore as within 4 days of previous'),
('George','Pune',' 02/15/2019',4,3,'Selected as third item'),
('George','Pune',' 02/18/2019',6,0,'ignore as within 4 days of previous'),
('George','Pune',' 02/19/2019',7,0,'ignore as within 4 days of previous'),
('George','Pune',' 02/20/2019',8,0,'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

你试过什么?向我们展示您的尝试?您好GMB,想添加一个重新测试条件以及lag am使用秩函数,这是正确的方法吗?