使用下面的代码我可以在本月和过去6个月返回。 我的代码:
SELECT TO_CHAR(add_months(TRUNC(to_date( sysdate),'Month'), -rownum+1), 'Month') mon,
rownum month_order
FROM dual
CONNECT BY rownum <=
(SELECT COUNT(mon)
FROM
(SELECT TO_CHAR( add_months( start_date, level-1 ), 'fmMonth' ) AS mon
FROM
(SELECT to_date( add_months(TRUNC(sysdate),-6)) start_date,
to_date( sysdate) end_date
FROM dual)
CONNECT BY level <= months_between( TRUNC(end_date,'MM'), TRUNC(start_date,'MM') ) + 1) dual);
同样地,我必须返回此日期和过去9个日期。
请帮助我使用connect by
返回10个日期。
提前谢谢。
答案 0 :(得分:0)
试试这个 -
SELECT ( TRUNC( SYSDATE ) + 1 ) - ROWNUM
FROM DUAL
CONNECT BY ROWNUM <= 10
答案 1 :(得分:0)
这是过去十天的查询。
select sysdate - (level-1)
, level as day_order
from dual
connect by level <= 10;
您的月份查询似乎极度过度设计。这会做同样的事情:
select to_char(add_months(trunc(sysdate, 'MM'), 1 - level), 'Month')
, level as month_order
from dual
connect by level <= 7;
“假设数据在今天的日期可用,剩余的9个日期没有任何数据,但必须将计数显示为零”
在外部联接中使用生成的结果集:
with q as (
select sysdate - (level-1) as dt
, level as day_order
from dual
connect by level <= 10
)
select q.dt as txn_date
, sum(t42.col1)
from q
left outer join t42
on t42.transaction_date = q.dt
group by q.dt;
答案 2 :(得分:0)
以同样的方式,我必须返回此日期和过去的9个日期。
这将是今天和过去9天:
SELECT TRUNC( SYSDATE ) - LEVEL + 1 AS day
FROM DUAL
CONNECT BY LEVEL <= 10
本月和过去6个月
SELECT TO_CHAR( ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 1 - LEVEL ), 'Month' ) AS month
FROM DUAL
CONNECT BY LEVEL <= 7