1

我有下表

table event(
   start_tstamp [datetime],
   stop_tstamp [datetime],
   exe_name [nvarchar](50),
   machine_name [nvarchar](30)
)

我需要从此表生成一个标量表,其中包含以下形式的start_tstampstop_tstamp之间每小时差异的一行信息:

table event_temp(
   day_occured [datetime],
   hour_occured [tinyint],
   exe_name [nvarchar](50),
   machine_name [nvarchar](30)
)

例如表事件包含两行

"2012/12/10 07:00", "2012/12/10 09:00", "notepad.exe", "testmachine"
"2012/12/11 15:00", "2012/12/11 18:00", "notepad.exe", "foomachine"

结果event_temp应该如下

"2012/12/10 00:00", 7, "notepad.exe", "testmachine"
"2012/12/10 00:00", 8, "notepad.exe", "testmachine"
"2012/12/10 00:00", 9, "notepad.exe", "testmachine"
"2012/12/11 00:00", 15, "notepad.exe", "foomachine"
"2012/12/11 00:00", 16, "notepad.exe", "foomachine"
"2012/12/11 00:00", 17, "notepad.exe", "foomachine"
"2012/12/11 00:00", 18, "notepad.exe", "foomachine"

然后,我需要将event_temp表与现有日历表连接起来,该表返回与event_temp中的日期匹配的日期列表

table calendar(
   day_occured [datetime],
   hour_occured [tinyint],
)

那将只包含:

"2012/12/10 00:00", 0
"2012/12/10 00:00", 1
"2012/12/10 00:00", 2
"2012/12/10 00:00", 3
"2012/12/10 00:00", 4
"2012/12/10 00:00", 5
...

结果基本上应该是在某个时间运行的记事本实例的列表。

table result(
   day_occured [datetime],
   hour_occured [tinyint],
   instances_running [int]
)

任何想法如何做到这一点?

4

1 回答 1

1

编辑:正如 Clockwork-Muse 指出的那样,我的最后一个答案有一些缺陷。这是一种不同的方法。

不包括零小时...

DECLARE @MinDate datetime = (SELECT TOP 1 start_tstamp FROM [event] ORDER BY start_tstamp ASC)
DECLARE @MaxDate datetime = (SELECT TOP 1 stop_tstamp FROM [event] ORDER BY stop_tstamp DESC)

WITH Dates AS (
    SELECT @MinDate AS dt
    UNION ALL
    SELECT DATEADD(hh, 1, dt)
      FROM Dates s
     WHERE DATEADD(hh, 1, dt) <= @MaxDate)

SELECT 
  CONVERT(date,d.dt,101) AS [day_occured],
  DATEPART(hh, d.dt) AS [hour_occured],
  COUNT(*) AS [instances_running]
FROM @Event e
JOIN Dates d ON e.start_tstamp <= d.dt
        AND e.stop_tstamp  > = d.dt 
GROUP BY d.dt
OPTION (MAXRECURSION 0)

包括零小时...

SET @MinDate = CONVERT(date,@MinDate,101)
SET @MaxDate = CONVERT(date, DATEADD(dd,1,@MaxDate))
SET @MaxDate = DATEADD(MILLISECOND, -2,@MaxDate)

SELECT 
  CONVERT(date,d.dt,101) AS [day_occured],
  DATEPART(hh, d.dt) AS [hour_occured],
  SUM(CASE ISNULL(exe_name,'-1') WHEN '-1' THEN 0 ELSE 1 END ) AS [instances_running]
FROM [event] e
FULL JOIN Dates d ON e.start_tstamp <= d.dt
         AND e.stop_tstamp  >= d.dt 
GROUP BY d.dt
OPTION (MAXRECURSION 0)
于 2012-12-12T20:18:06.913 回答