设为首页 收藏本站
查看: 826|回复: 0

[经验分享] oracle case when 复杂统计SQL

[复制链接]
YunVN网友  发表于 2016-8-13 06:10:45 |阅读模式
  select substr(unitcode,0,6),
count(*),
--01--
sum(case substr(jqxzcode,0,4) when '0105' then 1 else 0 end) as sharen,
sum(case substr(jqxzcode,0,4) when '0109' then 1 else 0 end) as qiangjie,
sum(case substr(jqxzcode,0,4) when '0112' then 1 else 0 end) as qiangduo,
sum(case substr(jqxzcode,0,4) when '0110' then 1 else 0 end) as daoqie,
sum(case substr(jqxzcode,0,4) when '0114' then 1 else 0 end) as dubo,
sum(case when(jqxzcode like '01%' and jqxzcode not like '0105%' and jqxzcode not like '0109%' and jqxzcode not like '0112%' and jqxzcode not like '0110%' and jqxzcode not like '0114%') then 1 else 0 end) as qita1,
--02--
sum(case substr(jqxzcode,0,6) when '020108' then 1 else 0 end) as douou,
sum(case substr(jqxzcode,0,4) when '0207' then 1 else 0 end) as qiaozha,
sum(case when (jqxzcode like '02%' and jqxzcode not like '020108%' and jqxzcode not like '0207%') then 1 else 0 end) as qita2,
--qt--
sum(case substr(jqxzcode,0,2) when '08' then 1 else 0 end) as jiufen,
sum(case substr(jqxzcode,0,2) when '05' then 1 else 0 end) as qiuzhu,
sum(case substr(jqxzcode,0,4) when '0602' then 1 when '0603' then 1 else 0 end) as qiuzhu,
sum(case substr(jqxzcode,0,4) when '0601' then 1 else 0 end) as xiansuojubao,
sum(case substr(jqxzcode,0,2) when '03' then 1 else 0 end) as jiaotongshigu,
sum(case substr(jqxzcode,0,2) when '09' then 1 else 0 end) as shehuiliandong,
sum(case substr(jqxzcode,0,2) when '04' then 1 else 0 end) as huozhai,
sum(case when (jqxzcode like '%' and jqxzcode not like '01%' and jqxzcode not like '02%' and jqxzcode not like '03%' and jqxzcode not like '04%' and jqxzcode not like '05%' and jqxzcode not like '0601%' and jqxzcode not like '0602%' and jqxzcode not like '0603%' and jqxzcode not like '08%' and jqxzcode not like '09%' )then 1 else 0 end) as qita3
from jqfxk j
where
j.bjtime>to_date('2015-07-25 16:00:00','yyyy-mm-dd hh24:mi:ss')
 and
j.bjtime<to_date('2015-08-25 16:00:00','yyyy-mm-dd hh24:mi:ss')
 and
jqxzcode is not null and
unitcode in ('431100000000','431103000000','431102000000','431122000000','431121000000','431123000000','431129000000','431124000000','431128000000','431126000000','431129000000')
group by substr(unitcode,0,6)

  
  以上SQL生成一个下面的表格统计数据:
  横栏是unitcode单位
  横栏是jqxzcode
DSC0000.png
 

  
  
  select bzdwcode, jqtime,
       sum(cnt) as cnt,   ---日合计
       sum(num0) as num0, --其他类合计
       sum(num1) as num1, --抢  劫010501 总数
       sum(num2) as num2, --抢 持枪
       sum(num3) as num3, --抢 室外 摩托
        sum(num3) as num4,--抢 室外 汽车
         sum(num3) as num5,--抢 室外 财物
          sum(num3) as num6,--抢 入室 财物
           sum(num3) as num7,--抢夺 总数
            sum(num3) as num8,--抢夺  飞车
             sum(num3) as num9,--抢夺  非 飞车
              sum(num3) as num10, -- 盗 总数
              sum(num3) as num11, --盗 室外 摩托
              sum(num3) as num12,--盗 室外 汽车
              sum(num3) as num13,---盗 室外 财物
              sum(num3) as num14---盗 入室 财物
      from (
  select  substr(jqtime,1,10)  jqtime,
          case when substr(bzdwcode,1,6) not like '445221%' and
              substr(bzdwcode,1,6) not like '445222%' and 
              substr(bzdwcode,1,6) not like '445224%' and 
              substr(bzdwcode,1,6) not like '445281%' then '445200' else substr(bzdwcode,1,6) end  bzdwcode,
count(*) as cnt,
sum(case when (bzjqxzdm like '01%' or bzjqxzdm like '0414%')
     and bzjqxzdm not like '010501%' and bzjqxzdm not like '010504%' and bzjqxzdm not like '010502%' then 1 else 0 end) num0,
      
sum(case when bzjqxzdm like '010501%' then 1 else 0 end ) num1,
sum(case when bzjqxzdm like '01050150%' or bzjqxzdm like '01050160%'
or bzjqxzdm like '0105016A%' or bzjqxzdm like '0105016B%' then 1 else 0 end ) num2,
sum(case when bzjqxzdm like '0105012A%' then 1 else 0 end ) num3,
sum(case when bzjqxzdm like '0105012B%' or bzjqxzdm like '01050120%'  then 1 else 0 end ) num4,
sum(case when bzjqxzdm like '01050102%' then 1 else 0 end ) num5,
sum(case when bzjqxzdm like '01050101%' then 1 else 0 end ) num6,
 
sum(case when bzjqxzdm like '010504%' then 1 else 0 end) num7,
sum(case when bzjqxzdm like '01050401%' then 1 else 0 end) num8,
sum(case when bzjqxzdm like '010504%' and bzjqxzdm not like '01050401%' then 1 else 0 end) num9,
sum(case when bzjqxzdm like '010502%' then 1 else 0 end) num10,
sum(case when bzjqxzdm like '01050224%' then 1 else 0 end) num11,
sum(case when bzjqxzdm like '01050223%' then 1 else 0 end) num12,
sum(case when bzjqxzdm like '01050240%' then 1 else 0 end) num13,
sum(case when bzjqxzdm like '01050201%' then 1 else 0 end) num14
 from
 JQCASE t where  substr(bzdwcode,1,6) <> '445221' and  substr(bzdwcode,1,6) <> '445224' and  substr(bzdwcode,1,6) <> '445222' and  substr(bzdwcode,1,6) <> '445281' --and jqtime >=? and jqtime <=?
 group by substr(bzdwcode,1,6), substr(jqtime,1,10) )  a
 group by bzdwcode,jqtime order by jqtime, bzdwcode

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-256846-1-1.html 上篇帖子: oracle根据用户查询空表 下篇帖子: oracle 修改锁定账户的处理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表