oracle統計每年每月資料,oracle 只根據年份(如2011)查詢每月統計資料

2022-03-15 04:49:28 字數 4844 閱讀 4202

1樓:匿名使用者

select year,

sum(case when month=1 then amount else 0 end) m1,

sum(case when month=2 then amount else 0 end) m2,

sum(case when month=3 then amount else 0 end) m3,

sum(case when month=4 then amount else 0 end) m4

from 表名

group by year

2樓:匿名使用者

第一步:建立表

--支出表

create table pay

(  id       number(11) not null,

pay_name varchar2(100),

author   varchar2(100),

type_id  number(11),

price    float, --金額

brief    varchar2(1000),

userid   number(11), --使用者id

times    date, --時間

primary key(id)

)第二步:插入資料

insert into pay (id, pay_name, author, type_id, price, brief, userid, times)

values (1, '1', '1', 0, 1, '1', 1, to_date('12-10-2012', 'dd-mm-yyyy'));

insert into pay (id, pay_name, author, type_id, price, brief, userid, times)

values (0, '3', '3', 1, 3, '3', 1, to_date('12-10-2012', 'dd-mm-yyyy'));

insert into pay (id, pay_name, author, type_id, price, brief, userid, times)

values (2, '2', '2', 1, 2, '2343444', 1, to_date('12-10-2012', 'dd-mm-yyyy'));

insert into pay (id, pay_name, author, type_id, price, brief, userid, times)

values (6, '6', '1', 0, 4000, '1', 1, to_date('12-08-2012', 'dd-mm-yyyy'));

insert into pay (id, pay_name, author, type_id, price, brief, userid, times)

values (5, '5', '1', 0, 1, '1', 1, to_date('12-10-2012', 'dd-mm-yyyy'));

insert into pay (id, pay_name, author, type_id, price, brief, userid, times)

values (4, '4', '1', 0, 100, '1', 1, to_date('12-09-2012', 'dd-mm-yyyy'));

insert into pay (id, pay_name, author, type_id, price, brief, userid, times)

values (7, '4', '1', 0, 566, '1', 1, to_date('03-03-2012', 'dd-mm-yyyy'));

insert into pay (id, pay_name, author, type_id, price, brief, userid, times)

values (11, '4', '1', 0, 566, '1', 1, to_date('03-04-2012', 'dd-mm-yyyy'));

insert into pay (id, pay_name, author, type_id, price, brief, userid, times)

values (10, '4', '1', 0, 566, '1', 1, to_date('03-05-2012', 'dd-mm-yyyy'));

insert into pay (id, pay_name, author, type_id, price, brief, userid, times)

values (9, '4', '1', 0, 566, '1', 1, to_date('03-06-2012', 'dd-mm-yyyy'));

insert into pay (id, pay_name, author, type_id, price, brief, userid, times)

values (8, '4', '1', 0, 566, '1', 1, to_date('03-07-2012', 'dd-mm-yyyy'));

commit;

第三步:執行分組查詢

select sum(decode(extract(month from b.times), 1, b.price, 0)) as a,

sum(decode(extract(month from b.times), 2, b.price, 0)) as b,

sum(decode(extract(month from b.times), 3, b.price, 0)) as c,

sum(decode(extract(month from b.times), 4, b.price, 0)) as d,

sum(decode(extract(month from b.times), 5, b.price, 0)) as e,

sum(decode(extract(month from b.times), 6, b.price, 0)) as f,

sum(decode(extract(month from b.times), 7, b.price, 0)) as g,

sum(decode(extract(month from b.times), 8, b.price, 0)) as h,

sum(decode(extract(month from b.times), 9, b.price, 0)) as i,

sum(decode(extract(month from b.times), 10, b.price, 0)) as j,

sum(decode(extract(month from b.times), 11, b.price, 0)) as k,

sum(decode(extract(month from b.times), 12, b.price, 0)) as l

from pay b

where userid = 1

and extract(year from b.times) = '2012'

第四步:輸出結果

3樓:匿名使用者

select year, sum(m1) m1, sum(m2) m2, sum(m3) m3, sum(m4) m4

from (select year,

decode(month, 1, amount, 0) m1,decode(month, 2, amount, 0) m2,decode(month, 3, amount, 0) m3,decode(month, 4, amount, 0) m4from table_name) a

group by year

order by year

-- 1. 首先使用decode函式進行行轉列-- 2. 再按年進行分組,統計每個月份的值有問題請追問,希望可以幫到你

oracle 只根據年份(如2011)查詢每月統計資料

4樓:98稻草人

不知道你的時間和引數time分別是什麼型別的,只能暫時考慮為vchar型的。

select

to_char(時間,'yyyy-mm') as 每月時間

, sum(統計資料)

from 測試表

where substr(to_char(to_date('2011/7','yyyy/mm'),'yyyy/mm/dd'),0,4) = substr(to_char(to_timestamp('2011/7/1 18:42:43','yyyy/mm/dd hh24:

mi:ss'),'yyyy/mm/dd hh24:mi:

ss'),0,4)

group by to_char(時間,'yyyy-mm')

-----下面的這個假設時間是date型的,引數time是timestamp型

select

to_char(時間,'yyyy-mm') as 每月時間

, sum(統計資料)

from 測試表

where substr(to_char(時間,'yyyy-mm'),0,4) = substr(to_char(time,'yyyy/mm/dd hh24:mi:ss'),0,4)

group by to_char(時間,'yyyy-mm')

5樓:匿名使用者

select to_char(時間,'yyyy-mm') as 每月時間, sum(統計資料) from 測試表 where to_char(時間),'yyyy-mm-dd')like '2011%' group by to_char(時間,'yyyy-mm')

6樓:匿名使用者

where trunc( 時間, 'yyyy' ) = trunc( time , 'yyyy' )

家庭每月開銷統計表,看家庭開支統計表的感想

星期三的遲到事件在我的人生長河中記下了重重的一記敗筆 現在的我無比的痛心疾首,追悔莫及!開學以來我曾因為遲到被寫兩次檢討,這是第三次了.在老師語重心長的諄諄教導下,我雖然認識到自己的錯誤,但是抱著僥倖心理,以為能瞞天過海繼續犯錯。星期三那天我再一次遲到,可是被老師發現了.守時守信是中華民族的傳統美德...

oracle中統計某個欄位重複數並將重複條目的另

方法和詳細的 ai作du步驟如下 1 第一zhi步,開啟一個資料中包dao 含整數值的回表,然後可以在右下答角看到該表資料,見下圖,轉到下面的步驟。2 第二步,執行完上面的操作之後,自動對錶中的資料進行 1,見下圖,轉到下面的步驟。3 第三步,執行完上面的操作之後,選擇 update biao1 s...

農村每年交500元社保現在60歲了每月能領多少養

城鄉居民養老保險的待遇標準 一生交的錢 利息 補貼 139 75 退休後的月領待遇標準。其中 補貼各省可能不一樣。75元是國家撥付的,有的省自己提高了。退休養老金公式 社平工資 20 個人帳戶 1 120之和,顯然跟社平和個人交納金額有直接關係。領不到錢。要想領到養老金必須滿足二個條件 1 年齡達到...