有个表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
表内数据:
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
解决方案 »
- 跟帖,求大神指点
- windows下导出数据迁移到linux下,数据出现乱码。求帮助。
- oracle 连接问题
- ora-12170问题请教
- 超难的SQL语句
- 触发器问题 大家帮看看
- 表table建了一个CLOB字段,请问如何插入长字符串呢,,老提示"字符串文字太长"
- oracle 9i导入!
- Oracle错误提示:VTK-1000:无法连接到Management Server yl。请验证您已输入Oracle Management Server 的正确主机名和状态。在线等待!
- 菜鸟问题:请问如何查看数据库及查看数据库的表?
- oracle分页的问题
- REHL 5 安装oracle 11g----如何解决?
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')
a2=a.a2这个是固定值吧 我需要分组
select * from A where A2 not in (select A2 from A where A3='c');
第一组是前面3条记录 虽然这组同时包含a%、b%,但是也包含c%,所有不符合查询要求;
第二组数据为:
5 2 a
6 2 b
这组数据同时包含a%、b%记录,且不包含c%记录,所以符合查询要求;
第三组数据为:
7 3 a
8 3 c
这组数据不满足同时包含a%、b%记录的条件 所以不符合查询要求
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
但是你这个还得加个条件 就是A3必须同时包含a、b
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');
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')
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;这样可得出楼主要的结果。
有没有一个在oracle下通用的
不能让我搞个循环吧
insert into t2(a1,a2,a3) values('10','4','a');
上面的记录就会被查出来
and a3='a' and a3='b这个条件本身就是一个false吧
----------------------------------------------------------------
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
如果是这样的话 sql就很不好组合了 而且效率就成问题了。