日常统计代码

-------------------------------国网充电记录(RECHARGER_REPORT )------------------------------
                                                                                                      
1.统计国网各月份各地市的电量
select to_char(t.ending_time,'yyyy-mm')  the_month,t.trade_company,sum(t.trade_electricity) the_elec from RECHARGER_REPORT t 
group by t.trade_company,to_char(t.ending_time,'yyyy-mm')
order by the_month,t.trade_company;

2.国网城市公用桩各时间段充电量
select t.trade_timegap,sum(t.trade_electricity) te, count(t.trade_electricity) cnt  from RECHARGER_REPORT t where t.special_or_public='公用' and t.high_speed='城市' 
and t.ending_time>=to_date('2017-01-01','yyyy-mm-dd') and t.ending_time<=to_date('2018-01-01','yyyy-mm-dd')
group by t.trade_timegap
order by t.trade_timegap  ,te desc;

3.国网各充电站充电量和充电次数(包括没有充电记录的充电站)
create table tmp as
select t.station_name,t.special_or_public,t.high_speed,sum(t.trade_electricity) total ,count(t.trade_electricity) cnt from RECHARGER_REPORT t 
where t.ending_time>= to_date('2017-01-01','yyyy-mm-dd') and  t.ending_time<= to_date('2018-01-01','yyyy-mm-dd') 
group by t.station_name,t.special_or_public,t.high_speed
order by total desc


insert into tmp(station_name,special_or_public,high_speed) 
select distinct p.station_name,p.special_or_public,p.high_speed from piles p where not exists (select 1 from tmp t where t.station_name= p.station_name)

4.国网各个站充电量(包括每个站充电桩数量)
create table tmp_yys as 
select t.station_name,count(t.pile_num) cnt, t.high_speed,t.special_or_public from piles t where 1=1
group by t.station_name, t.high_speed,t.special_or_public;

select * from tmp_yys;

create table tmp_yys_hs as 
select t.station_name ,t.high_speed,t.special_or_public, sum(t.TRADE_ELECTRICITY) sume ,YYS.cnt,sum(t.TRADE_ELECTRICITY)/YYS.cnt sumep
  from RECHARGER_REPORT t,tmp_yys YYS where t.ending_time>=to_date('2017-01-01','yyyy-mm-dd') and t.ending_time<=to_date('2018-01-01','yyyy-mm-dd') 
--and t.high_speed='高速' and t.special_or_public='公用' 
AND t.station_name=yys.station_name
group by  t.station_name ,t.high_speed,t.special_or_public,YYS.cnt
order by  t.station_name ,t.high_speed,t.special_or_public,YYS.cnt;

insert into tmp_yys_hs(station_name,cnt,high_speed,special_or_public)

5.统计国网充电桩谷电量
select t.special_or_public,t.high_speed, sum(t.trade_electricity) v1 from RECHARGER_REPORT t where t.trade_timegap<=8 and t.ending_time<=to_date('2018-01-01','yyyy-mm-dd') 
group by t.special_or_public,t.high_speed order by t.special_or_public,t.high_speed;
select t.special_or_public,t.high_speed, sum(t.trade_electricity) v2 from RECHARGER_REPORT t where t.trade_timegap>=22 and t.ending_time<=to_date('2018-01-01','yyyy-mm-dd')
group by t.special_or_public,t.high_speed order by t.special_or_public,t.high_speed;
select  t.special_or_public,t.high_speed,sum(t.trade_electricity) from RECHARGER_REPORT t where t.ending_time<=to_date('2018-01-01','yyyy-mm-dd')
group by t.special_or_public,t.high_speed order by t.special_or_public,t.high_speed;
select t.special_or_public,t.high_speed,sum(t.trade_electricity) total,valley1.v1,valley2.v2 from RECHARGER_REPORT t,
(select t.special_or_public,t.high_speed, sum(t.trade_electricity) v1 from RECHARGER_REPORT t where t.trade_timegap<=8 and t.ending_time<=to_date('2018-01-01','yyyy-mm-dd') 
group by t.special_or_public,t.high_speed order by t.special_or_public,t.high_speed ) valley1
,(select t.special_or_public,t.high_speed, sum(t.trade_electricity) v2 from RECHARGER_REPORT t where t.trade_timegap>=22 and t.ending_time<=to_date('2018-01-01','yyyy-mm-dd')
group by t.special_or_public,t.high_speed order by t.special_or_public,t.high_speed)valley2 
where valley1.special_or_public=valley2.special_or_public and valley1.high_speed=valley2.high_speed and t.special_or_public=valley2.special_or_public and t.high_speed=valley2.high_speed 
and t.ending_time<=to_date('2018-01-01','yyyy-mm-dd')
group by t.special_or_public,t.high_speed,valley1.v1,valley2.v2

