现在有一个表:
成绩表
t_result:    a_no      a_type       a_subject      a_flag
    001        0000            A             1
    001        0000            B             1
    001        0001            A             1
    001        0001            C             0
    002        0000            C             1
    002        0000            E             0
    002        0000            F             1
    002        0001            A             1
    002        0001            B             0
    002        0001            C             1 有自己的编号(a_no),还有一个类型编号(a_type), 这两个字段作为表的主键。
可以选不同可的科目,且科目的数量及个数都可以不同。后面是标识当前科目是否通过。
现在想做一个统计语句,求出的结果类似于查出来按照主键进行分组。查询的结果实例为:
    a_no       a_type         全部通过
    001        0000              Y
    001        0001              N
    002        0000              N
    002        0001              N

解决方案 »

  1.   

    select a_no,
           a_type,
           case when count(decode(a_flag,0,1))>0 then 'N'
             else 'Y' end "全部通过"
    from t_result
    group by a_no,a_type
      

  2.   

    select a_no,a_type,decode(sign(sum(a_flag)-count(1)),'0','Y','N')全部通过 
          from t_result 
        group by a_no,a_type ;
      

  3.   

    A_NO       A_TYPE     A_SUBJECT  A_FLAG
    ---------- ---------- ---------- ----------
    001        0001       C          0
    001        0001       A          1
    001        0000       A          1
    001        0000       B          1
    SQL> select a_no,a_type,decode(flag,1,'N','Y') as "全部通过"
      2  from
      3  (
      4  select a_no,a_type,sum(case when a_flag=0 then 1 else 0 end) as flag
      5  from t_result
      6  group by a_no,a_type
      7  );
    A_NO       A_TYPE     全部通过
    ---------- ---------- ----------------------------------------
    001        0001       N
    001        0000       Y