sql - How to duplicate Rows with new entries -
i have sql table have id , start , end dates. example,
id startdt enddt 123 1/1/2010 12/31/2014 456 7/16/2013 11/20/2014
based on oct-sept fy calendar can fy dates (e.g., 2010 , 2015 id 123). however, duplicate row initial , last fy; , in-between fy. below have above rows of data:
id fy
123 2010
123 2011
123 2012
123 2014
123 2015
456 2013
456 2014
the query below uses recursive cte count years start date fiscal year end date fiscal year.
;with cte ( select id, year(startdt) fy, year(enddt) ey [source] union select id, fy + 1, ey cte fy < ey ) select id, fy cte order id, fy
Comments
Post a Comment