6.清分结算
select * from RECHARGER_REPORT t where t.ending_time>=to_date('2017-12-21','yyyy-mm-dd') and t.ending_time<=to_date('2018-01-21','yyyy-mm-dd') order by t.ending_time desc;
select t.trade_company,sum(t.trade_electricity),sum(t.trade_money) from RECHARGER_REPORT t
 where t.ending_time>=to_date('2017-12-21','yyyy-mm-dd') and t.ending_time<=to_date('2018-01-21','yyyy-mm-dd')
 group by t.trade_company  order by t.trade_company;

7.国网示范站点充电量统计
select rt.station_name,sum(rt.trade_electricity),s.pile_count,sum(rt.trade_electricity)/s.pile_count from recharger_report rt,SEMPLE_STATION s where rt.station_name=s.station_name 
and rt.ending_time >to_date('2017-11-01','yyyy-mm-dd') and  rt.ending_time group by rt.station_name,s.pile_count;

8.查询国网充电桩某段日期的记录(三种方法)
select * from RECHARGER_REPORT t where trunc(t.ending_time)=to_date('2017-11-20','yyyy-mm-dd')----不能用于索引
order by to_date('2017-11-20','yyyy-mm-dd');
select count(*)from RECHARGER_REPORT t;

select * from RECHARGER_REPORT t where t.ending_time between to_date('2017-11-20','yyyy-mm-dd') and  to_date('2017-11-20 23:59:59','yyyy-mm-dd hh24:mi:ss')
order by to_date('2017-11-20','yyyy-mm-dd');
select count(*)from RECHARGER_REPORT t;

select * from RECHARGER_REPORT t where t.ending_time >= to_date('2017-11-20','yyyy-mm-dd') and  t.ending_time < to_date('2017-11-21','yyyy-mm-dd')
order by to_date('2017-11-20','yyyy-mm-dd');
select count(*)from RECHARGER_REPORT t;

9.统计各个厂家故障率
select num1,num2,num1/num2,m1.manufactruer from (
select count(1) num1,t.manufactruer from RECHARGER_REPORT t  where t.ending_reason like '%故障' and t.trade_time >to_date('2017-10-01','yyyy-mm-dd')  
group by t.manufactruer order by t.manufactruer) m1,
(select count(1) num2 ,t.manufactruer from RECHARGER_REPORT t  where t.trade_time >to_date('2017-10-01','yyyy-mm-dd')  
group by t.manufactruer order by t.manufactruer)m2 where m1.manufactruer=m2.manufactruer order by m1.manufactruer

10.更改高速名称
select p.* from piles p where p.station_name  like '%沪昆高速嘉兴服务区%' 
update piles rt set rt.station_name='沪昆高速嘉兴服务区快充站(昆明方向)' where rt.station_name like '%沪昆高速嘉兴服务区%' and rt.station_name like '%昆明方向%'
select * from RECHARGER_REPORT rt where rt.station_name like '%沪昆高速嘉兴服务区%' and rt.station_name like '%上海方向%'

11.国网公交站日充电量、站规模、利用率
select t.station_nameyys 国网公交充电站,sum(t.trade_electricity)/2 日充电量,p.cnt 站规模,(sum(t.trade_electricity)/2)/(p.summer)*24 利用率 from RECHARGER_REPORT_TMP t,
(select t.statiom_nameyys ,count(t.pile_num) cnt,sum(t.max_power) summer from piles t where t.high_speed like '%城市%' and t.special_or_public like '%专用%'
group by t.statiom_nameyys
order by t.statiom_nameyys desc ) p
where t.ending_time>=to_date('2018-01-26','yyyy-mm-dd') and t.ending_time<=to_date('2018-01-28','yyyy-mm-dd') 
and t.high_speed like '%城市%' and t.special_or_public like '%专用%' and t.station_nameyys=p.statiom_nameyys
group by t.station_nameyys,p.cnt,p.summer
order by t.station_nameyys desc 


