【问题标题】:How do I return the most recent result for the week each week?如何每周返回该周的最新结果?
【发布时间】:2022-11-10 23:59:34
【问题描述】:

我有两张表要工作,一张有日期列表和它们所属的相应周,另一张有一个人参加了一组 8 次测试中的任何一次的日期(每个测试一行)。 我希望能够显示一年中每周每个测试的最近日期,无论何时进行测试。 这是我试图实现的输出示例:

|周末 |人键 |测试 1 |测试 2 | |:-----------|:---------:|:----------:|----------- :| | 2019-01-06 | 1 | 2019-01-04 | 2018-12-15 | | 2019-01-13 | 1 | 2019-01-04 | 2019-01-11 | | 2019-01-20 | 1 | 2019-01-18 | 2019-01-11 | ... |周末 |人键 |测试 1 |测试 2 | |:-----------|:---------:|:----------:|----------- :| | 2020-10-25 | 1 | 2019-01-18 | 2019-01-11 | | 2020-11-01 | 1 | 2020-10-30 | 2019-01-11 | | 2020-11-07 | 1 | 2020-10-30 | 2019-01-11 |

到目前为止,我已经能够(我认为)知道那一周是否有针对每个人、每个星期的测试。

|周末 |人键 |测试 1 |测试 2 | |:-----------|:---------:|:----------:|----------- :| | 2019-01-06 | 1 | 2019-01-04 |空 | | 2019-01-13 | 1 |空 | 2019-01-11 | | 2019-01-20 | 1 | 2019-01-18 |空 | ... |周末 |人键 |测试 1 |测试 2 | |:-----------|:---------:|:----------:|----------- :| | 2020-10-25 | 1 |空 |空 | | 2020-11-01 | 1 | 2020-10-30 |空 | | 2020-11-07 | 1 |空 |空 |

我有以下查询可以做到这一点。

with wkref  as (
Select distinct 
    d.[DateKey]
,   d.FirstDayOfWeek
from Dates_table    d with(nolock)

where   d.CalendarYear  between 2018 and YEAR(getdate())
)
, checks as (
Select 
    Dateadd(d, 6, w.FirstDayOfWeek) 'WeekEnding'
,   t.PersonKey
,   MAX(case 
        when    t.Measurement   =   'Test1' then    t.EventDateKey
        else    null 
    end) 'Test1_Date'
,   MAX(case 
        when    t.Measurement   =   'Test2' then    t.EventDateKey
        else    null 
    end) 'Test2_Date'
from    wkref w with(nolock)

left    join    Tests_table t with(nolock)
    on  t.EventDateKey  =   w.DateKey
)

我尝试使用 LAG 计算条目和语句之间的空值数,其中空值条目数是要滞后的行数。

Select 
    c.WeekEnding
,   c.PersonKey
,   c.partn
,   c.test1_Date
,   LAG(c.test1_date,partn-1,c.test1_Date) over(order by weekending) 'LatestTest1'
from (
        Select 
            c.WeekEnding
        ,   c.PersonKey
        ,   c.Test1_Date
        ,   ROW_NUMBER() over(partition by c.personkey, c.test1_date order by c.weekending asc) 'partn'
        from checks c
        ) c

虽然这没有奏效。我对 ROW_NUMBER() 的使用并没有恢复非空值之间的行数,而是恢复非空值的总数。然后它不会填充所有非空行,只是填充已经有值的行 - 所以我知道我离正确答案还很远。

我已经尝试过更简单的选项,例如自我加入和基于 testdate <= 周末的加入,但我认为这些都没有奏效。特别是这里的解决方案:Fetch the rows which have the Max value for a column for each distinct value of another column

所以我的问题是:

  1. 我想要的输出可能吗?
  2. 如果是这样,究竟什么是解决这个问题的正确方法?

    我尝试在 SQLFiddle 中设置一个实时示例,因为这已经变得相当漫长和复杂,但这也不是很顺利。这是我第一次无法通过谷歌搜索自己的答案,而且我整天都在这样做。请帮忙!!

    (针对表格格式进行了编辑,但似乎仍然无法正常工作......)

【问题讨论】:

    标签: ssms-2017


    【解决方案1】:

    我已经回答了我自己的问题。如果我再次陷入这个困境,请为后代发布此信息。 链接的帖子包含了大部分答案。将测试表左连接到日期表中,以便为每个满足条件的测试复制日期表。下面我将测试放入 CTE 中,仅通过我感兴趣的测试。

    with tests_cte as (
             t.id
          ,  t.eventdate
    from tests_table t
    where t.testtype = 'test in question'
       and 
         t.eventdate between 'desired start' and 'desired end'
    )
    
    Select
         d.FirstDayofWeek
     ,   count(distinct t1.id) 'People'
     from dates_table d
     
     left join tests_cte t
       on t.eventdate between dateadd(d,-366,d.firstdayofweek) and d.firstdayofweek
    
     where d.firstdayofweek between 'desired start date' and 'desired end date'
    
     group by d.firstdayofweek
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      相关资源
      最近更新 更多