我正在尝试创建一个显示特定时间间隔内日期的查询。例如,如果我有一个hire_date且min(hire_date)是2009-01-01而max(hire_date)是2009-03-1,那么表格应该以2天的频率显示hire_date。像:
+---------+
hire_date
+---------+
2009-01-01
2009-01-03
2009-01-05
2009-01-07
..........
名为Employee_Service的表包含:
ID Number
Hire_date Timestamp
Name Varchar
我使用的当前查询是:
select trunc(hire_date) "Date",
count(id) "Num Empl Opened"
from Employee_Service
group by trunc(hire_date)
order by trunc(hire_date)
有没有办法为此构建查询。
答案 0 :(得分:1)
您可以尝试:
select trunc(hire_date) "Date",
count(id) "Num Empl Opened"
from Employee_Service
where mod(to_char(hire_date,'dd'),2)<>0
and trunc(hire_date) between to_date('2009-01-01','yyyy-mm-dd') and to_date('2009-03-01','yyyy-mm-dd')
group by trunc(hire_date)
order by trunc(hire_date)
答案 1 :(得分:0)
试试这个:
WITH DAYS AS
(SELECT TRUNC(TO_DATE('2009-03-01','YYYY-mm-dd')) - ROWNUM*2 D
FROM ALL_OBJECTS
WHERE ROWNUM < 365)
SELECT
DAYS.D
FROM
DAYS WHERE D >= TO_DATE('2009-01-01','YYYY-mm-dd');
答案 2 :(得分:0)
我想这样的查询可以帮到你
select to_date('01/05/2014','DD/MM/YYYY') + level -1 HIRE_DATE
from dual
WHERE MOD(LEVEL,2) <> 0
connect by level <= to_date('01/06/2014','DD/MM/YYYY') - to_date('01/05/2014','DD/MM/YYYY') +1;
WHERE '01 / 05/2014'从日期开始,'01/06 / 2014'截止日期。
所以你的查询是:
如果hire_date是DD / MM / YYYY格式的VARCHAR
select to_date(min(hire_date),'DD/MM/YYYY') + level -1 HIRE_DATE
from dual
WHERE MOD(LEVEL,2) <> 0
connect by level <= to_date(max(hire_date),'DD/MM/YYYY') - to_date(min(hire_date),'DD/MM/YYYY') +1;
或者,
如果HIRE_DATE是日期字段,
select (min(hire_date)) + level -1 HIRE_DATE
from dual
WHERE MOD(LEVEL,2) <> 0
connect by level <= (max(hire_date)) - (min(hire_date)) +1;
修改强> Max和Min不能与Group by一起使用。所以请使用以下格式:
with cte as
(
select max(hire_date) max_date, min(hire_date) min_date from my_table
where --- all conditions here
and rownum=1
)
select (min_date + level -1) hire_dates
from dual
WHERE MOD(LEVEL,2) <> 0
connect by level <= ( max_date - min_date +1 );
with cte as
(
select max(hire_date) max_date, min(hire_date) min_date from Employee_Service
)
select (min_date + level -1) "Date", count(id) "Num Empl Opened"
from Employee_Service , cte
WHERE MOD(LEVEL,2) <> 0
group by (min_date + level -1)
connect by level <= ( max_date - min_date +1 )
ORDER BY (min_date + level -1);