oraclesql查詢時間段
① oracle sql語句中怎麼查詢一個月內固定時間段的數據,比如9月1號到10月1號每天的八點到九點的呼叫數目
使用Oracle 函數extract(fmt from d)獲取日期中的特定部分:
select count(*) from table where
SQL> select * from table where createdate
between to_date('2010-9-1','yyyy-MM-dd') and to_date('2010-10-1','yyyy-MM-dd')
and EXTRACT(hour from createdate) between '8:00' and '9:00';
② oracle資料庫 查詢時間段一年的數據的SQL語句怎麼寫。 比如查詢一年白班時間(八點至五點)的數據
使用to_char()將時間欄位轉換成字元串然後再做。例如
select * from table1
where to_char(t,'HH24')>=8 and to_char(t,'HH24')<17
③ oracle 查詢一段時間內每一天的統計數據sql怎麼寫
可以寫一個簡單的procere來實現,原理是遍歷日期范圍,並查詢日期資料筆數,寫入表。
數據源表test03
1 2016-06-01 1
2 2016-06-02 1
3 2016-06-05 1
4 2016-06-04 1
5 2016-06-04 1
procere代碼如下:
create or replace procere loop_by_date(pbeg_tim in varchar2,--開始日期
pend_tim in varchar2,--結束日期
errmessage out varchar2) is
nCount number(10); --總天數
i_point number(10); --當天
is_zero number(10); --當天是否有記錄
begin
nCount := 0;
i_point := 0;
is_zero := 0;
select ROUND(to_date(pend_tim, 'yyyy-mm-dd') -
to_date(pbeg_tim, 'yyyy-mm-dd'))
into nCount
from al;
delete from test02;
<<fst_loop>>
loop
select count(*)
into is_zero
from test03
where date1 =
to_char(to_date(pbeg_tim, 'yyyy-mm-dd') + i_point, 'yyyy-mm-dd');
insert into test02
(date01, nccount)
values
(to_char(to_date(pbeg_tim, 'yyyy-mm-dd') + i_point, 'yyyy-mm-dd'),
is_zero);
i_point := i_point + 1;
exit fst_loop when i_point >= nCount;
end loop fst_loop;
--end;
end loop_by_date;
傳入參數"2016-06-01"~~"2016-06-10"並執行,結果寫入test02為:
1 2016-06-01 1
2 2016-06-02 1
3 2016-06-03 0
4 2016-06-04 2
5 2016-06-05 1
6 2016-06-06 0
7 2016-06-07 0
8 2016-06-08 0
9 2016-06-09 0
④ sql 查詢某欄位的某段內容
如果 表結構是這樣的話 text ,sdate 的話 可以使用
select test||sdate from book where sdate = (select min(sdate) from book where text ='張三')