Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 612945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:56:53+00:00 2026-05-13T17:56:53+00:00

I have to following table in sql server: date | status 2009-01-01 12:00:00 OK

  • 0

I have to following table in sql server:

date                 |   status  

2009-01-01 12:00:00      OK
2009-01-01 12:03:00      FAILED
2009-01-01 12:04:00      OK
2009-01-01 12:06:20      OK
2009-01-01 12:07:35      FAILED
2009-01-01 12:07:40      FAILED
2009-01-01 12:20:40      FAILED
2009-01-01 12:25:40      OK

I need the following: starting 2009-01-01 12:00:00, every 10 minute from this date i need to see the number of OK and FAILED.

something like:

INTERVAL                                  FAILED      OK
2009-01-01 12:00:00-2009-01-01 12:15:00    1           2
2009-01-01 12:15:01-2009-01-01 12:30:00    0           1

etc..

what is the best way to do this in sql?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:56:53+00:00Added an answer on May 13, 2026 at 5:56 pm

    Ok first of all ..

    You mention 10 minutes and the provide an example with 15 minutes.. Additionally you sample data should return different results than what you posted..

    solution using Pivot

    Declare @datetimestart datetime
    Declare @interval int
    Set @datetimestart = '2009-01-01 12:00:00'
    Set @interval = 15
    
    Select
      *
    From
      (
        Select 
         DateAdd( Minute,Floor(DateDiff(Minute,@datetimestart,[date])/@interval)*@interval
    ,@datetimestart), 
        DateAdd( Minute,@interval + Floor(DateDiff(Minute,@datetimestart,[date])/@interval)*@interval
    ,@datetimestart) 
    , status
        From dtest
      ) As W([from],[to], status)
    Pivot (Count(status) For status In ([ok],[failed])) p
    

    this will return

    From                       To                       Ok  Failed
    2009-01-01 12:00:00.000 2009-01-01 12:15:00.000     3   3
    2009-01-01 12:15:00.000 2009-01-01 12:30:00.000     1   0
    

    Update after comments

    This version will include time intervals that do not have values in the database..
    We will need to create a temporary table on the fly ..

    Declare @datetimestart datetime, @datetimeend datetime, @datetimecurrent datetime
    Declare @interval int
    Set @datetimestart = '2009-01-01 12:00:00'
    Set @interval = 10
    Set @datetimeend = (Select max([date]) from dtest)
    
    SET @datetimecurrent = @datetimestart
    
    declare @temp as table ([from] datetime,[to] datetime)
    while  @datetimecurrent < @datetimeend
    BEGIN
      insert into @temp select (@datetimecurrent), dateAdd( minute, @interval, @datetimecurrent)
      set @datetimecurrent = dateAdd( minute, @interval, @datetimecurrent)
    END
    
    Select
      *
    From
      (
        Select 
          [from],[to], status
        From @temp t left join dtest d on d.[date] between t.[from] and t.[to]
      ) As W([from],[to], status) 
    Pivot (Count(status) For status In ([ok],[failed])) p
    

    Using a 10 minute interval now, to show a period without values, returns..

    From                       To                       Ok  Failed
    2009-01-01 12:00:00.000 2009-01-01 12:10:00.000     3   3
    2009-01-01 12:10:00.000 2009-01-01 12:20:00.000     0   0
    2009-01-01 12:20:00.000 2009-01-01 12:30:00.000     1   0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposing we have the following records in an SQL Server table. Date 19/5/2009 12:00:00
I have table the following data structure in SQL Server: ID Date Allocation 1,
In SQL Server 2008, I have a table with following columns and data Date
I have the following table in SQL Server 2000: TABLE_NAME | COLUMN_NAME | TYPE_NAME
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id
I have the following table and data in SQL Server 2005: create table LogEntries
I have a following scenario in my SQL Server 2005 database. zipcodes table has
I have the following SQL table - Date StoreNo Sales 23/4 34 4323.00 23/4
I have a Sql Server 2005 table with following data: idHearing is the primary
Using SQL Server 2005 I have a table with the following columns id name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.