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.
Results:
FWIW I just started a blog series on this very thing:
http://www.sqlperformance.com/2013/01/t-sql-queries/generate-a-set-1