然后,SQL查询计数发生次数逐日,但也填充缺少的日期

时间:2016-09-27 04:32:35

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

我有一个名为diseaseScores的表,它有计算结果。每小时计算一次跑分(currentScore)。此查询的目的是按小时(obsDate)对小时分数进行分组,然后计算运行分数处于高水平的numberOfhours。高于16(currentScore > 16)。

到目前为止,我的查询是:

SELECT
    DATEADD(DAY, 0, DATEDIFF(day, 0, obsDate)) AS obsDate, 
    (CASE 
        WHEN count(id) > 12 THEN count(id) 
        ELSE 0 
     END) numOfHoursAtHigh 
FROM
    diseaseScores
WHERE 
    diseaseID = 2 
    AND siteID = 72160 
    AND numOfRotationYears = 3 
    AND currentScore > 16 
    AND month(obsDate) IN (6) 
GROUP BY
    DATEADD(DAY, 0, DATEDIFF(day, 0, obsDate)) 
ORDER BY
    DATEADD(DAY, 0, DATEDIFF(day, 0, obsDate));

查询返回一个月中13天的结果。我希望填补这些空白,以便我每月的每一天都有一份记录。差距需要numOfHoursAtHigh结果为0.

我该怎么做?这适用于SQL Server 2008 +

返回的结果集是:

        2016-06-04 00:00:00.000     0
        2016-06-05 00:00:00.000     23
        2016-06-06 00:00:00.000     23
        2016-06-07 00:00:00.000     23
        2016-06-08 00:00:00.000     3
        2016-06-09 00:00:00.000     23
        2016-06-10 00:00:00.000     0
        2016-06-17 00:00:00.000     13
        2016-06-18 00:00:00.000     23
        2016-06-19 00:00:00.000     0
        2016-06-20 00:00:00.000     14
        2016-06-21 00:00:00.000     23
        2016-06-22 00:00:00.000     16

更新:所以使用修改版本的knobcreekmans方法(在某些日子加倍)我现在有了这个填补了我的空白,并且工作了一个月。一旦我通过将月份(obsDate)改为IN(6)到月份(obsDate)IN(6,7)而要求两个月的价值,如果碰巧在第6和第7个月发生冲突,它就会跳过几天.Grrrrrr,我是如此接近!

        SELECT CAST(obsDate AS DATE) as obsDate, 
            (CASE 
                WHEN COUNT(id) > 12 THEN COUNT(id) 
                ELSE 0 
              END) numOfHoursAtHigh 
        FROM diseaseScores
        WHERE diseaseID=2 
           AND siteID=72160 
           AND numOfRotationYears=3 
           AND currentScore > 16 
           AND month(obsDate) IN (6) 
          GROUP BY CAST(obsDate AS DATE) 
        UNION
        SELECT CAST(obsDate AS DATE) AS obsDate, 
            0 AS numOfHoursAtHigh 
        FROM diseaseScores
        WHERE diseaseID=2 
           AND siteID=72160 
           AND numOfRotationYears=3 
           AND currentScore <= 17       
           AND month(obsDate) IN (6)
           and day(obsDate) NOT IN      --<-- added from here
           (
        SELECT distinct day(obsDate) 
        FROM diseaseScores
        WHERE diseaseID=2 
           AND siteID=72160 
           AND numOfRotationYears=3 
           AND currentScore > 16 
           AND month(obsDate) IN (6) 
           )                           --<-- to here to omit the duplicates
        GROUP BY CAST(obsDate AS DATE)
        ORDER BY CAST(obsDate AS DATE)  

回答有关预期结果的问题。它为列提供了一个月(或几个月)中每一天的记录。一个日期,然后是numOfHoursAtHigh的整数,例如

        2016-06-01  0
        2016-06-02  0
        2016-06-03  0
        2016-06-04  0
        2016-06-05  23
        2016-06-06  23
        2016-06-07  23
        2016-06-08  23
        2016-06-09  23
        2016-06-10  0
        2016-06-11  0
        2016-06-12  0
        2016-06-13  0
        2016-06-14  0
        2016-06-15  0
        2016-06-16  0
        2016-06-17  13
        2016-06-18  23
        2016-06-19  0
        2016-06-20  14
        2016-06-21  23
        2016-06-22  16
        2016-06-23  0
        2016-06-24  0
        2016-06-25  0
        2016-06-26  0
        2016-06-27  0
        2016-06-28  0
        2016-06-29  0
        2016-06-30  0

2 个答案:

答案 0 :(得分:0)

您可以创建与原始版本相同的另一个SELECT,修改过滤掉您想要的结果的WHERE子句部分(currentScore > 16),然后{ {1}}他们在一起。

UNION

答案 1 :(得分:0)

嗨,您可以进行以下查询,

for (i = 0; i < list.size(); ++i) {
    for (j = 0; i < list.size(); ++j) { 
        if (list[i] == list[j] && i != j) {
            // Do your stuff here.
        }
    }
}