--建立用於測試的表及資料
create table t603
( code varchar2(10),
year varchar2(10),
month varchar2(10),
p_id varchar2(7),
v1 number(10),
v2 number(10)
);comment on column t603.code is '工廠';
comment on column t603.year is '年份';
comment on column t603.month is '月份';
comment on column t603.p_id is '產品';
comment on column t603.v1 is '生產量';
comment on column t603.v2 is '銷售量';
insert into t603 values('600001',2012,11,'30',1,1);
insert into t603 values('600001',2012,11,'31',1,1);
insert into t603 values('600001',2012,11,'10',2,2);
insert into t603 values('600002',2012,12,'10',3,2);
insert into t603 values('600002',2012,12,'31',2,1);
insert into t603 values('600002',2012,12,'30',2,1);
select * from t603;
--按自定義的規則聚合
select code,
p_id,
v1 from t603
where code in ('600001', '600002')
model return updated rows --model後面的return updated rows子句將結果限制為在該查詢中建立或更新的那些行,使結果集只包含新計算的值。
partition by(code) --分割槽
dimension by(p_id) --維度
measures(v1) --度量
rules (v1['err1'] = v1['30'] + v1['31'] - v1['10']) --計算規則
order by code, p_id;
輸出 codep_idv1
1600001err10
2600002err11
--按多個規則聚合,返回包括非聚合行
select code,
year,
p_id,
month,
v1from t603
where year = '2012'
model
partition by (code,year)
dimension by (p_id,month)
measures (v1,v2)
rules (
v1['err1',11] = v1['30',11] + v1['31',11] - v1['10',11],
v2['err1',12] = v2['30',12] + v2['31',12] - v2['10',12])
order by code, p_id;
輸出 codeyearp_idmonthv1
46000012012err112
56000012012err1110
96000022012err112
106000022012err111
--指定要聚合的維值
select code,
p_id,
month,
v1from t603
where year = '2012'
model return updated rows
partition by (code)
dimension by (p_id,month)
measures (v1)
rules (
v1['err1',for month in(11,12)] = v1['30',cv(month)] + v1['31',cv(month)] -v1['10',cv(month)])
order by code, p_id, month;
輸出 codep_idmonthv1
1600001err1110
2600001err112
3600002err111
4600002err1121
--cv()函式獲取多行統計記錄的當前值,可以使用表示式進行計算
--為數值型可用語法:for month from 1 to 12 increment 1
--還可以使用子查詢:for year in (select year from t603)
Oracle模式SQL基礎知識
建立用於測試的表及資料 create table t603 code varchar2 10 year varchar2 10 month varchar2 10 p id varchar2 7 v1 number 10 v2 number 10 comment on column t603.cod...
Oracle基礎知識 SQL簡單命令
sql語句包括兩個部分 1 ddl 資料定義語言 2 dml 資料控制語言 ddl create 建立乙個表 create table b clob char 1 alter 增加已經定義的表列的分配 drop 刪除乙個表 desc 檢視乙個表的定義 dml selelct select from ...
SQL基礎知識
本篇文章是講解sql的基礎知識,但也講得不全面,我只記錄了自己不懂的或者不熟悉的東西。一 在sql中簡單的查詢 1.重複的記錄 distinct 可以通過在選擇列表前的select語句中插入關鍵字distinct來消除重複的查詢結果記錄。比如 select distinct city from ci...