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

The Archive Base Latest Questions

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

First off, thanks to anyone who helps me solve this problem. I am using

  • 0

First off, thanks to anyone who helps me solve this problem. I am using SQL 2005, but can use 2008 if no solution is available in 05.

I have a rows of data that look like such:

select * from mySPtable

| myPK | Area | RequestType |  StartDate  |  EndDate  |
   1      SB        ADD        8/14/2011    8/18/2011
   2      NB        RMV        8/16/2011    8/16/2011

So what I want to do is count up the total requests for each area by day. Results should be:

|  myDate  | RequestType |  Area  | myCount |
  8/14/2011      ADD         SB        1
  8/15/2011      ADD         SB        1
  8/16/2011      ADD         SB        1
  8/16/2011      RMV         NB        1
  8/17/2011      ADD         SB        1
  8/18/2011      ADD         SB        1

How do I go about doing this? I’m stumped and no amount of googling has helped.

  • 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-24T22:39:04+00:00Added an answer on May 24, 2026 at 10:39 pm

    You’ll need either a Calendar table or you can generate one with a CTE. Once you have that, the rest of the query should be fairly trivial. The CTE approach can be a little complex due to recursion issues and not being allowed to use aggregates, so below I’ve used a table variable. You can also make this a permanent table that you keep in your database.

    SET NOCOUNT ON
    
    DECLARE @Calendar TABLE (my_date DATETIME NOT NULL)
    DECLARE @date DATETIME, @max_date DATETIME
    
    SELECT @date = MIN(StartDate), @max_date = MAX(EndDate) FROM My_Table
    
    WHILE (@date <= @max_date)
    BEGIN
        INSERT INTO @Calendar (my_date) VALUES (@date)
        SELECT @date = DATEADD(dy, 1, @date)
    END
    
    SELECT
        C.myDate,
        M.RequestType,
        M.Area,
        COUNT(*) AS myCount
    FROM
        @Calendar C
    INNER JOIN My_Table M ON
        M.StartDate <= C.myDate AND
        M.EndDate >= C.myDate
    GROUP BY
        C.myDate,
        M.RequestType,
        M.Area
    ORDER BY
        C.myDate,
        M.RequestType,
        M.Area
    

    Depending on how large your potential date range is, filling the table variable could take awhile. For example, if the range spanned a decade or two.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, thanks so much to everyone who has helped me on this
Can anyone explain to me what is going on here? First off, I think
This starts off as an Android question but I think becomes a linux/cygwin question.
First off, I know there are ways to make it so that text can
This may be a simple question for you experts but I can't figure out
First off, I'm not asking how to find the path or use the IO
I have an emergency issue at hand. First off, yes, I realize this was
Now this probably seems a little stupid at first, but I am creating a
First off, I'd like to apologize in advance for not knowing this. I've been
First off, I'm using Tomcat 5.5 and my .jsp's live in /webapps/foo/bar/*.jsp. I followed

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.