oracle 分析不同月份數據

 
創建銷售表:店鋪銷售表s1電商銷售表s2

create table s1 (id int, cp char(10), jg int, time date);

create table s2 (id int, cp char(10), jg int, time date);

commit;

表中添加數據

S1中數據

insert into s1 values (1,'shouji',2800,to_date('2012-12-12 12:12:12','yyyy-mm-dd hh24:mi:ss'));

insert into s1 values (2,'dianshi',15800,to_date('2012-12-10 12:12:12','yyyy-mm-dd hh24:mi:ss'));

insert into s1 values (3,'shouji',2800,to_date('2012-12-8 12:12:12','yyyy-mm-dd hh24:mi:ss'));

insert into s1 values (4,'bingxiang',8800,to_date('2012-11-12 12:12:12','yyyy-mm-dd hh24:mi:ss'));

insert into s1 values (5,'kongtiao',12800,to_date('2012-12-3 12:12:12','yyyy-mm-dd hh24:mi:ss'));

insert into s1 values (6,'dianshi',15800,to_date('2012-11-11 12:12:12','yyyy-mm-dd hh24:mi:ss'));

insert into s1 values (7,'shouji',2800,to_date('2012-11-10 12:12:12','yyyy-mm-dd hh24:mi:ss'));

insert into s1 values (8,'kongtiao',12800,to_date('2012-11-3 12:12:12','yyyy-mm-dd hh24:mi:ss'));

S2中數據

insert into s2 values (1,'shouji',2600,to_date('2012-12-12 12:12:12','yyyy-mm-dd hh24:mi:ss'));

insert into s2 values (2,'dianshi',15200,to_date('2012-12-10 12:12:12','yyyy-mm-dd hh24:mi:ss'));

insert into s2 values (3,'shouji',2600,to_date('2012-12-8 12:12:12','yyyy-mm-dd hh24:mi:ss'));

insert into s2 values (4,'bingxiang',8000,to_date('2012-11-12 12:12:12','yyyy-mm-dd hh24:mi:ss'));

insert into s2 values (6,'dianshi',15200,to_date('2012-11-11 12:12:12','yyyy-mm-dd hh24:mi:ss'));

insert into s2 values (7,'shouji',2600,to_date('2012-11-10 12:12:12','yyyy-mm-dd hh24:mi:ss'));

commit;

 

查看錶中數據

select * from s1;

select * from s2;

 
分別統計數據表s1s2 11月和12月份的銷售額

 

select sum(jg) as heji12s1 from s1 where substr(to_char(time,'yyyy-mm-dd'),1,7)='2012-12' ;

select sum(jg) as heji12s2 from s2 where substr(to_char(time,'yyyy-mm-dd'),1,7)='2012-12';

select sum(jg) as heji11s1 from s1 where substr(to_char(time,'yyyy-mm-dd'),1,7)='2012-11';

select sum(jg) as heji11s2 from s2 where substr(to_char(time,'yyyy-mm-dd'),1,7)='2012-11';

 
11月和12月銷售額分別求和

 
select (select sum(jg) as heji12s1 from s1 where substr(to_char(time,'yyyy-mm-dd'),1,7)='2012-12')

+(select sum(jg) as heji12s2 from s2 where substr(to_char(time,'yyyy-mm-dd'),1,7)='2012-12') as heji12

from dual;

select (select sum(jg) as heji11s1 from s1 where substr(to_char(time,'yyyy-mm-dd'),1,7)='2012-11')

+(select sum(jg) as heji11s2 from s2 where substr(to_char(time,'yyyy-mm-dd'),1,7)='2012-11')

as heji11 from dual;

 
對比12月和11月銷售額

 

select ((select sum(jg) as heji12s1 from s1 where substr(to_char(time,'yyyy-mm-dd'),1,7)='2012-12')

+(select sum(jg) as heji12s2 from s2 where substr(to_char(time,'yyyy-mm-dd'),1,7)='2012-12') )

- ((select sum(jg) as heji11s1 from s1 where substr(to_char(time,'yyyy-mm-dd'),1,7)='2012-11')

+(select sum(jg) as heji11s2 from s2 where substr(to_char(time,'yyyy-mm-dd'),1,7)='2012-11')) 

 as xiaoshoucha from dual;

總結:
11月份銷售總額爲:66000元
12月份銷售總額爲:54600元
12月份銷售量有所下降,與11月份對比,銷售額下降了11400元。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章