我有一个查询,我可以生成下表,
Name School Department StartDate Enddate
John ABC School Math 7/1/2004 7/18/2007
John ABC School Science 7/1/2010 Nulll
Steve XYZ School English 2/1/2004 9/30/2006
Steve XYZ School Geology 10/1/2006 Null
我想知道有没有办法根据会计年度将每行拆分成多行
Name School Department Fiscal Year
John ABC School Math 2005
John ABC School Math 2006
John ABC School Math 2007
John ABC School Science 2011
John ABC School Science 2012
Steve XYZ School English 2004
Steve XYZ School English 2005
Steve XYZ School English 2006
Steve XYZ School Geology 2007
Steve XYZ School Geology 2008
Steve XYZ School Geology 2009
Steve XYZ School Geology 2010
Steve XYZ School Geology 2011
Steve XYZ School Geology 2012
答案 0 :(得分:0)
假设存在一个表f
来定义具有开始和结束日期的会计年度,您可以使用以下内容执行此操作:
SELECT f.name, t.name FROM t
INNER JOIN f
ON f.start BETWEEN t.start AND t.end
OR f.end BETWEEN t.start AND t.end
答案 1 :(得分:0)
您可以使用COALESCE处理NULL结束日期
SELECT f.name, t.name FROM t
INNER JOIN f
ON f.start BETWEEN t.start AND COALESCE(t.end,CURRENT_DATE)
OR f.end BETWEEN t.start AND COALESCE(t.end,CURRENT_DATE)