Oracle按不同時(shí)間分組統(tǒng)計(jì)的sql

創(chuàng)新互聯(lián)主要從事成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)旌陽(yáng),10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢(xún)建站服務(wù):18982081108
如下表table1:
日期(exportDate)
數(shù)量(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
注意:為了顯示更直觀,如下查詢(xún)已皆按相應(yīng)分組排序
1.按年份分組
select
to_char(exportDate,'yyyy'),sum(amount)
from
table1
group
by
to_char(exportDate,'yyyy');
年份
數(shù)量
-----------------------------
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');
月份
數(shù)量
-----------------------------
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');
季度
數(shù)量
------------------------------
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');
周
數(shù)量
------------------------------
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按時(shí)間段分組統(tǒng)計(jì)
想要按時(shí)間段分組查詢(xún),首先要了解level,connect
by,oracle時(shí)間的加減.
關(guān)于level這里不多說(shuō),我只寫(xiě)出一個(gè)查詢(xún)語(yǔ)句:
----level
是一個(gè)偽例
select
level
from
dual
connect
by
level
=10
---結(jié)果:
1
2
3
4
5
6
7
8
9
10
oracle時(shí)間的加減看看試一下以下sql語(yǔ)句就會(huì)知道:
select
sysdate
-1
from
dual
----結(jié)果減一天,也就24小時(shí)
select
sysdate-(1/2)
from
dual
-----結(jié)果減去半天,也就12小時(shí)
select
sysdate-(1/24)
from
dual
-----結(jié)果減去1
小時(shí)
select
sysdate-((1/24)/12)
from
dual
----結(jié)果減去5分鐘
select
sydate-(level-1)
from
dual
connect
by
level=10
---結(jié)果是10間隔1天的時(shí)間
下面是本次例子:
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_timedt
and
i.insert_time
d.dt-2
group
by
d.dt
例子中的sysdate
-
(level-1)
*
2得到的是一個(gè)間隔是2天的時(shí)間
group
by
d.dt
也就是兩天的時(shí)間間隔分組查詢(xún)
自己實(shí)現(xiàn)例子:
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
--從第一條時(shí)間記錄開(kāi)始(sysdate-13)為表中的最早的日期,“34”出現(xiàn)的分組數(shù)(一天按每六個(gè)小時(shí)分組
就應(yīng)該為4)
一下是按照每6個(gè)小時(shí)分組
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_timedt
and
i.created_time
d.dt-1/4
group
by
mobile_no,area_name,d.dt
另外一個(gè)方法:
--按六小時(shí)分組
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小時(shí)分組
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)
SQL select * from orderr;
PRODID ORDID ORDERDATA
---------- ---------- --------------
1 100 01-1月 -12
2 200 01-1月 -12
SQL select * from PRODUCT;
PRODID PROD
---------- ----
1 車(chē)
2 床
SQL select * from ORDERDETAIL;
PRODID PRICE COUNT ORDID
---------- ---------- ---------- ----------
1 50000 10 100
2 30000 15 200
SQL select "PRODUCT"."PRODNA" "產(chǎn)品名稱(chēng)","ORDERR"."ORDERDATA" "訂購(gòu)日期","ORDERDETAIL"."PRICE"*"ORDERDETAIL"."COUNT" "產(chǎn)品銷(xiāo)售額",
2 to_char("ORDERR"."ORDERDATA",'q') "訂購(gòu)季度"
3 from PRODUCT,ORDERDETAIL,ORDERR
4 where "PRODUCT"."PRODID"="ORDERDETAIL"."PRODID" AND "ORDERDETAIL"."ORDID"="ORDERR"."ORDID"
5 group by "ORDERR"."ORDERDATA","ORDERDETAIL"."PRICE"*"ORDERDETAIL"."COUNT",to_char("ORDERR"."ORDERDATA",'q'),"PRODUCT"."PRODNA";
產(chǎn)品 訂購(gòu)日期 產(chǎn)品銷(xiāo)售額 訂購(gòu)季度
---- -------------- ---------- ----------
床 01-1月 -12 450000 1
車(chē) 01-1月 -12 500000 1
有什么問(wèn)題?
另外說(shuō)一下:
1、你“產(chǎn)品名稱(chēng)”前面的雙引號(hào)有問(wèn)題。
2、在雙引號(hào)引用中是必須區(qū)分大小寫(xiě)的。
3、orderdata前的表名呢?
select distinct to_char(dt, 'yyyy-mm-dd') as years,to_char(dt, 'iw') as week
from (select (to_date('2013-4-15', 'yyyy-mm-dd') + rownum - 1) dtfrom dual connect by rownum = to_date('2013-7-15', 'yyyy-mm-dd') -to_date('2013-4-15', 'yyyy-mm-dd'))
group by to_char(dt, 'iw')
ORDER BY years,week
可以利用to_char函數(shù)將date類(lèi)型數(shù)據(jù)轉(zhuǎn)換為字符串,寫(xiě)法如下: select * from 表名 where to_char(issued_date, 'yyyy')='2008'~
一.表分區(qū)策略
1.識(shí)別大表
采用ANALYZE TABLE語(yǔ)句進(jìn)行分析,然后查詢(xún)數(shù)據(jù)字典獲得相應(yīng)的數(shù)據(jù)量。
2.大表如何分區(qū)
可根據(jù)月份,季度以及年份等進(jìn)行分區(qū);
3.分區(qū)的表空間規(guī)劃
要對(duì)每個(gè)表空間的大小進(jìn)行估計(jì)
二.創(chuàng)建表分區(qū)
a.創(chuàng)建范圍分區(qū)的關(guān)鍵字是'RANGE'
1.范圍分區(qū)
create table ware_retail_part --創(chuàng)建一個(gè)描述商品零售的數(shù)據(jù)表
(
id integer primary key,--銷(xiāo)售編號(hào)
retail_date date,--銷(xiāo)售日期
ware_name varchar2(50)--商品名稱(chēng)
)
partition by range(retail_date)
(
--2011年第一個(gè)季度為part_01分區(qū)
partition par_01 values less than(to_date('2011-04-01','yyyy-mm-dd')) tablespace TEMP01,
--2011年第二個(gè)季度為part_02分區(qū)
partition par_02 values less than(to_date('2011-07-01','yyyy-mm-dd')) tablespace TEMP01,
--2011年第三個(gè)季度為part_03分區(qū)
partition par_03 values less than(to_date('2011-10-01','yyyy-mm-dd')) tablespace TEMP01,
--2011年第四個(gè)季度為part_04分區(qū)
partition par_04 values less than(to_date('2012-01-01','yyyy-mm-dd')) tablespace TEMP01
);
2.創(chuàng)建散列分區(qū)
3.組合分區(qū):
4.interval 分區(qū)
三.創(chuàng)建索引分區(qū)
索引分區(qū)分為本地索引分區(qū)和全局索引分區(qū),全局索引不反應(yīng)基礎(chǔ)表的結(jié)構(gòu),要分區(qū)只能進(jìn)行范圍分區(qū)。
創(chuàng)建索引分區(qū)要參照表分區(qū)
四.分區(qū)技術(shù)簡(jiǎn)介
優(yōu)點(diǎn):
1.減少維護(hù)工作量
2.增強(qiáng)數(shù)據(jù)的可用性
3.均衡I/O,提升性能
4.提高查詢(xún)速度
5.分區(qū)對(duì)用戶(hù)保持透明,用戶(hù)感覺(jué)不到分區(qū)的存在。
五,管理表分區(qū)
1.添加表分區(qū)
ALTER TABLE...ALTER PARATITION
2.合并表分區(qū)
3.刪除分區(qū)
ALTER TABLE...DROP PARTITION
刪除分區(qū)時(shí),里面的數(shù)據(jù)也會(huì)被刪除。
-創(chuàng)建表和分區(qū)
create table sales--創(chuàng)建一個(gè)銷(xiāo)售記錄表
(
id number primary key,--記錄編號(hào)
goodsname varchar2(10),--商品名
saledate date--銷(xiāo)售日期
)
partition by range(saledate)--按照日期分區(qū)
(
--第一季度數(shù)據(jù)
partition part_sea1 values less than(to_date('2011-04-01','yyyy-mm-dd')) tablespace tbsp_1,
--第二季度數(shù)據(jù)
partition part_sea2 values less than(to_date('2011-07-01','yyyy-mm-dd')) tablespace tbsp_2,
--第三季度數(shù)據(jù)
partition part_sea3 values less than(to_date('2011-10-01','yyyy-mm-dd')) tablespace tbsp_1,
--第四季度數(shù)據(jù)
partition part_sea4 values less than(to_date('2012-01-01','yyyy-mm-dd')) tablespace tbsp_2
);
--創(chuàng)建局部索引
create index index_3_4 on sales(saledate)
local(
partition part_seal tablespace tbsp_1,
partition part_sea2 tablespace tbsp_2,
partition part_sea3 tablespace tbsp_1,
partition part_sea4 tablespace tbsp_2
);
--并入分區(qū)
alter table sales merge partitions part_sea3,part_sea4 into partition part_sea4;
--重建局部索引
alter table sales modify partition part_sea4 rebuild unusable local indexes;
六.管理索引分區(qū)
刪除索引:DROP PARTITION
重建分區(qū):REBUILT PARTITION
更名索引分區(qū):RENAME PARTITION
分割索引分區(qū):SPLIT PARTITION
通過(guò)配置本地?cái)?shù)據(jù)庫(kù)的tns配置文件實(shí)現(xiàn):
去oracle安裝目錄下oracle\product\10.2.0\db_2\NETWORK\ADMIN\ 找到tnsnames.ora,用記事本打開(kāi),里邊有遠(yuǎn)程數(shù)據(jù)庫(kù)的tns連接配置串如下
ORCL23 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.23)(PORT = 1521))
)
(CONNECT_DATA =
(SID = ORCL)
)
)
添加好ip、端口和目標(biāo)數(shù)據(jù)庫(kù)的實(shí)例名(SID),然后確保tns名稱(chēng)(ORCL23)在整個(gè)文檔中唯一,保存后打開(kāi)數(shù)據(jù)庫(kù)連接工具,輸入遠(yuǎn)程數(shù)據(jù)庫(kù)上的用戶(hù)名密碼,選擇數(shù)據(jù)庫(kù)對(duì)象為你配置的連接名就可以了
分享名稱(chēng):oracle怎么分季度,oracle判斷日期為第幾季度
分享URL:http://chinadenli.net/article35/dsicpsi.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶(hù)體驗(yàn)、建站公司、網(wǎng)站排名、App設(shè)計(jì)、微信小程序、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)