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 4232866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:08:58+00:00 2026-05-21T02:08:58+00:00

I have a table of registrations, around 300K records. I need a SQL statement

  • 0

I have a table of registrations, around 300K records. I need a SQL statement that will show the total number of registrations for that particular day?

select
count('x'),CONVERT(varchar(12),date_created,111)
from reg group by
cONVERT(varchar(12),date_created,111)
order by
CONVERT(varchar(12),date_created,111)

Result of this query:

169      2011/03/24
3016     2011/03/25
2999     2011/03/26

Desired outcome:

 2011/03/25  3016+169
 2011/03/26  2999+3016+169

How can this be done?

  • 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-21T02:08:59+00:00Added an answer on May 21, 2026 at 2:08 am

    Here is two versions to do this. I have tested with 100000 rows spread over 6000 days on a really slow computer with not enough memory, and that shows that the cte version is faster than the loop version. The other versions suggested here (so far) is a lot slower, provided that I have understood the problem correctly.

    Recursive CTE (10 seconds)

    -- Table variable to hold count for each day
    declare @DateCount table(d int, c int, rn int)
    insert into @DateCount
      select 
        datediff(d, 0, date_created) as d,
        count(*) as c,
        row_number() over(order by datediff(d, 0, date_created)) as rn
      from reg
      group by datediff(d, 0, date_created)
    
    -- Recursive cte using @DateCount to calculate the running sum
    ;with DateSum as
    (
      select 
        d, c, rn
      from @DateCount
      where rn = 1
      union all
      select 
        dc.d, ds.c+dc.c as c, dc.rn
      from DateSum as ds
        inner join @DateCount as dc
          on ds.rn+1 = dc.rn  
    )
    select
      dateadd(d, d, 0) as date_created,
      c as total_num
    from DateSum
    option (maxrecursion 0)
    

    Loop (14 seconds)

    -- Table variable to hold count for each day
    declare @DateCount table(d int, c int, rn int, cr int)
    insert into @DateCount
      select 
        datediff(d, 0, date_created) as d,
        count(*) as c,
        row_number() over(order by datediff(d, 0, date_created)) as rn,
        0
      from reg
      group by datediff(d, 0, date_created)
    
    declare @rn int = 1
    
    -- Update cr with running sum
    update dc set
      cr = dc.c  
    from @DateCount as dc
    where rn = @rn
    
    while @@rowcount = 1
    begin
      set @rn = @rn + 1
    
      update dc set
        cr = dc.c + (select cr from @DateCount where rn = @rn - 1)  
      from @DateCount as dc
      where rn = @rn
    end
    
    -- Get the result
    select
      dateadd(d, d, 0) as date_created,
      cr as total_num
    from @DateCount
    

    Edit 1 The really fast version

    The quirky update

    -- Table variable to hold count for each day
    declare @DateCount table(d int primary key, c int, cr int)
    insert into @DateCount
      select 
        datediff(d, 0, date_created) as d,
        count(*) as c,
        0
      from reg
      group by datediff(d, 0, date_created)
    
    declare @rt int = 0
    declare @anchor int
    
    update @DateCount set
      @rt = cr = @rt + c,
      @anchor = d
    option (maxdop 1)
    
    -- Get the result
    select
      dateadd(d, d, 0) as date_created,
      cr as total_num
    from @DateCount                
    order by d
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in a SQL Server 2005 database with a trigger that
While trying to use LINQ to SQL I encountered several problems. I have table
I have a table called Registrations with the following fields: Id DateStarted (not null)
I have a SQL Table with the following columns: PROFILE ---------------------------------- Name | DateOfBirth
I have a script that appends some rows to a table. One of the
I have a MySQL table containing fixedprice-registrations both monthly and with a certain date,
I'm using sql-server 2005 Hi, i have Users table with userID and registrationDate. I
Preamble: I'm creating some tables in SQL Server that will be accessed via Linq-to-entities
I have a table of vehicles with registration numbers, and want to select a
I have table inside a div tab. The table has 40 rows in it

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.