有个表A,里面有字段A1、A2、A3, 我需要以下条件的记录:按照A2分组,分组后A3的数据要同时包含a%、b%,但是不能包含c%的A表记录  不知道我说明白没有  如下:
表内数据:
A1  A2  A3
1   1   a 
2   1   b 
3   1   c 
5   2   a 
6   2   b 
7   3   a 
8   3   c 
如果像上面的数据  我需要查出以下2条数据:
5   2   a 
6   2   b 

解决方案 »

  1.   

    try
    select a1,a2,a3 from mytab a
    where exists (select 1 from mytab where a2=a.a2 and a3='a')
    and exists (select 1 from mytab where a2=a.a2 and a3='b')
    and not exists (select 1 from mytab where a2=a.a2 and a3='c')
      

  2.   

    需要改改
    a2=a.a2这个是固定值吧  我需要分组
      

  3.   

    这个易懂,但是效率不高
    select * from A where A2 not in (select  A2 from A where A3='c');
      

  4.   

    这个如果只包含'a'不是也查出来了?要的是同时包含a%、b%
      

  5.   

    怎么会不懂呢我上面有个例子  例子里面有7条记录,按照A2分组,可以分成3组:
    第一组是前面3条记录  虽然这组同时包含a%、b%,但是也包含c%,所有不符合查询要求;
    第二组数据为:
    5  2  a 
    6  2  b 
    这组数据同时包含a%、b%记录,且不包含c%记录,所以符合查询要求;
    第三组数据为:
    7  3  a 
    8  3  c
    这组数据不满足同时包含a%、b%记录的条件  所以不符合查询要求
      

  6.   

    在10g下容易点
    SQL> select * from t01;        A1         A2 A3
    ---------- ---------- ----------
             1          1 a
             2          1 b
             3          1 c
             5          2 a
             6          2 b
             7          3 a
             8          3 c已选择7行。SQL> ed
    已写入 file afiedt.buf  1  select
      2  a1,a2,a3
      3  from
      4  (select a1,a2,a3,wm_concat(a3) over(partition by a2) a4 from t01) a
      5* where a4 like '%a%' and a4 like '%b%' and a4 not like '%c%'
    SQL> /        A1         A2 A3
    ---------- ---------- ----------
             5          2 a
             6          2 b
      

  7.   

    效率我不说 
    但是你这个还得加个条件  就是A3必须同时包含a、b
      

  8.   

    这个改造一下
    select a1,a2,a3 from mytab a
    where exists (select 1 from mytab where a2=a.a2 and a3='a' and a3='b')
    and not exists (select 1 from mytab where a2=a.a2 and a3='c');如果不管效率的话还可以试试这个
    select * from A where A2 not in (select  A2 from A where A3='c') and A2 in (select A2 from A WHERE A3='a' and A3='b');
      

  9.   

    写个好懂的,不过,这个in没exists执行效率高
    select a1,a2,a3 from mytab a
    where a2 in (select a2 from mytab where a3='a')
    and a2 in(select a2 from mytab where a3='b')
    and a2 not in(select a2 from mytab where a3='c')
      

  10.   

    Oracle 9i 下 的 结果:
    create table t2(
    a1 varchar2(10),
    a2 varchar2(10),
    a3 varchar2(10) 
    )
    insert into t2(a1,a2,a3) values('1','1','a');
    insert into t2(a1,a2,a3) values('2','1','b');
    insert into t2(a1,a2,a3) values('3','1','c');
    insert into t2(a1,a2,a3) values('5','2','a');
    insert into t2(a1,a2,a3) values('6','2','b');
    insert into t2(a1,a2,a3) values('7','3','a');
    insert into t2(a1,a2,a3) values('8','3','c');
    insert into t2(a1,a2,a3) values('9','3','c');
    select * from t2;select a1,a2,a3
    from(
    select t2.*,count(decode(t2.a3,'c',1))over(partition by a2) as flag
    from t2
    )
    where flag=0;这样可得出楼主要的结果。
      

  11.   

    9i下怎么办?
    有没有一个在oracle下通用的  
      

  12.   

    2楼和14楼的sql楼主试过了没有,按道理应该可以得出你想要的结果
      

  13.   

    有个问题  这个a、b、c写死了  如何让程序带进来
    不能让我搞个循环吧
      

  14.   

    这个还是有问题的:
    insert into t2(a1,a2,a3) values('10','4','a');
    上面的记录就会被查出来
      

  15.   

    你的a、b、c是从哪取出来的,把完整的需求贴出来
      

  16.   


    and a3='a' and a3='b这个条件本身就是一个false吧
      

  17.   

    稍改了下SQLSQL> select * from v$version;BANNER
    ----------------------------------------------------------------
    Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
    PL/SQL Release 9.2.0.4.0 - Production
    CORE    9.2.0.3.0       Production
    TNS for Linux: Version 9.2.0.4.0 - Production
    NLSRTL Version 9.2.0.4.0 - ProductionSQL> select * from t01;        A1         A2 A3
    ---------- ---------- ----------
             1          1 a
             2          1 b
             3          1 c
             5          2 a
             6          2 b
             7          3 a
             8          3 d已选择7行。SQL> select a1,a2,a3 from t01 a
      2  where exists (select * from t01 b where a3 like '%a%' and a.a2=b.a2)
      3  and exists (select * from t01 c where a3 like '%b%' and a.a2=c.a2)
      4  and not exists(select * from t01 d where a3 like '%c%' and a.a2=d.a2);        A1         A2 A3
    ---------- ---------- ----------
             5          2 a
             6          2 b
      

  18.   

    查询条件a,b,c是用户从页面上输入的  可能是a,b,c,d,e,f,g,h... ...
    如果是这样的话 sql就很不好组合了  而且效率就成问题了。