月报SQL代码

一、建表统计社会充电量(原充电记录为当年累计充电量)

1.计算1月份社会充电量

create table SOCIAL_CHARGE_DATA_month(city,month,station_type,charging) as

select t.city,'2017-01',t.station_type,(sum(t.charging)/59)*31 from SOCIAL_CHARGE_DATA t 

where t.station_attribute='社会报装' and t.month='2017-02' 

group by t.city,t.station_type order by city,station_type;

2.计算2月份社会充电量
insert into SOCIAL_CHARGE_DATA_month(city,month,station_type,charging)

select t.city,'2017-02',t.station_type,(sum(t.charging)/59)*28 from SOCIAL_CHARGE_DATA t 

where t.station_attribute='社会报装' and t.month='2017-02' group by t.city,t.station_type

order by city,station_type;


3.计算2017年3—12月社会充电量

insert into SOCIAL_CHARGE_DATA_month(city,month,station_type,charging)
select s.city,s.month,s.station_type,s.charging-e.charging as charging from (

select t.city,t.month,t.station_type,sum(t.charging) charging from  SOCIAL_CHARGE_DATA t 

where t.station_attribute='社会报装' group by t.city,t.month,t.station_type) s,

(select t.city,t.month,t.station_type,sum(t.charging) charging from  SOCIAL_CHARGE_DATA t 

where t.station_attribute='社会报装' group by t.city,t.month,t.station_type) e

where s.city=e.city and add_months(to_date(s.month,'yyyy-mm'),-1)= to_date(e.month,'yyyy-mm') 

and s.station_type=e.station_type

order by s.month,s.city,s.station_type;


4.删除2018年1月的计算数据

delete from SOCIAL_CHARGE_DATA_MONTH t where t.month like '%2018-01%' and t.charging<0

5.计算2018年1月份社会充电量
insert into SOCIAL_CHARGE_DATA_month(city,month,station_type,charging)
select t.city,'2018-01',t.station_type,sum(t.charging) from SOCIAL_CHARGE_DATA t where t.station_attribute='社会报装' and t.month='2018-01' group by t.city,t.station_type

order by city,station_type;

6.将金华地市上报数据填入表SOCIAL_CHARGE_DATA_month中

二、建表统计国网充电量、充电次数、用户数等

1.计算国网各地市各月份充电量
create table monthly_city as
select substr(t.trade_company,3,2) city,to_char(t.ending_time,'yyyy-mm') month,t.special_or_public ,t.high_speed ,
round(sum(t.trade_electricity),0)charging,count(t.trade_electricity) TIMES,round(sum(t.trade_electricity)/count(t.trade_electricity),0)per_charging,

count(distinct t.card_id)  USERNUMBER,

round(sum(t.trade_electricity)/ count(distinct t.card_id)) PER_USERCHARGING

from RECHARGER_REPORT t
where t.ending_time>=to_date('2017-01','yyyy-mm')
group by t.trade_company,to_char(t.ending_time,'yyyy-mm'),t.special_or_public,t.high_speed

order by to_char(t.ending_time,'yyyy-mm'),t.trade_company,t.special_or_public,t.high_speed;


2.创建字段station_type,并将石膏高速和是否公用合并
select * from monthly_city
update monthly_city t set t.station_type=concat(t.是否高速,t.是否公用)

三、建表将将国网和社会统计数据汇总

1.创建新表mon_ele_analtsis

create table MON_ELE_ANALTSIS
(
  city             VARCHAR2(50),
  month            VARCHAR2(50),
  station_type     VARCHAR2(50),
  charging         NUMBER,
  charg_times      NUMBER,
  per_charging     NUMBER,
  usernumber       NUMBER,
  per_usercharging NUMBER
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited

  );


2.EXCEL插入CITY,MONTH,station_type数据

3.将国网充电数据放入表MON_ELE_ANALTSIS中

update MON_ELE_ANALTSIS t set (t.charging,t.charg_times,t.per_charging,t.usernumber,t.per_usercharging)=
(select c.国网充电量,c.国网充电次数,c.国网平均单次充电量,c.国网用户数,c.国网平均单用户充电量 from monthly_city c 

where t.city=c.city and t.month=c.month and t.station_type=c.station_type);

3.将社会充电数据放入表MON_ELE_ANALTSIS中

update MON_ELE_ANALTSIS t set t.charging=(select s.charging from SOCIAL_CHARGE_DATA_MONTH s 
where t.city=s.city and t.month=s.month and t.station_type=s.station_type )

where t.station_type like '%居民%' or t.station_type like '%公交站%' or t.station_type like '%公共站%' ;

4.将表中所有空值用0填充

你可能感兴趣的:(月报SQL代码)