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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:29:29+00:00 2026-05-15T09:29:29+00:00

This is how a table is presented SELECT RequestsID, Country, Activity, [People needed (each

  • 0

This is how a table is presented

SELECT RequestsID, Country, Activity,
[People needed (each day)], [Start date], [End date]
FROM dbo.Requests

There will be a lot of requests, and I would like to sum up the “People needed” per day (!), not as Between Start- and End date.

Also I would like to group by country, and have the possibility to set between which dates I want to get data.

Some days might be empty regarding needed people (0), but the Date should be presented anyway.
Note that there can be several requests pointing out the same Dates, and the same Country – but the Activity is then different.

The query should be like (well, it´s not SQL as you can see, just trying to show the logic)

From Requests,
show Country and SUM 'People needed'
where (column not in Requests table-Date) is a Date (will be
a number of dates, I want to set the scope by a Start and End date)
and Requests.Country is @Country 
(and the Date(s) above of course is between the Requests Start date and End date...)
And from (a non existing table...?) show Date
Group by Country

I would like to see something like this:

Date            Country         People needed

06/01/2010      Nigeria          34 // this might be from three different Requests, all pointing out Nigeria. People needed might be (30+1+3 = 34)
06/02/2010      Nigeria          10
06/03/2010      Nigeria           0
06/04/2010      Nigeria           1
06/05/2010      Nigeria         134

06/01/2010      China             2
06/02/2010      China             0
06/03/2010      China            14
06/04/2010      China            23
06/05/2010      China            33

06/01/2010      Chile             3
06/02/2010      Chile             4
06/03/2010      Chile             0
06/04/2010      Chile             0
06/05/2010      Chile            19

How would you do it?

NOTE:
I would like to see some kind of example code, to get started 🙂

  • 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-15T09:29:30+00:00Added an answer on May 15, 2026 at 9:29 am

    Normally, I’d suggest having a static calendar table which contains a sequential list of dates. However, using Cade Roux’s clever approach of generating a calendar table, you would have something like:

    ;With Calendar As
        (
         Select Cast(Floor(Cast(@StartDate As float)) As datetime) As [Date]
         Union All
         Select DateAdd(d, 1, [Date])
         From Calendar
         Where DateAdd(d, 1, [Date]) < @EndDate
        )
    Select C.[Date], R.Country, Sum(R.PeopleNeeded)
    From Calendar As C
        Left Join Requests As R
            On C.[Date] Between R.[Start Date] And R.[End Date]
                And ( @Country Is Null Or R.Country = @Country )
    Group By C.[Date], R.Country    
    Option (MAXRECURSION 0); 
    

    Now, if it is the case that you want to filter on country such that the only days returned are those for the given country that have data, then you would simply need to change the Left Join to an Inner Join.

    ADDITION

    From the comments, it was requested to show all countries whether they have a Request or not. To do that, you need to cross join to the Countries table:

    With Calendar As
        (
         Select Cast(Floor(Cast(@StartDate As float)) As datetime) As [Date]
         Union All
         Select DateAdd(d, 1, [Date])
         From Calendar
         Where DateAdd(d, 1, [Date]) < @EndDate
        )
    Select C.[Date], C2.Country, Sum(R.PeopleNeeded)
    From Calendar As C
        Cross Join Countries As C2
        Left Join Requests As R
            On C.[Date] Between R.[Start Date] And R.[End Date]
                And R.CountryId = C2.CountryId
    Group By C.[Date], C2.Country    
    Option (MAXRECURSION 0); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.