Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Needed - Determine number of rows per minute

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 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.

like image 649
Hosea146 Avatar asked Jul 08 '11 14:07

Hosea146


People also ask

How do you count the number of rows in a query?

SQL Query to Count Number of Rows:The SQL COUNT( ) function is used to return the number of rows in a table. It is used with the Select( ) statement.

How do I find the number of rows in a row in SQL?

If you'd like to number each row in a result set, SQL provides the ROW_NUMBER() function. This function is used in a SELECT clause with other columns. After the ROW_NUMBER() clause, we call the OVER() function. If you pass in any arguments to OVER , the numbering of rows will not be sorted according to any column.

How do you find the number of minutes in SQL?

To find the minimum value of a column, use the MIN() aggregate function; it takes the name of the column or expression to find the minimum value. In our example, the subquery returns the minimum value in the temperature column (subquery: SELECT MIN(temperature) FROM weather ).


3 Answers

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
like image 129
Jon Egerton Avatar answered Sep 25 '22 14:09

Jon Egerton


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);
like image 42
Charles Bretana Avatar answered Sep 24 '22 14:09

Charles Bretana


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
like image 27
John Klemetsrud Avatar answered Sep 25 '22 14:09

John Klemetsrud