sql

select '本科毕业生' a,
       nvl(t.b, 0) b,
       round(nvl(t.b, 0)/t.d*100,2)||'%' e,  
       nvl(t.c, 0) c,
       round(nvl(t.c, 0) /t.d*100,2)||'%' f  
        from
(select '1' hdss, count(*) bkshznum
          from (select * from jy_jyxt_syxxb 
         WHERE xlccdm = '31'
           and byzd5 = '1' and mzdm='01') where bynf='2014'
           union all

select '2' hdss, count(*) bksssmznum
          from (select * from jy_jyxt_syxxb 
         WHERE xlccdm = '31'
           and byzd5 = '1' and mzdm<>'01') where bynf='2014'
 union all
 select '3' hdss, count(*) zrs
          from (select * from jy_jyxt_syxxb 
         WHERE xlccdm = '31'
           and byzd5 = '1') where bynf='2014')
      
           
           pivot(sum(bkshznum)
   for hdss in('1' as b, '2' as c, '3' as d)) t
   union all
select '毕业研究生' a,
       nvl(t.b, 0) b,
       round(nvl(t.b, 0)/t.d*100,2)||'%' e,  
       nvl(t.c, 0) c,
       round(nvl(t.c, 0) /t.d*100,2)||'%' f  
        from
(select '1' hdss, count(*) bkshznum
          from (select * from jy_jyxt_syxxb 
         WHERE xlccdm = '11'
           and byzd5 = '1' and mzdm='01') where bynf='2014'
           union all

select '2' hdss, count(*) bksssmznum
          from (select * from jy_jyxt_syxxb 
         WHERE xlccdm = '11'
           and byzd5 = '1' and mzdm<>'01') where bynf='2014'
 union all
 select '3' hdss, count(*) zrs
          from (select * from jy_jyxt_syxxb 
         WHERE xlccdm = '11'
           and byzd5 = '1') where bynf='2014')
      
           
           pivot(sum(bkshznum)
   for hdss in('1' as b, '2' as c, '3' as d)) t
     
   union all
select '总体' a,
       nvl(t.b, 0) b,
       round(nvl(t.b, 0)/t.d*100,2)||'%' e,  
       nvl(t.c, 0) c,
       round(nvl(t.c, 0) /t.d*100,2)||'%' f  
        from
(select '1' hdss, count(*) ztbkshznum
          from (select * from jy_jyxt_syxxb 
         WHERE byzd5 = '1' and mzdm='01' and (xlccdm = '31' or xlccdm = '11')) where bynf='2014'
           union all

   select '2' hdss, count(*) bksssmznum
          from (select * from jy_jyxt_syxxb 
         WHERE byzd5 = '1' and mzdm<>'01' and (xlccdm = '31' or xlccdm = '11')) where bynf='2014'
 union all
    select '3' hdss, count(*) zrs
          from (select * from jy_jyxt_syxxb 
         WHERE byzd5 = '1' and (xlccdm = '31' or xlccdm = '11')
           ) where 1=1 and bynf='2014')
      
           
           pivot(sum(ztbkshznum)
   for hdss in('1' as b, '2' as c, '3' as d)) t
A B E C F
1 本科毕业生 13 92.86% 1 7.14%
2 毕业研究生 2 100% 0 0%
3 总体 15 93.75% 1 6.25%
  • round 除法留几位小数点

  • pivot 行列转换

  • nvl(t.b, 0) 如果t.b是null,那么返回0

  • substr 截取字符,例如(1994-06-09),sj='1994-06-09' substr(sj,0,4) 就会把1994筛选出来

  • distinct重复的值只取一个,例如,时间筛选出来有很多条记录且时间是一样的,例如1994,多条,那么执行如下sql,就只筛选出一个

select distinct(dlsj) sj from (select substr(sj,0,4) sj from JY_JYWZ_YHDLJLB 
            where sj is not null  order by sj asc)
  • 视图:类似于联表查询的结果,只能查询
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Oracle行转列、列转行的Sql语句总结 多行转字符串这个比较简单,用||或concat函数可以实现SQL Co...
    王滕辉阅读 6,155评论 0 3
  • 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句。 问题及描述: --1.学生表 Stud...
    lijun_m阅读 1,325评论 0 1
  • 50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cna...
    最美的太阳WW阅读 3,235评论 0 23
  • NB-iot 的风刚刚吹起,为了保持以时俱进,入手一块开发版。 经测试并没有像运营商所说的那样子,信号覆盖率超广,...
    Lupino阅读 390评论 0 0
  • 《交办的技术》8-9 原文: "把工作交办出去"是当主管的事,第一件要学的事。但很多人对这件事都有困扰,甚至还有人...
    Michelle卢晓燕阅读 201评论 0 0