zoukankan      html  css  js  c++  java
  • 待研究 case when 和decode

     假设有张学生成绩表(CJ)如下 
    [姓名] [学科] [成绩] 
    张三 语文 80 
    张三 数学 86 
    张三 英语 75 
    李四 语文 78 
    李四 数学 85 
    李四 英语 78 

    现有需求如下:
    (1)要求统计分数段的人数。显示结果为:
    [成绩]      [人数]
    0<成绩<60      0 
    60<成绩<80      3
    80<成绩<100    3

    with w as (select 'zhang' name, 'chinese' subject,80 score from dual
      union all
              select 'zhang' name, 'math' subject,86 score from dual
           
      union all
     
      select 'zhang' name, 'english' subject,75 score from dual
      union all
     
      select 'li' name, 'english' subject,78 score from dual
      union all
     
      select 'li' name, 'math' subject,85 score from dual
      union all
     
      select 'li' name, 'chinese' subject,78 score from dual
      )
     select w.*  from w

    with w as (select 'zhang' name, 'chinese' subject,80 score from dual
      union all
              select 'zhang' name, 'math' subject,86 score from dual
           
      union all
      
      select 'zhang' name, 'english' subject,75 score from dual
      union all
      
      select 'li' name, 'english' subject,78 score from dual
      union all
      
      select 'li' name, 'math' subject,85 score from dual
      union all
      
      select 'li' name, 'chinese' subject,78 score from dual
      )
     select w.*  from w 
    

      select w.*, decode( score, score >=0 and score<60  ,'0-60',
         score>=60 and score<80, '60-80', score>=80 and score<100 , '80-100', else)  from w
    这样不行

    select dj,count(*) rs from(select xm,xk,case when cj>=0 and cj<60 then 'c' when cj>=60 and cj<80 then 'b' when cj>=80 and cj<=100 then 'a' end dj from cjb)
    group by dj
    
  • 相关阅读:
    勿忘心安
    设△ABC的内角A,B,C,所对的边分别为a,b,c,且acosB-bcosA=3/5c,则tan(A-B)的最大值为
    P1011 车站
    第一天
    P1134 阶乘问题
    P3152 正整数序列
    某数论
    DO YOU WANNA BUILD A SNOW MAN ?
    luogu P1579 哥德巴赫猜想(升级版)
    紫书 习题 10-25 UVa 1575 (有重复元素的全排列+暴搜)
  • 原文地址:https://www.cnblogs.com/sumsen/p/2837669.html
Copyright © 2011-2022 走看看