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

  • Home
  • SEARCH
  • 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 6164853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:03:52+00:00 2026-05-23T22:03:52+00:00

I am trying to construct an sql query using a while loop that increments

  • 0

I am trying to construct an sql query using a while loop that increments a datetime by one minute each iteration and then generates a select statement based on the time:

declare @dt datetime
set @dt = '2011-7-21'
while @dt < '2011-7-22'
begin
    select Count(*) From Actions Where Timestamp = @dt
    set @dt = DATEADD(mi, 1, @dt)
end

The query works as intended except that every iteration of the while loop seems to produce a new query entirely, rather than simply a new row. Is there a way to construct this so that its one single query and each row is generated by the incrementation of the loop?
I believe this occurs because the select statement is inside the loop, but I’m not sure how to construct it a different way that works.

EDIT – Here is what I came up with using a temporary table, but it is slow. Maybe there is a faster way? If not thats fine, atleast this works:

create table #temp 
(
  [DT] datetime not null,
  [Total] int not null
)

declare @dt datetime
declare @result int
set @dt = '2011-7-21'
while @dt < '2011-7-22'
begin
    set @result = Count(*) From Actions Where Timestamp = @dt
    insert #temp ([DT],[Total]) values (@dt, @result)
    set @dt = DATEADD(mi, 1, @dt)
end

select * from #temp;

drop table #temp; 
  • 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-23T22:03:53+00:00Added an answer on May 23, 2026 at 10:03 pm

    One way by using a table of numbers

    declare @dt datetime
    set @dt = '2011-07-21'
    
    select DATEADD(mi, number, @dt) 
    from master..spt_values
    where type = 'P'
    and DATEADD(mi, number, @dt)  < '2011-07-22'
    

    If you have your own number table, use that

    See here for more info http://wiki.lessthandot.com/index.php/Date_Ranges_Without_Loops

    you full query would be like

    DECLARE @dt DATETIME
    SET @dt = '2011-07-21'
    
    SELECT x.SomeTime,y.TheCount FROM 
    (SELECT DATEADD(mi, number, @dt) as SomeTime FROM master..spt_values
    WHERE TYPE = 'P'
    AND DATEADD(mi, number, @dt)  < '2011-07-22') x
    LEFT JOIN (
        SELECT TIMESTAMP, COUNT(*) AS TheCount
        FROM Actions
        GROUP BY TIMESTAMP
    ) AS y
    ON x.SomeTime = dateadd(mi, datediff(mi, 0, y.Timestamp)+0, 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to construct a SQL query that I think requires multiple JOIN's, but
I'm trying to construct a query that will include a column indicating whether or
I've been trying to construct a sql query with ZendFW, but I cant seem
Trying to construct a query in Access 2007. One of the columns has the
MSSQL 2008. I am trying to construct a SQL statement which returns the total
Very complex query been trying to construct it for few days with more real
I am stuck trying to construct a JPQL query and was hoping someone with
I'm trying to construct a find command to process a bunch of files in
I am trying to construct a way to keep certain hard drive partitions/usb drives
I am trying to define a WCF contract that returns an interface, something like

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.