一张表中有两个字段,字段值有的为空如:有一个字段中有100个有效数据,另一个字段有75个有效数据和25个null;
现在我要把有效数据多的那个字段查询出来,求高手出手!

解决方案 »

  1.   

    我抛砖
    select case
             when G1.cnt > G2.cnt then
              'col_AAA'
             else
              'col_BBB'
           end
      from (select count(t1.*) cnt from table_name t1 where t1.col_AAA <> null) G1,
           (select count(t2.*) cnt from table_name t2 where t2.col_BBB <> null) G2
      

  2.   

    补个别名
    select case
             when G1.cnt > G2.cnt then
              'col_AAA' col_name
             else
              'col_BBB' col_name
           end
      from (select count(t1.*) cnt from table_name t1 where t1.col_AAA <> null) G1,
           (select count(t2.*) cnt from table_name t2 where t2.col_BBB <> null) G2
      

  3.   

    表有些字段,有些什么记录,sql结果是什么,举例说明
      

  4.   

    select (case kip>kip1 then a else b) from (
    select a,b,sum(kip) kip,sum(kip1) kip from
    (select a,b,count(*)kip,0 kip1 from table where a is not null
    UNION all
     select a,b,0 kip, count(*) kip1from table where b is not null
    )
    group a )
      

  5.   

    SELECT CASE
             WHEN (Q.COUNT1 - Q.COUNT2) > 0 THEN
              'col1 有效'
             ELSE
              'col2 有效'
           END RESULT
      FROM (SELECT COUNT(CASE
                           WHEN T.COL1 IS NOT NULL THEN
                            1
                           ELSE
                            0
                         END) COUNT1,
                   COUNT(CASE
                           WHEN T.COL2 IS NOT NULL THEN
                            1
                           ELSE
                            0
                         END) COUNT2
              FROM TAB1 T) Q
      

  6.   

    把函数修改了一下!
    SELECT CASE 
            WHEN (Q.COUNT1 - Q.COUNT2) > 0 THEN 
              'col1 有效' 
            ELSE 
              'col2 有效' 
          END RESULT 
      FROM (SELECT SUM(CASE 
                          WHEN T.COL1 IS NOT NULL THEN 
                            1 
                          ELSE 
                            0 
                        END) COUNT1, 
                  SUM(CASE 
                          WHEN T.COL2 IS NOT NULL THEN 
                            1 
                          ELSE 
                            0 
                        END) COUNT2 
              FROM TAB1 T) Q
      

  7.   


    select case when num1>num2 then 'col1' else 'col2' end from (select sum(decode(col1, null, 0, 1)) num1, sum(decode(col2, null, 0, 1)) num2 from table1);
      

  8.   

    已解决select 
    (case when count(A_NO)>count(B_NO) then A_NO else B_NO end) as t_NO
    from t1 group by A_NO,B_NO