求一条oracle的sql语句

missmile 2009-05-25 04:05:01
有个表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
...全文
211 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿三 2009-05-26
  • 打赏
  • 举报
回复
如果条件太多,效率的确不会高,可不可以写个存储过程先将此表进行一下行列转换,然后查询其转换后的表呢
missmile 2009-05-25
  • 打赏
  • 举报
回复
查询条件a,b,c是用户从页面上输入的 可能是a,b,c,d,e,f,g,h... ...
如果是这样的话 sql就很不好组合了 而且效率就成问题了。

[Quote=引用 27 楼 zxf_feng 的回复:]
稍改了下SQL

SQL> 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 - Production

SQL> select * from t01;

A1 …
[/Quote]
阿三 2009-05-25
  • 打赏
  • 举报
回复
稍改了下SQL

SQL> 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 - Production

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 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

superhsj 2009-05-25
  • 打赏
  • 举报
回复
是啊,所以我又说不对了吗
[Quote=引用 25 楼 zxf_feng 的回复:]
这个条件本身就是一个false吧[/Quote]
阿三 2009-05-25
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 superhsj 的回复:]
这个改造一下
SQL code
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');



如果不管效率的话还可以试试这个
SQL code
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');


[/Quote]

and a3='a' and a3='b

这个条件本身就是一个false吧
bw555 2009-05-25
  • 打赏
  • 举报
回复
你的a、b、c是从哪取出来的,把完整的需求贴出来
[Quote=引用 21 楼 missmile 的回复:]
有个问题 这个a、b、c写死了 如何让程序带进来
不能让我搞个循环吧


引用 14 楼 bw555 的回复:
写个好懂的,不过,这个in没exists执行效率高

SQL codeselect 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')
[/Quote]
missmile 2009-05-25
  • 打赏
  • 举报
回复
这个还是有问题的:
insert into t2(a1,a2,a3) values('10','4','a');
上面的记录就会被查出来

[Quote=引用 16 楼 chh2008 的回复:]
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…
[/Quote]
bw555 2009-05-25
  • 打赏
  • 举报
回复
你把语句到环境里一执行不久什么都清楚了,哎!!!
16楼的不行,不能保证a、b都有
missmile 2009-05-25
  • 打赏
  • 举报
回复
有个问题 这个a、b、c写死了 如何让程序带进来
不能让我搞个循环吧


[Quote=引用 14 楼 bw555 的回复:]
写个好懂的,不过,这个in没exists执行效率高

SQL codeselect 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')
[/Quote]
missmile 2009-05-25
  • 打赏
  • 举报
回复
这个貌似也正确

[Quote=引用 14 楼 bw555 的回复:]
写个好懂的,不过,这个in没exists执行效率高

SQL codeselect 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')
[/Quote]
missmile 2009-05-25
  • 打赏
  • 举报
回复
貌似正确

[Quote=引用 16 楼 chh2008 的回复:]
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…
[/Quote]
superhsj 2009-05-25
  • 打赏
  • 举报
回复
2楼和14楼的sql楼主试过了没有,按道理应该可以得出你想要的结果
missmile 2009-05-25
  • 打赏
  • 举报
回复
9i下怎么办?
有没有一个在oracle下通用的

[Quote=引用 9 楼 zxf_feng 的回复:]
在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…
[/Quote]
chh2008 2009-05-25
  • 打赏
  • 举报
回复
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;

这样可得出楼主要的结果。
superhsj 2009-05-25
  • 打赏
  • 举报
回复
还是得拆开写,合着不对
[Quote=引用 13 楼 superhsj 的回复:]
这个改造一下

SQL codeselect 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');



如果不管效率的话还可以试试这个

SQL codeselect * 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');
[/Quote]
bw555 2009-05-25
  • 打赏
  • 举报
回复
写个好懂的,不过,这个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')
superhsj 2009-05-25
  • 打赏
  • 举报
回复
这个改造一下
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');
lee24 2009-05-25
  • 打赏
  • 举报
回复
这个是对的,看不出楼主要分组的意思
[Quote=引用 6 楼 superhsj 的回复:]
这个易懂,但是效率不高

SQL codeselect * from A where A2 not in (select A2 from A where A3='c');
[/Quote]
superhsj 2009-05-25
  • 打赏
  • 举报
回复
哦,还要必须有a、b啊,那样的话2楼的答案就是正确的吧
[Quote=引用 8 楼 missmile 的回复:]
怎么会不懂呢

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


引用 4 楼 jdsnhan 的回复:
没看懂
[/Quote]
missmile 2009-05-25
  • 打赏
  • 举报
回复
效率我不说
但是你这个还得加个条件 就是A3必须同时包含a、b


[Quote=引用 6 楼 superhsj 的回复:]
这个易懂,但是效率不高

SQL codeselect * from A where A2 not in (select A2 from A where A3='c');
[/Quote]
加载更多回复(9)

17,090

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