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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:50:01+00:00 2026-06-14T11:50:01+00:00

Given a table of projects with start and end dates as well as a

  • 0

Given a table of projects with start and end dates as well as a region that they are taking place, I am trying to get my result to output the number of active projects per week over a given interval and grouped by region.

I have many records in Projects that look like

region         start_date         end_date
Alabama        2012-07-08         2012-08-15
Texas          2012-06-13         2012-07-24
Alabama        2012-07-25         2012-09-13
Texas          2012-08-08         2012-10-28
Florida        2012-07-03         2012-08-07
Lousiana       2012-07-14         2012-08-12
....

If I want results for a single week, I can do something like

DECLARE @today datetime
SET @today ='2012-11-09'

SELECT 
[Region],
count(*) as ActiveProjectCount
FROM [MyDatabase].[dbo].[Projects]
where (datecompleted is null and datestart < @today) OR (datestart < @today AND @today     < datecompleted)
Group by region
order by region asc

This produces

Region           ActiveProjectCount
Arkansas                15
Louisiana               18
North Dakota            18
Oklahoma                27
...

How can I alter this query to produce results that look like

Region          10/06   10/13   10/20   10/27    
Arkansas          15      17      12      14
Louisiana          3       0       1       5
North Dakota      18      17      16      15
Oklahoma          27      23      19      22
...

Where on a weekly interval, I am able to see the total number of active projects (projects between start and end date)

  • 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-06-14T11:50:03+00:00Added an answer on June 14, 2026 at 11:50 am

    you could do sth. like this:

    with "nums"
    as
    (
      select 1 as "value"
      union all select "value" + 1 as "value"
      from "nums"
      where "value" <= 52
    )
    , "intervals"
    as
    (
      select
      "id" = "value"
      , "startDate" = cast( dateadd( week, "value" - 1, dateadd(year, datediff(year, 0, getdate()), 0)) as date )
      , "endDate" = cast( dateadd( week, "value", dateadd( year, datediff( year, 0, getdate()), 0 )) as date )
    
    from
      "nums"
    )
    , "counted"
    as
    (
    select
      "intervalId" = I."id"
      , I."startDate"
      , I."endDate"
      , D."region"
      , "activeProjects" = count(D."region") over ( partition by I."id", D."region" )
    
    from
      "intervals" I
      inner join "data" D
        on D."startDate" <= I."startDate"
        and D."endDate" > I."endDate"
    )
    
    select
      *
    from
    (
      select
        "region"
        , "intervalId"
      from
        "counted"
    ) as Data
    pivot
    ( count(Data."intervalId") for intervalId in ("25", "26", "27", "28", "29", "30", "31")) as p
    

    intervals can be defined as you wish.

    see SQL-Fiddle

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

Sidebar

Related Questions

Given a table in MySQL with rows that have a 'Country' field, I need
Given a table row, I want to get the HTML out of the span
Given a user, select only the projects that he is interested in. So a
Given table: ID ONE TWO X1 15 15 X2 10 - X3 - 20
Given the table: CREATE TABLE IF NOT EXISTS `users` ( `id` bigint(20) NOT NULL
Given: CREATE TABLE foo (id BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id)); I'd like to
Given a table like: CREATE TABLE MyTable ( MyColumn NUMBER NOT NULL ); I
Given a table where the first column is seconds past a certain reference point
Given a table of logical resource identifiers (one per row), what is the best
Given a table containing dotted quad IPv4 addresses stored as a VARCHAR(15) , for

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.