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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:37:37+00:00 2026-06-17T08:37:37+00:00

I have a table, an example shown below, that lists people and their birth

  • 0

I have a table, an example shown below, that lists people and their birth and death years (9999 indicates an alive person).

person | born | died
1      | 2001 | 9999
2      | 2002 | 2003
3      | 2002 | 2008
4      | 2004 | 2009
5      | 2005 | 9999
6      | 2010 | 2012

I want to compute the population over the years. For the example shown above, the output is going to be the following. The list of persons alive at the end of the rows is for demonstration purposes only, I don’t need that in my output.

year | population
2001 | 1                  (1)
2002 | 3                  (1,2,3)
2003 | 2                  (1,3)
2004 | 3                  (1,3,4)
2005 | 4                  (1,3,4,5)
2006 | 4                  (1,3,4,5)
2007 | 4                  (1,3,4,5)
2008 | 3                  (1,4,5)
2009 | 2                  (1,5)
2010 | 3                  (1,5,6)
2011 | 3                  (1,5,6)
2012 | 2                  (1,5)

I am able to get this writing a C# program and looping through each of the rows in the original table and incrementing the population count of each of the years that fall between the birth and death years. However, I need to do it entirely in a TSQL script. I am a TSQL newbie. How do I go about it? If a similar problem has already been discussed, please point me to it. Thank you.

  • 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-17T08:37:38+00:00Added an answer on June 17, 2026 at 8:37 am
    DECLARE @t TABLE(person INT, born INT, died INT);
    
    INSERT @t VALUES
    (1,2001,9999),
    (2,2002,2003),
    (3,2002,2008),
    (4,2004,2009),
    (5,2005,9999),
    (6,2010,2012);
    
    DECLARE @start INT, @end INT;
    
    SELECT @start = 2001, @end = 2012;
    
    ;WITH n(y) AS 
    (
      SELECT TOP (@end - @start + 1) 
        @start - 1 + ROW_NUMBER() OVER (ORDER BY [object_id])
      FROM sys.all_objects
    )
    SELECT 
      [year] = n.y, 
      population = SUM(CASE WHEN n.y >= t.born AND n.y < t.died THEN 1 ELSE 0 END) 
    FROM n CROSS JOIN @t AS t GROUP BY n.y;
    

    Results:

    year    population
    ----    ----
    2001    1
    2002    3
    2003    2
    2004    3
    2005    4
    2006    4
    2007    4
    2008    3
    2009    2
    2010    3
    2011    3
    2012    2
    

    FWIW I just started a blog series on this very thing:

    http://www.sqlperformance.com/2013/01/t-sql-queries/generate-a-set-1

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

Sidebar

Related Questions

I have a SQLite table called posts . An example is shown below. I
I have a table similar to the example shown below. I would like to
i have a table t_points in mySql like example in below. Name Surname Point
I have a table that contains common entries in two columns. For Example: Column1
I have a row that provides me values like shown below: ClubbedValues ----------------------------------------------------- 561#557,
I have a query as shown below: TRUNCATE TABLE DNARTLOAD; COMMIT; .... <Cursor here>
I have three records in a table example: *Usernames* *email* *city* Nick nick@example.com London
Ok, I have an example table with the following information and query. First up
For example I have a table. With a field Idnumber. And the field has
For example I have a table MyTable like this: Id Value Rate 1 10

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.