用于获取记录日期时间字段值作为结果列的SQL语句

时间:2011-11-23 03:14:57

标签: sql sql-server-2005

我有以下两个表

activity(activity_id, title, description, group_id)
statistic(statistic_id, activity_id, date, user_id, result)

group_id和user_id来自活动目录。结果是一个整数。

鉴于我在业务逻辑方面计算的user_id和6天(周一至周六)的日期范围,以及日期范围中的某些日期可能没有针对特定日期的统计结果date(即第1天和第4天可能已输入特定活动的统计行,但第2,3,5和6天可能没有任何条目)如何获得具有以下格式的SQL结果?请记住,如果某个特定活动没有统计表中特定日期的记录,那么那天应该在SQL结果中返回0。

activity_id    group_id    day1result    day2result    day3result    day4result    day5result    day6 result
-----------    --------    ----------    ----------    ----------    ----------    ----------    -----------
sample1        Secured     0             5             1             0             2             1
sample2        Unsecured   1             0             0             4             3             2

注意:目前我正在计划在业务逻辑中处理此问题,但这需要多个查询(一个用于为该用户创建日期范围的不同活动列表,一个用于循环每个日期的每个活动的一个结果或缺少结果,使用与日期相关的结果填充数组的第二维)。对于每个日期范围,每个用户最多可能会有50多个查询,这对我来说似乎有点过分了。

我让这个工作了4天,我可以让它工作6天,但看起来有点矫枉过正。有没有办法简化这个?:

SELECT d1d2.activity_id, ISNULL(d1d2.result1,0) AS day1, ISNULL(d1d2.result2,0) AS day2, ISNULL(d3d4.result3,0) AS day3, ISNULL(d3d4.result4,0) AS day4
FROM
    (SELECT ISNULL(d1.activity_id,0) AS activity_id, ISNULL(result1,0) AS result1, ISNULL(result2,0) AS result2
     FROM
         (SELECT ISNULL(statistic_result,0) AS result1, ISNULL(activity_id,0) AS activity_id
          FROM statistic 
              WHERE user_id='jeremiah' AND statistic_date='11/22/2011'
          ) d1
          FROM JOIN
          (SELECT ISNULL(statistic_result,0) AS result2, ISNULL(activity_id,0) AS activity_id
           FROM statistic WHERE user_id='jeremiah' AND statistic_date='11/23/2011'
          ) d2
          ON d1.activity_id=d2.activity_id
     ) d1d2
     FULL JOIN
     (SELECT d3.activity_id AS activity_id, ISNULL(d3.result3,0) AS result3, ISNULL(d4.result4,0) AS result4
      FROM
          (SELECT ISNULL(statistic_result,0) AS result3, ISNULL(activity_id,0) AS activity_id
           FROM statistic WHERE user_id='jeremiah' AND statistic_date='11/24/2011'
          ) d3
          FULL JOIN
          (SELECT ISNULL(statistic_result,0) AS result4, ISNULL(activity_id,0) AS activity_id
           FROM statistic WHERE user_id='jeremiah' AND statistic_date='11/25/2011'
          ) d4
          ON d3.activity_id=d4.activity_id
     ) d3d4
     ON d1d2.activity_id=d3d4.activity_id
ORDER BY d1d2.activity_id

2 个答案:

答案 0 :(得分:3)

以下是此类事情的典型方法:

DECLARE @minDate DATETIME,
        @maxdate DATETIME,
        @userID VARCHAR(200)

SELECT  @minDate = '2011-11-15 00:00:00',
        @maxDate = '2011-11-22 23:59:59',
        @userID = 'jeremiah'

SELECT A.activity_id, A.group_id, 
        SUM(CASE WHEN DATEDIFF(day, @minDate, S.date) = 0 THEN S.Result ELSE 0 END) AS Day1Result,
        SUM(CASE WHEN DATEDIFF(day, @minDate, S.date) = 1 THEN S.Result ELSE 0 END) AS Day2Result,
        SUM(CASE WHEN DATEDIFF(day, @minDate, S.date) = 2 THEN S.Result ELSE 0 END) AS Day3Result,
        SUM(CASE WHEN DATEDIFF(day, @minDate, S.date) = 3 THEN S.Result ELSE 0 END) AS Day4Result,
        SUM(CASE WHEN DATEDIFF(day, @minDate, S.date) = 4 THEN S.Result ELSE 0 END) AS Day5Result,
        SUM(CASE WHEN DATEDIFF(day, @minDate, S.date) = 5 THEN S.Result ELSE 0 END) AS Day6Result
FROM activity A 
    LEFT OUTER JOIN statistic S 
        ON A.activity_id = S.activity_ID
            AND S.user_id = @userID
WHERE S.date between @minDate AND @maxDate 
GROUP BY A.activity_id, A.group_id 

首先,我使用group by将每个activity_id / group_id的结果集减少到一行,然后我使用CASE来分隔每个列的值。在这种情况下,我正在查看过去七天中的哪一天,但您可以使用任何逻辑来确定日期。如果行是针对特定日期,则case语句将返回S.result的值,如果不是,则返回0。 SUM会将各个值(或只有一个,如果只有一个)相加,并将其合并为一行。

您还会注意到我的日期范围是基于该范围中第一天的午夜和该范围最后一天的晚上11:59,以确保所有时间都包含在该范围内。

最后,我正在执行左连接,因此即使没有统计信息,您的列中也总是会有0。

答案 1 :(得分:0)

除了活动之外,我不完全确定你的结果是如何按组隔离的(除非组是更高级别的结构),但这是我要采取的方法:

   SELECT activity_id
          day1result = SUM(CASE DATEPART(weekday, date) WHEN 1 THEN result ELSE 0 END)
     FROM statistic
GROUP BY activity_id

我会留下剩下的时间并添加group_id给你,但你应该看到一般方法。