在 Oracle 中动态构建数字表
如何根据当前日期返回由过去四年组成的行集?
如果此查询在 2010 年 12 月 31 日运行,则应返回:
2007
2008
2009
2010
但如果在 2011 年 1 月 1 日运行,则应返回:
2008
2009
2010
2011
这是我开始的内容,两个查询返回起始年份。我更喜欢第二种,因为转换为字符串对我来说有点脏。
SELECT TO_CHAR(TRUNC(sysdate, 'YY') - INTERVAL '3' YEAR, 'YYYY') FROM DUAL;
SELECT EXTRACT (YEAR FROM sysdate) - 3 FROM DUAL;
但我不知道如何生成行来充实这一点。在 SQL Server 中,我将使用 CTE,如 本页的 fn_nums 函数。
How do I return a rowset consisting of the last four years based on the current date?
If this query runs on 12/31/2010 it should return:
2007
2008
2009
2010
But if it is run on 1/1/2011 it should return:
2008
2009
2010
2011
Here's what I started with, two queries that return the starting year. I prefer the second as converting to string feels a bit dirty to me.
SELECT TO_CHAR(TRUNC(sysdate, 'YY') - INTERVAL '3' YEAR, 'YYYY') FROM DUAL;
SELECT EXTRACT (YEAR FROM sysdate) - 3 FROM DUAL;
But I don't know how to generate rows to flesh this out. In SQL Server I'd use a CTE as in the fn_nums function on this page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要显示 1 到 100 之间的数字:
要更改最大值,请在第三行中更改它。
To display numbers from 1 to 100:
To change the max, change it in the third line.
这是一种方法:
或:
或:
Here is one way:
Or:
Or:
与接受的答案类似,您可以用 with 子句替换内联视图。我发现 with 子句更具可读性。特别是如果您要作为同一查询的一部分对动态 NUMBERS 表执行多个计算 - 整个 SQL 语句更具可读性。
动态数字表:
输出:
Similar to the accepted answer you can replace the inline view with a with clause. I find the with clause more readable. In particular if you are going to perform multiple calculations against the on-the-fly NUMBERS table as part of the same query - the entire SQL statement is more readable.
Numbers table on the fly:
Output: