使用場景:統計支出表中某人某年的每個月的支出情況
1.建立表如下(oracle)
--支出表
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)
)2.插入測試資料
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;
3.統計sql(查詢userid為1的使用者2023年的每個月支出情況)
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'
4.統計結果如下
a b c d e f g h i j k l
0 0 566 566 566 566 566 4000 100 7 0 0
5.另外一種寫法:對於時間的操作也可以用下面這個
select sum(decode(to_char(b.times, 'mm'), '01', b.price, 0)) m1,
sum(decode(to_char(b.times, 'mm'), '02', b.price, 0)) m2,
sum(decode(to_char(b.times, 'mm'), '03', b.price, 0)) m3,
sum(decode(to_char(b.times, 'mm'), '04', b.price, 0)) m4,
sum(decode(to_char(b.times, 'mm'), '05', b.price, 0)) m5,
sum(decode(to_char(b.times, 'mm'), '06', b.price, 0)) m6,
sum(decode(to_char(b.times, 'mm'), '07', b.price, 0)) m7,
sum(decode(to_char(b.times, 'mm'), '08', b.price, 0)) m8,
sum(decode(to_char(b.times, 'mm'), '09', b.price, 0)) m9,
sum(decode(to_char(b.times, 'mm'), '10', b.price, 0)) m10,
sum(decode(to_char(b.times, 'mm'), '11', b.price, 0)) m11,
sum(decode(to_char(b.times, 'mm'), '12', b.price, 0)) m12
from pay b
where userid = 1
and to_char(b.times, 'yyyy') = '2012'
統計每個月兔子的總數
有乙隻兔子,從出生後第3個月起每個月都生乙隻兔子,小兔子長到第三個月後每個月又生乙隻兔子,假如兔子都不死,問每個月的兔子總數為多少?這個問題可能我比較笨,看大多數解釋都是一句話,f n f n 1 f n 2 但是總有點想不明白這個。列了個 才看清楚咋回事。月份1 2345 67兔子總數11 235...
mysql按月獲取一年內每個月的資料量
select count id as num,date format create time,y m as createtime from table where create time 2016 01 01 00 00 00 and create time 2017 01 01 00 00 00 ...
統計每個月兔子的個數 C
題目描述 開始有乙個兔子,然後這個兔子從三個月後開始生小兔子,小兔子也是三個月之後就可以生,兔子不會死,要求輸入月份,輸出這個月的兔子數量。解題思路 由題得,兔子分為三種,乙個月大小的兔子n1 兩個月大小的兔子n2 三個月及三個月以上大小的兔子n3,n3 n2,n2 n1,n1 n3.解釋一下就是因...