Oracleデータベースの統計データを時間単位でグループ化する方法
4441 ワード
Oracleの異なる時間別グループ統計のsqlは、次の表table 1に示されています.
注:より直感的に表示するには、次のクエリが対応するパケットでソートされます.
1.年ごとにグループ化
2.月ごとにグループ化
3.四半期ごとのグループ化
4.週単位でグループ化
PS:Oracleの期間別グループ統計期間別グループクエリーを作成するには、まずlevel,connect by,oracle時間の増減を理解します.levelについてはあまり言いませんが、クエリー文は1つしか書きません.
oracle時間の加減は、次のsql文を試してみるとわかります.
次の例を示します.
group by d.dt
例のsysdate-(level-1)*2は、2日間隔の時間group by d.dt、すなわち2日間隔のパケットクエリを得る
自己実現例:
select(sysdate-13)-(level-1)/4 from dual connect by level<=34--最初のタイムレコードから(sysdate-13)がテーブルの最初の日付であり、「34」が表示されるパケット数(1日6時間ごとに4)
次は6時間ごとにグループ化されます
group by mobile_no,area_name,d.dt
もう1つの方法:
(exportDate) (amount)
-------------- -----------
14-2 -08 20
10-3 -08 2
14-4 -08 6
14-6 -08 75
24-10 -09 23
14-11 -09 45
04-8 -10 5
04-9 -10 44
04-10 -10 88
注:より直感的に表示するには、次のクエリが対応するパケットでソートされます.
1.年ごとにグループ化
select to_char(exportDate,'yyyy'),sum(amount) from table1 group by to_char(exportDate,'yyyy');
-----------------------------
2009 68
2010 137
2008 103
2.月ごとにグループ化
select to_char(exportDate,'yyyy-mm'),sum(amount) from table1 group by to_char(exportDate,'yyyy-mm')
order by to_char(exportDate,'yyyy-mm');
-----------------------------
2008-02 20
2008-03 2
2008-04 6
2008-06 75
2009-10 23
2009-11 45
2010-08 5
2010-09 44
2010-10 88
3.四半期ごとのグループ化
select to_char(exportDate,'yyyy-Q'),sum(amount) from table1 group by to_char(exportDate,'yyyy-Q')
order by to_char(exportDate,'yyyy-Q');
------------------------------
2008-1 22
2008-2 81
2009-4 68
2010-3 49
2010-4 88
4.週単位でグループ化
select to_char(exportDate,'yyyy-IW'),sum(amount) from table1 group by to_char(exportDate,'yyyy-IW')
order by to_char(exportDate,'yyyy-IW');
------------------------------
2008-07 20
2008-11 2
2008-16 6
2008-24 75
2009-43 23
2009-46 45
2010-31 5
2010-35 44
2010-40 88
PS:Oracleの期間別グループ統計期間別グループクエリーを作成するには、まずlevel,connect by,oracle時間の増減を理解します.levelについてはあまり言いませんが、クエリー文は1つしか書きません.
----level
select level from dual connect by level <=10
--- :
1
2
3
4
5
6
7
8
9
10
oracle時間の加減は、次のsql文を試してみるとわかります.
select sysdate -1 from dual
---- , 24
select sysdate-(1/2) from dual
----- , 12
select sysdate-(1/24) from dual
----- 1
select sysdate-((1/24)/12) from dual
---- 5
select sydate-(level-1) from dual connect by level<=10
--- 10 1
次の例を示します.
select dt, count(satisfy_degree) as num from T_DEMO i ,
(select sysdate - (level-1) * 2 dt
from dual connect by level <= 10) d
where i.satisfy_degree='satisfy_1' and
i.insert_time
d.dt-2 group by d.dt
例のsysdate-(level-1)*2は、2日間隔の時間group by d.dt、すなわち2日間隔のパケットクエリを得る
自己実現例:
create table A_HY_LOCATE1
(
MOBILE_NO VARCHAR2(32),
LOCATE_TYPE NUMBER(4),
AREA_NO VARCHAR2(32),
CREATED_TIME DATE,
AREA_NAME VARCHAR2(512),
);
select(sysdate-13)-(level-1)/4 from dual connect by level<=34--最初のタイムレコードから(sysdate-13)がテーブルの最初の日付であり、「34」が表示されるパケット数(1日6時間ごとに4)
次は6時間ごとにグループ化されます
select mobile_no,area_name,max(created_time ),dt, count(*) as num from a_hy_locate1 i ,
(select (sysdate-13)-(level-1)/4 dt
from dual connect by level <= 34) d
where i.locate_type = 1 and
i.created_time
d.dt-1/4group by mobile_no,area_name,d.dt
もう1つの方法:
--
select trunc(to_number(to_char(created_time, 'hh24')) / 6),count(*)
from t_test
where created_time > trunc(sysdate - 40)
group by trunc(to_number(to_char(created_time, 'hh24')) / 6)
-- 12
select trunc(to_number(to_char(created_time, 'hh24')) / 6),count(*)
from t_test
where created_time > trunc(sysdate - 40)
group by trunc(to_number(to_char(created_time, 'hh24')) / 6)