从当天的某个小时开始按天分组

时间:2017-06-20 05:53:40

标签: sql sql-server sql-server-2008-r2

SQLServer 2008r2。我有一张桌子,每天每小时都有10分钟的记录。每工作一小时,它还会输入48条记录,这些记录代表了对未来48小时内可能发生的事件的预测。注意 - 在进入48小时预测之前,它会删除上次输入的预测。因此,虽然它每小时进入48小时预测,但系统中只有一个预测。表格中的相关字段如下所示:

currentScore       obsDate  
     9         2017-06-22 08:10:00
     9         2017-06-22 07:10:00
     9         2017-06-22 06:10:00
    10         2017-06-22 05:10:00
    ...       ...

如何从一天的特定时间开始查询此表和组?我希望这一天从前一天早上6点开始,并在当天早上6点结束。我只需要表中的五个记录,一天,两个记录和两个记录。因此,如果它的6月20日我想要6月18日,19日,20日,21日和22日。以下是在日历日获得正确结果的查询。

SELECT cast(obsDate AS DATE) AS theDate
    ,sum(CASE 
            WHEN currentScore < 8
                THEN 1
            ELSE 0
            END) AS currentscore_low
    ,sum(CASE 
            WHEN currentScore >= 8
                AND currentScore < 17
                THEN 1
            ELSE 0
            END) AS currentscore_medium
    ,sum(CASE 
            WHEN currentScore >= 17
                THEN 1
            ELSE 0
            END) AS currentscore_high
FROM diseaseScores
WHERE siteID = 8315
    AND obsDate >= cast(getdate() - 2 AS DATE)
GROUP BY cast(obsDate AS DATE)
ORDER BY cast(obsDate AS DATE);

返回此结果:

theDAte       low   med   high
2017-06-18     23    0     0
2017-06-19     22    0     0
2017-06-20     5     19    0
2017-06-21     0     24    0
2017-06-22     0     9     0

需要获得相同的结果,但是分组和后续计数需要从早上6点到早上6点。 e.g

第一个rec应该是2017-06-17 06:00 am到2017-06-18 06:00 am 第二个rec应该是2017-06-18 06:00 am到2017-06-19 06:00 am ....等

我该怎么做?提前致谢

更新,我做了两件事:

1 ..介绍蒂姆的想法

2 ..我还添加了一个额外的字段&#39; numOfScores&#39;显示多少小时的数据    每行代表

    select
        cast(dateadd(hour, -6, obsDate) as date) as theDate,  count(currentScore) as numOfScores, 
        sum(case when currentScore < 8 then 1 else 0 end) as currentscore_low,
        sum(case when currentScore >= 8 and currentScore < 17
                 then 1 else 0 end) as currentscore_medium,
        sum(case when currentScore >= 17 then 1 else 0 end) as currentscore_high
    from diseaseScores
    where siteID = 8315 and
          obsDate >= cast(getdate() - 2 as date)  
    group by cast(dateadd(hour, -6, obsDate) as date)
    order by cast(dateadd(hour, -6, obsDate) as date); 

我现在得到这个结果:

    2017-06-18  5   5   0   0
    2017-06-19  24  23  1   0
    2017-06-20  24  1   23  0
    2017-06-21  24  8   16  0
    2017-06-22  24  1   23  0
    2017-06-23  9   0   9   0  

这告诉我,2017-06-18只有5个小时的分数。我希望第一行能够达到24小时。从17日早上6点到18日早上6点。这让我觉得我没有得到我希望的结果

只有9小时的23号是可以的,因为这是最近的预测

更新:

我不认为它可以在一个查询中轻松完成(如果可能的话),所以我将使用五个查询,并明确说明日期和时间以获得我的结果。例如,这里是前两个:

    select 
      sum(case when currentScore < 9 then 1 else 0 end) as numOfLOWRecs, 
      sum(case when currentScore > 8 and currentScore < 17 then 1 else 0 end) as currentscore_medium, 
      sum(case when currentScore >= 17 then 1 else 0 end) as currentscore_high 
    from diseaseScores where siteID = 9999 
    and obsDate >= '2017-06-18 06:00' and obsDate < '2017-06-19 06:00'

    select 
      sum(case when currentScore < 9 then 1 else 0 end) as numOfLOWRecs, 
      sum(case when currentScore > 8 and currentScore < 17 then 1 else 0 end) as currentscore_medium, 
      sum(case when currentScore >= 17 then 1 else 0 end) as currentscore_high 
    from diseaseScores where siteID = 9999 
    and obsDate >= '2017-06-19 06:00' and obsDate < '2017-06-20 06:00'

1 个答案:

答案 0 :(得分:2)

这里可能有用的一个技巧就是将每个观察向后移动 6个小时。这会将2017-06-17 06:00:00转换为2017-06-17 00:00:00,即现在早上6点成为该实际日期的开始。

select
    cast(dateadd(hour, -6, obsDate) as date) as theDate,  
    sum(case when currentScore < 8 then 1 else 0 end) as currentscore_low,
    sum(case when currentScore >= 8 and currentScore < 17
             then 1 else 0 end) as currentscore_medium,
    sum(case when currentScore >= 17 then 1 else 0 end) as currentscore_high
from diseaseScores
where siteID = 8315 and
      obsDate >= cast(getdate() - 2 as date)  
group by cast(dateadd(hour, -6, obsDate) as date)
order by cast(dateadd(hour, -6, obsDate) as date);