去年写的比较长的SQL,虽然现在不怎么写了。发在博客上面纪念一下。
计算公交驾车分担率,逻辑如下
早上6点到晚上9点早高峰,晚上16:30-19:30晚高峰,其余时间平峰。
根据OD五分钟的公交的出行人数(人数是之前表中算好的),按照高峰类型,是否工作日,某个区域公交出行的人数。某个区域公交人数除以总人数就是公交的分担率。
drop table if exists bus_qy_fdl_jc;
CREATE TABLE IF NOT EXISTS bus_qy_fdl_jc (
rq STRING,
lx STRING,
sjd STRING,
qyid STRING,
qylx STRING,
fdl DOUBLE
);
insert overwrite table bus_qy_fdl_jc
select rq,lx,sjd,bh as qyid,qylx,fdl from (
select a.rq,a.lx,a.sjd,a.bh,a.qylx,round(b.jcrs/a.zrs,4)*100 as fdl from (
select rq,lx,sjd,bh,qylx,round(avg(rs),3) as zrs from (
select substr(rq,1,6) as rq,lx,sjd,bh,qylx,sum(rs) as rs from (
select rq,size as qylx,bh,rs,
case when weekday(to_date(concat(rq,sjd,"00"),"yyyymmddhhmiss")) in (5,6) then "非工作日"
else "工作日"
end as lx,
case when (substr(sjd,1,2)>=6 and substr(sjd,1,2)<9) then "早高峰"
when (sjd>=1630 and sjd<1930) then "晚高峰"
else "平峰"
end as sjd
from csdnsz_mda_prod.qy_od_5min ) a group by rq,lx,sjd,bh,qylx ) a2 group by rq,lx,sjd,bh,qylx
) a join
(
select rq,lx,sjd,bh,qylx,round(avg(rs),3) as jcrs from (
select substr(rq,1,6) as rq,lx,sjd,bh,qylx,sum(rs) as rs from (
select rq,size as qylx,bh,rs,
case when weekday(to_date(concat(rq,sjd,"00"),"yyyymmddhhmiss")) in (5,6) then "非工作日"
else "工作日"
end as lx,
case when (substr(sjd,1,2)>=6 and substr(sjd,1,2)<9) then "早高峰"
when (sjd>=1630 and sjd<1930) then "晚高峰"
else "平峰"
end as sjd
from csdnsz_mda_prod.qy_cll_5min ) a group by rq,lx,sjd,bh,qylx ) a1 group by rq,lx,sjd,bh,qylx
) b
on a.rq=b.rq and a.lx=b.lx and a.sjd =b.sjd and a.bh=b.bh and a.qylx=b.qylx
union all
select a.rq,a.lx,a.sjd,a.bh,a.qylx,round(b.jcrs/a.zrs,4)*100 as fdl from (
select rq,lx,sjd,bh,qylx,round(avg(rs),3) as zrs from (
select substr(rq,1,6) as rq,lx,sjd,bh,qylx,sum(rs) as rs from (
select rq,size as qylx,bh,rs,
case when weekday(to_date(concat(rq,sjd,"00"),"yyyymmddhhmiss")) in (5,6) then "非工作日"
else "工作日"
end as lx,
"全天" as sjd
from csdnsz_mda_prod.qy_od_5min ) a group by rq,lx,sjd,bh,qylx ) c group by rq,lx,sjd,bh,qylx) a join
(
select rq,lx,sjd,bh,qylx,round(avg(rs),3) as jcrs from (
select substr(rq,1,6) as rq,lx,sjd,bh,qylx,sum(rs) as rs from (
select rq,size as qylx,bh,rs,
case when weekday(to_date(concat(rq,sjd,"00"),"yyyymmddhhmiss")) in (5,6) then "非工作日"
else "工作日"
end as lx,
"全天" as sjd
from csdnsz_mda_prod.qy_cll_5min ) a group by rq,lx,sjd,bh,qylx ) a3 group by rq,lx,sjd,bh,qylx
) b
on a.rq=b.rq and a.lx=b.lx and a.sjd =b.sjd and a.bh=b.bh and a.qylx=b.qylx ) t;