现在有一个表:
成绩表
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 ans = 1 then 'Y' else 'N' end 全部通过
      from(
        select a_no,a_type,sum(case when a_flag = 1 then 1 else 0 end)/count(1) ans
          from t_result
        group by a_no,a_type
    );
      

  2.   

    select a_no,a_type,case when lv = 1 then 'Y' else 'N' end 全部通过
      from(
        select a_no,a_type,sum(case when a_flag = 1 then 1 else 0 end)/count(1) lv
          from t_result
        group by a_no,a_type
    );
      

  3.   

    楼上两位写的都是对的啊!楼主可以尝试一下!
    其实还有一个更简单的:
    select a_no,a_type,decode(sign(sum(a_flag)-count(1)),'0','Y','N')全部通过
          from t_result 
        group by a_no,a_type ;
      

  4.   


    SELECT
     TMP_TABLE.A_NO,
     TMP_TABLE.A_TYPE,
     CASE WHEN ASUM = CSUM THEN 'Y' ELSE 'N' END AS 全部通过
    FROM
    (
    SELECT T.A_NO,
           T.A_TYPE,
           T.A_FLAG,
           SUM(T.A_FLAG) OVER(PARTITION BY T.A_NO, T.A_TYPE ORDER BY T.A_NO) AS ASUM,
           COUNT(T.A_FLAG) OVER(PARTITION BY T.A_NO, T.A_TYPE ORDER BY T.A_NO) AS CSUM,
           ROW_NUMBER() OVER(PARTITION BY T.A_NO, T.A_TYPE ORDER BY T.A_NO ) AS RN 
      FROM T_RESULT T
    )TMP_TABLE
    WHERE
    TMP_TABLE.RN = 1
      

  5.   

    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
      

  6.   

    现在有这样的问题,我的A_FLAG字段里面的值可能有四种'0'  '1'  '2'   '3'   '4'这四种,我想查询出当年前A_FLAG全部为四的记录,查出来:A_NO       A_TYPE     A_SUBJECT  A_FLAG
    ---------- ---------- ---------- ----------
    001        0001       C          0
    001        0001       A          1
    001        0001       A          2
    001        0001       A          4
    001        0000       A          4
    001        0000       B          4
    001        0000       C          4
    001        0000       F          4查询结果为:001        0001       N    (原因是001--0001 这条记录A_FLAG不是全部为4的)
    001        0000       Y
      

  7.   

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

  8.   

    那就再加两条数据给你试试
    SQL> select * from t_result;A_NO       A_TYPE     A_SUBJECT  A_FLAG
    ---------- ---------- ---------- ----------
    001        0001       C          0
    001        0001       A          1
    001        0000       A          1
    001        0000       B          1
    001        0001       A          4
    001        0002       A          4
    001        0002       B          47 rows selected.SQL> select a_no,a_type,decode(flag,0,'Y','N') as "全部通过"
      2  from
      3  (
      4  select a_no,a_type,sum(case when a_flag=4 then 0 else 1 end) as flag
      5  from t_result
      6  group by a_no,a_type
      7  );
    A_NO       A_TYPE     全部通过
    ---------- ---------- ----------------------------------------
    001        0001       N
    001        0002       Y
    001        0000       N
      

  9.   

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

  10.   

    昱总是猪select A_NO, A_TYPE    from T group by A_NO,A_TYPE 
    having  ,sum(case when A_FLAG=4 then null else 1 end  ) is null就可以了
    。这样查出来的就是那个值不全为4的