oracle sql查询列出上个月的所有日期
伙计们,我需要列出上个月的所有日期,如下所示,
20101201
20101202
20101203
20101204
20101205
..
..
..
..
..
..
..
..
20101231
请告诉我是否有比此查询更好的方法。
select TO_CHAR(TRUNC(SYSDATE,'MM')-1,'YYYYMMDD')-(level-1) as
EACH_DATE from dual A connect by level
< (TO_NUMBER(TO_CHAR(TRUNC(SYSDATE,'MM')-1,'DD'))+1)
另请让我知道此查询的问题,它显示“缺少右括号”
SELECT /*+ PARALLEL (A,8) */ /*+ DRIVING_STATE */
TO_CHAR(TRUNC(TRUNC(SYSDATE,'MM')-1,'MM'),'MONYYYY') "MONTH", TYPE AS "TRAFF", COLUMN, A_COUN AS "A_COUNT",COST FROM DATA_P B WHERE EXISTS
(
select TO_NUMBER(TO_CHAR(TRUNC(SYSDATE,'MM')-1,'YYYYMMDD')-(level-1)) EACH_DATE
from dual A connect by level < TO_NUMBER(TO_CHAR(TRUNC(SYSDATE,'MM')-1,'DD')+1)
WHERE A.EACH_DATE = B.DATE order by EACH_DATE ASC
)
强调文本
Guys i have a requirement to list all the dates of the previous month like below
20101201
20101202
20101203
20101204
20101205
..
..
..
..
..
..
..
..
20101231
kindly let me know if any better way to do than this query.
select TO_CHAR(TRUNC(SYSDATE,'MM')-1,'YYYYMMDD')-(level-1) as
EACH_DATE from dual A connect by level
< (TO_NUMBER(TO_CHAR(TRUNC(SYSDATE,'MM')-1,'DD'))+1)
Also please let me know the problem with this query it says "missing right parenthesis"
SELECT /*+ PARALLEL (A,8) */ /*+ DRIVING_STATE */
TO_CHAR(TRUNC(TRUNC(SYSDATE,'MM')-1,'MM'),'MONYYYY') "MONTH", TYPE AS "TRAFF", COLUMN, A_COUN AS "A_COUNT",COST FROM DATA_P B WHERE EXISTS
(
select TO_NUMBER(TO_CHAR(TRUNC(SYSDATE,'MM')-1,'YYYYMMDD')-(level-1)) EACH_DATE
from dual A connect by level < TO_NUMBER(TO_CHAR(TRUNC(SYSDATE,'MM')-1,'DD')+1)
WHERE A.EACH_DATE = B.DATE order by EACH_DATE ASC
)
emphasized text
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
听起来你想要这样的东西
It sounds like you want something like this
当月:
for current month :
一点 add_months 肯定会让它变得更好,例如
A bit of add_months would definitely make it better, as in e.g.
这可能更容易理解一点:
但是,“按级别连接”方法是最清晰的,并且如这里,更快地生成数字序列的方法。我认为没有办法显着改善您的查询。
This may be a little easier to understand:
However, the "connect by level" method is the most clear, and as described here, faster way to generate sequence of numbers. I don't think there is no way to dramatically improve your query.
就正确的括号而言,您正在尝试以错误的方式连接字符串:
应该有效:
显然您不希望发生连接。因此,我认为您实际上想将级别添加到
TRUNC()
部分修复:
As far as the right parenthesis is concerned, you are trying to concatenate strings the wrong way:
should work:
Obviously you don't want the concatenation to happen. Therefore, I think you actually want to add the level to the
TRUNC()
-partFix: