开发者

Query Needed - Determine number of rows per minute

开发者 https://www.devze.com 2023-03-19 02:11 出处:网络
To keep things very simple, say I have a SQL Server 2008 table with a single column that has a datetime data type. I\'d like a query that produces the number of rows for each minute interval. For exam

To keep things very simple, say I have a SQL Server 2008 table with a single column that has a datetime data type. I'd like a query that produces the number of rows for each minute interval. For example, the results would look like this:

7/3/2011 14:00:00 | 1000
7/3/2011 14:01:00 | 1097
7/3/2011 14:02:00 |  569

The first row would mean that 1000 rows have a datetime value between 7/3/2011 13:59:00 and 7/3/2011 14:00:00.

The second row would开发者_如何学Go mean that 1097 rows have a datetime value between 7/3/2011 14:00:00 and 7/3/2011 14:01:00.

The third row would mean that 569 rows have a datetime value between 7/3/2011 14:01:00 and 7/3/2011 14:02:00.

Thank you.


This:

;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)

Returns

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


Simple group By:

 Select DateAdd(minute, DateDiff(minute, 0, [colName]), 0), Count(*)
 From [tableName]
 Group By DateAdd(minute, DateDiff(minute, 0, [colName]), 0)

If you want every minute within some range in the output, regardless of whether or not there is any data in that minute, use a Common Table Expression (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);


You could also get the average per minute if someone wanted you to let's say, tell them on average how long your ETL was going to take for X records.

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

Or if you want to get more in depth, you could give percentages complete all that...put it in a proc...you get the idea.

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消