12.示范站点站规模、总功率、利用率

create table sfzd as
select t.station_nameyys,count(t.pile_num) size_station,sum(t.max_power) total_power from PILES t
where t.station_nameyys like '%空港大道%' or t.station_nameyys like '%香樟街%' or t.station_nameyys like '%余杭人民大道%' or t.station_nameyys like '%九堡客运%'
 or t.station_nameyys like '%缤纷小区%' or t.station_nameyys like '%宝盛世纪%'or t.station_nameyys like '%洪合毛衫%'or t.station_nameyys like '%南浔古镇%'or t.station_nameyys like '%越王路%'
 or t.station_nameyys like '%石碶%'or t.station_nameyys like '%新城公交%'or t.station_nameyys like '%环城西路%' or t.station_nameyys like '%东关%'or t.station_nameyys like '%新城充电站%'
 or t.station_nameyys like '%高湾%'or t.station_nameyys like '%水阁街道沙溪%'or t.station_nameyys like '%紫金南路%'or t.station_nameyys like '%丽水市客运西站公交%'
 or t.station_nameyys like '%红星街道%'or t.station_nameyys like '%浮石公交%' 
 group by t.station_nameyys
 create table rec1 as
 select t.station_nameyys,sum(t.trade_electricity) smu from RECHARGER_REPORT t
where t.ending_time>=to_date('2017-11-01','yyyy-mm-dd') and t.ending_time<=to_date('2017-12-01','yyyy-mm-dd') group by t.station_nameyys order by smu desc

update sfzd t set t.SUMM=(select r.smu from rec1 r where t.station_nameyys=r.station_nameyys)

create table model_station3 as 
select t.station_nameyys 示范站点,t.size_station 站规模,t.total_power 站总功率,t.summ 总充电量,t.summ/(t.total_power*24*30) 利用率 from sfzd t 
group by t.station_nameyys,t.size_station,t.summ,t.total_power 
order by t.summ






----------------------------营销系统充电记录(SOCIAL_CHARGE_DATA )---------------------------

1.统计社会各类型充电量
create table report_all2 as 
select t.city ,sum(t.charging) type_resident,t.month from SOCIAL_CHARGE_DATA t where t.station_attribute='社会报装' and t.station_type='居民'
group by t.city,t.month order by t.month,t.city

update report_all2 a set a.type_bus=(select s.sumCharge from (
select t.city,t.station_type,sum(t.charging) sumCharge,t.month from SOCIAL_CHARGE_DATA t where t.station_attribute='社会报装' and t.station_type='公交站'
group by t.city,t.station_type,t.month order by t.month,t.city) s where s.city=a.city and a.month=s.month)

update report_all2 a set a.type_public=(select s.sumCharge from (
select t.city,t.station_type,sum(t.charging) sumCharge,t.month from SOCIAL_CHARGE_DATA t where t.station_attribute='社会报装' and t.station_type='公共站'
group by t.city,t.station_type,t.month order by t.month,t.city) s where s.city=a.city and a.month=s.month)

update REPORT_ALL2 t set t.type_bus='0' where  t.type_bus is null;
update REPORT_ALL2 t set t.type_public='0' where  t.type_public is null;
select * from   REPORT_ALL2 t order by t.month ,t.city;

2.计算各种类型充电站单月社会充电量
select  m1.station_type,mun1,mun2,mun2-mun1 from (
select t.station_type,sum(t.charging) mun1,t.month from SOCIAL_CHARGE_DATA t where t.station_attribute='社会报装'and t.month='10月'
group by t.station_type,t.month order by t.month) m1,
(select t.station_type,sum(t.charging) mun2,t.month from SOCIAL_CHARGE_DATA t where t.station_attribute='社会报装'and t.month='11月'
group by t.station_type,t.month order by t.month) m2 where m1.station_type=m2.station_type order by m1.station_type

3.计算各地市单月社会充电量
select  m1.city,mun1,mun2,mun2-mun1 from (
select t.city,sum(t.charging) mun1,t.month from SOCIAL_CHARGE_DATA t where t.station_attribute='社会报装'and t.month='9月'
group by t.city,t.month order by t.month) m1,
(select t.city,sum(t.charging) mun2,t.month from SOCIAL_CHARGE_DATA t where t.station_attribute='社会报装'and t.month='10月'
group by t.city,t.month order by t.month) m2 where m1.city=m2.city order by m1.city



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