有一个表A,字段有nbm,sysl,xse。sysl有4%,6%这两个值。要求对于同一个nbm,只要sysl有6%,则查询出sum(xse)>180的nbm,如果sysl只有4%的而没有6%,则查询出sum(xse)>100的nbm。请问这个sql语句如何写啊?

解决方案 »

  1.   

    Try it ..
    SQL> select *
      2    from A tt;       NBM SYSL        XSE
    ---------- ---- ----------
             1 4%          120
             1 6%           20
             1 4%           80
             2 6%          100
             2 4%          200
             2 6%           60
             3 4%           50
             3 4%           30
             3 4%           80
             4 4%           10
             4 4%           8011 rows selectedSQL> 
    SQL> select decode(sign(sysl_6),
      2                1,decode(sign(sum_xse-180),1,nbm,null),
      3                0,decode(sign(sum_xse-100),1,nbm,null),
      4                null) as nbm
      5    from (
      6          select nbm,
      7                 sum(decode(sysl,'4%',1,0)) as sysl_4,
      8                 sum(decode(sysl,'6%',1,0)) as sysl_6,
      9                 count(1) as counts,
     10                 sum(xse) as sum_xse
     11            from A tt
     12           group by nbm
     13         )zz;       NBM
    ----------
             1
             2
             3
      

  2.   

    select nbm
    from A
    where 180 < (select sum(xse)
                            from A
                            where sysl = 6%)
    union all
    select nbm
    from A
    where 100 < (select sum(xse)
                            from A
                            where sysl = 4%)
      

  3.   

    LOVE 野,你大概还没太理解我的意思。
    select   nbm 
    from   A 
    where   180   <   (select   sum(xse) 
                        from   A 
                       where   sysl   =   6%) 
    你这个查询结果估计要么是空要么是全部是nbm。你觉得呢?