两个日期之间的SQL SUM小时数& GROUP BY列名?

时间:2015-04-14 07:16:57

标签: sql sql-server datetime datediff

我需要显示一个月内的行动所经过的总小时数以及此前的前一个月:

 ___________________________________________
| Rank |  Action  |   Month    | Prev Month |
|------|----------|------------|------------|
| 1    | Action1  |      580.2 |      200.7 |
| 2    | Action8  |      412.5 |      550.2 |
| 3    | Action10 |      405.0 |       18.1 |
---------------------------------------------

我有一个格式为的SQL表:

 _____________________________________________________
| Action  |     StartTime       |       EndTime       |
|---------|---------------------|---------------------|
| Action1 | 2015-02-03 06:01:53 | 2015-02-03 06:12:05 |
| Action1 | 2015-02-03 06:22:16 | 2015-02-03 06:25:33 |
| Action2 | 2015-02-03 06:36:07 | 2015-02-03 06:36:49 |
| Action1 | 2015-02-03 06:36:46 | 2015-02-03 06:48:10 |
| ..etc   | 20..-..-..   ...etc | 20..-..-..   ...etc |
-------------------------------------------------------

查询会是什么样的?

编辑:

A ツ 的回答让我朝着正确的方向前进,但是我使用JOIN解决了问题。请参阅下面的解决方案。

4 个答案:

答案 0 :(得分:1)

我改变了一些值,因为只有一天是无聊的

INSERT INTO yourtable
    ([Action], [StartTime], [EndTime])
VALUES
    ('Action1', '2015-02-18 06:01:53', '2015-02-18 06:12:05'),
    ('Action1', '2015-02-18 06:22:16', '2015-02-18 06:25:33'),
    ('Action2', '2015-04-03 06:36:07', '2015-04-03 06:36:49'),
    ('Action1', '2015-03-19 06:36:46', '2015-03-19 06:48:10'),
    ('Action2', '2015-04-13 06:36:46', '2015-04-13 06:48:10'),
    ('Action2', '2015-04-14 06:36:46', '2015-04-14 06:48:10')
;

现在定义日期边框:

declare @dateEntry datetime = '2015-04-03';

declare @date1 date
      , @date2 date
      , @date3 date;

set @date1 = @dateEntry;               -- 2015-04-03
set @date2 = dateadd(month,-1,@date1); -- 2015-03-03 
set @date3 = dateadd(month,-1,@date2); -- 2015-02-03 

所选日期将包括在2015-04-03 00:00之前开始并在2015-02-03 00:00之后开始的所有操作

select date1 = @date1 
     , date2 = @date2
     , date3 = @date3
     , [Action] 
     , thisMonth = 
       sum(
       case when Starttime between @date2 and @date1 
            then datediff(second, starttime, endtime)/360.0
       end)
     , lastMonth =
       sum(
       case when Starttime between @date3 and @date2
            then datediff(second, starttime, endtime)/360.0
       end)
  from yourtable 
  where starttime between @date3 and @date1
  group by [Action]

http://sqlfiddle.com/#!6/35784/5

答案 1 :(得分:1)

我只是在研究了一些关于SQL Server的问题之后才重新审视这个问题。

可以从查询创建临时表,然后在另一个查询中使用 - 如果您愿意,可以使用嵌套查询
通过这种方式,结果可以JOIN像任何其他普通表一样,没有讨厌的CASE语句。这对于显示第一个查询所需的其他数据(如COUNT(DISTINCT ColumnName)

)也很有用

JOIN two SELECT statement results

SELECT TOP 10
    t1.Action, t1.[Time],
    COALESCE(t2.[Time],0) AS [Previous Period 'Time'],
    COALESCE( ( ((t1.[Time]*1.0) - (t2.[Time]*1.0)) / (t2.[Time]*1.0) ), -1 ) AS [Change]
FROM 
    (
        SELECT 
            Action, 
            SUM(DATEDIFF(SECOND, StartTime, EndTime)) AS [Time],
        FROM Actions
        WHERE StartTime BETWEEN @start AND @end
        GROUP BY Action
    ) t1
LEFT JOIN
    (
        SELECT 
            Action, 
            SUM(DATEDIFF(SECOND, StartTime, EndTime)) AS [Time]
        FROM Actions
        WHERE StartTime BETWEEN @prev AND @start
        GROUP BY Action
    ) t2
ON
    t1.Action = t2.Action
ORDER BY t1.[Time] DESC

希望这些信息对某人有用。

答案 2 :(得分:0)

您可能应该检查对预处理数据的使用分组,如下所示:

select Action, SUM(Hours)
  from (select Action, DATEDIFF('hh',StartTime, EndTime) as Hours
  FROM Actions)
group by Action

答案 3 :(得分:0)

我的假设是你开始 - 结束时间跨度太短,以至于你不必担心2个月的日期,所以你可能需要这样的事情:< / p>

select 
  dense_rank() over (order by Month desc) as Rank,
  action,
  Month,
  PrevMonth
from
(
  select
    action,
    sum(case when StartTime >= @curMonth then hours else 0 end) as Month,
    sum(case when StartTime >= @prevMonth and StartTime < @curMonth then hours else 0 end) as PrevMonth
  from
  (
    select 
      action, 
      StartTime,
      datediff(second, StartTime, EndTime) / 3600.0 as hours
    from
      yourtable
  ) T1
    group by
       action
) T2

计算持续时间为秒,然后将其除以3600以获得小时数。排名仅基于当前月份。这要求您有2个变量@curMonth和@prevMonth,它们具有限制的日期,并且未来没有数据。

用于测试的SQL小提琴:http://sqlfiddle.com/#!6/d64b7d/1