为了保持简单,请说我有一个SQL Server 2008表,其中一列具有日期时间数据类型。我想要一个产生每分钟间隔行数的查询。例如,结果如下所示:
7/3/2011 14:00:00 | 1000
7/3/2011 14:01:00 | 1097
7/3/2011 14:02:00 | 569
第一行意味着1000行的日期时间值介于7/3/2011 13:59:00和7/3/2011 14:00:00之间。
第二行意味着1097行的日期时间值介于7/3/2011 14:00:00和7/3/2011 14:01:00之间。
第三行意味着569行的日期时间值介于7/3/2011 14:01:00和7/3/2011 14:02:00之间。
谢谢。
答案 0 :(得分:13)
此:
;WITH CTE_ExampleData as (
select stamp = '07/07/2011 14:00:01'
union select stamp = '07/07/2011 14:00:02'
union select stamp = '07/07/2011 14:00:03'
union select stamp = '07/07/2011 14:01:01'
union select stamp = '07/07/2011 14:01:02'
union select stamp = '07/07/2011 14:01:03'
union select stamp = '07/07/2011 14:01:04'
union select stamp = '07/07/2011 14:02:02'
union select stamp = '07/07/2011 14:02:03'
union select stamp = '07/07/2011 14:02:04'
union select stamp = '07/07/2011 14:02:05'
)
select
stamp = dateadd(mi,datediff(mi,0,stamp) + 1,0),
rows = count(1)
from CTE_ExampleData
group by dateadd(minute,datediff(mi,0,stamp)+1,0)
返回
stamp rows
2011-07-07 14:01:00.000 3
2011-07-07 14:02:00.000 4
2011-07-07 14:03:00.000 4
答案 1 :(得分:10)
简单组:
Select DateAdd(minute, DateDiff(minute, 0, [colName]), 0), Count(*)
From [tableName]
Group By DateAdd(minute, DateDiff(minute, 0, [colName]), 0)
如果您希望输出中某个范围内的每一分钟,无论该分钟内是否有任何数据,请使用公用表表达式(CTE):
Declare @startMinute smalldatetime Set @startMinute = '30 June 2011'
Declare @endMinute smalldatetime Set @endMinute = '1 July 2011';
With minuteList(aMinute) As
(Select @startMinute Union All
Select dateadd(minute,1, aMinute)
From minuteList
Where aMinute < @endMinute)
Select aMinute, Count(T.[colName])
From minuteList ml Left Join [tableName] T
On DateAdd(minute, DateDiff(minute, 0, T.[colName]), 0) = aMinute
Group By aMinute
Option (MaxRecursion 10000);
答案 2 :(得分:1)
如果有人想让你这样说,你也可以得到平均每分钟平均值,告诉他们你的ETL平均需要多长时间才能获得X记录。
SELECT AVG(ABC.TOTAL_MINUTE) FROM (Select DateAdd(minute, DateDiff(minute, 0,createdon),0) AS [DAY_MINUTE], Count(*) AS [TOTAL_MINUTE] From contactbase Group By DateAdd(minute, DateDiff(minute, 0, createdon), 0)) ABC here
或者,如果你想深入了解,你可以完成百分比......把它放在一个过程中......你明白了。
DECLARE @TtlToProcess AS DECIMAL --- put arbitrary number of 2.5 million in as this is aproximate, could be replaces with true source count if known. SET @TtlToProcess = 2500000 SELECT DATEDIFF(MINUTE,MIN(CB.CreatedOn),max(CB.CreatedOn)) AS [Minutes Run], ROUND(100*(CAST(COUNT(*) AS DECIMAL)/CAST(@TtlToProcess AS DECIMAL)),1) AS [%Complete], ROUND(100-(100*(CAST(COUNT(*) AS DECIMAL)/CAST(@TtlToProcess AS DECIMAL))),1) AS [% Left], COUNT(*) AS [Rows Processed], @TtlToProcess-COUNT(*) AS [Rows Left], (SELECT AVG(ABC.TOTAL_MINUTE) FROM (SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, CreatedOn), 0) AS [DAY_MINUTE], COUNT(*) AS [TOTAL_MINUTE] FROM ContactBase GROUP BY DATEADD(MINUTE, DATEDIFF(MINUTE, 0, CreatedOn), 0)) AS ABC) AS [Avg. Rows per Minute], ROUND(((@TtlToProcess-COUNT(*))/(SELECT AVG(ABC.TOTAL_MINUTE) FROM (SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, CreatedOn), 0) AS [DAY_MINUTE], COUNT(*) AS [TOTAL_MINUTE] FROM ContactBase GROUP BY DATEADD(MINUTE,DATEDIFF(MINUTE, 0, CreatedOn), 0)) ABC))/CAST(60 AS DECIMAL),2) AS [Est. Hours Left],DATEADD(hh,((@TtlToProcess-COUNT(*))/(SELECT AVG(ABC.TOTAL_MINUTE) FROM (SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, CreatedOn), 0) AS [DAY_MINUTE], COUNT(*) AS [TOTAL_MINUTE] FROM ContactBase GROUP BY DATEADD(MINUTE, DATEDIFF(MINUTE, 0, CreatedOn), 0)) AS ABC))/CAST(60 AS DECIMAL),GETDATE()) AS [Est. Complete Date_Time] FROM dbo.ContactBase AS CB
答案 3 :(得分:0)
Select DateAdd(minute, DateDiff(minute, 0, createdon), 0) AS [DAY_MINUTE], Count(*) AS [TOTAL_MINUTE] From contactbase Group By DateAdd(minute, DateDiff(minute, 0, createdon), 0) order by DateAdd(minute, DateDiff(minute, 0, createdon), 0) desc