Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 插入频率_Sql Server 2005 - Fatal编程技术网

Sql server 2005 插入频率

Sql server 2005 插入频率,sql-server-2005,Sql Server 2005,我有一个表,它除了保存所有其他数据信息外,还保存了插入此数据时的数据。现在我想知道每小时或每15分钟的插入次数。下面是我想要实现的代码。注意,在我的示例中,我还想显示从10:00到11:00没有插入的时间间隔 if object_id('tempdb..#temp') is not null drop table #temp create table #temp (Id int identity(1,1), Date datetime) insert into #temp (Date) se

我有一个表,它除了保存所有其他数据信息外,还保存了插入此数据时的数据。现在我想知道每小时或每15分钟的插入次数。下面是我想要实现的代码。注意,在我的示例中,我还想显示从10:00到11:00没有插入的时间间隔

if object_id('tempdb..#temp') is not null drop table #temp 
create table #temp (Id int identity(1,1), Date datetime)

insert into #temp (Date)
select '2009-10-05 08:01:00' union all
select '2009-10-05 08:22:00' union all
select '2009-10-05 08:23:00' union all
select '2009-10-05 08:24:00' union all
select '2009-10-05 09:30:00' union all
select '2009-10-05 11:01:00' union all
select '2009-10-05 11:05:00' union all
select '2009-10-05 11:52:00' union all
select '2009-10-05 12:01:00' union all
select '2009-10-05 12:05:00' union all
select '2009-10-05 12:07:00' union all
select '2009-10-05 12:09:00' union all
select '2009-10-05 12:20:00' union all
select '2009-10-05 12:30:00' union all
select '2009-10-05 12:40:00' union all
select '2009-10-05 12:50:00' union all
select '2009-10-05 12:55:00' union all
select '2009-10-05 13:30:00' union all
select '2009-10-05 13:35:00' union all
select '2009-10-05 13:40:00' union all
select '2009-10-05 14:01:00'

-- I want to get this data
select '08:00 - 09:00', 4 union all
select '09:00 - 10:00', 1 union all
select '10:00 - 11:00', 0 union all
select '11:00 - 12:00', 3 union all
select '12:00 - 13:00', 9 union all
select '13:00 - 14:00', 3 union all
select '14:00 - 15:00', 1

这应该可以做到。递归CTE用于构建动态小时列表:

;WITH rangeCTE
AS
(
        SELECT  MIN(DATE) minDate
                ,MAX(DATE) maxDate
        FROM #temp
)
,timeListCTE
AS
(
        SELECT CAST(CONVERT(CHAR(14),minDate,121) + '00:00' AS DATETIME) AS timeStart
               ,DATEADD(hh,1,CAST(CONVERT(CHAR(14),minDate,121) + '00:00' AS DATETIME)) AS timeEnd
               ,1 AS timeID
        FROM rangeCTE

        UNION ALL

        SELECT DATEADD(hh,1,timeStart)
               ,DATEADD(hh,2,timeStart)
               ,timeID + 1
        FROM timeListCTE
        WHERE timeStart <= (SELECT maxDate FROM rangeCTE)
)
SELECT tl.timeStart
       ,tl.timeEnd
       ,SUM(CASE WHEN t.Date IS NOT NULL 
                  THEN 1
                  ELSE 0
            END
           )
FROM      timeListCTE        AS tl
LEFT JOIN #temp              AS t
ON        t.DATE >= tl.timeStart
AND       t.DATE <  tl.timeEnd
GROUP BY tl.timeStart
        ,tl.timeEnd