681
社区成员
发帖
与我相关
我的任务
分享
ID PID detail
1 3 葱花
2 3 糖
3 3 盐
4 4 小麦
5 4 糖
6 4 酵母
7 4 麦乳精
8 5 糖
9 5 麦乳精
10 5 盐
with t as
(
select 1 id , 3 pid , '葱花' detail from dual
union all
select 2 , 3 , '糖' from dual
union all
select 3 , 3 , '盐' from dual
union all
select 4 , 4 , '小麦' from dual
union all
select 5 , 4 , '糖' from dual
union all
select 6 , 4 , '酵母' from dual
union all
select 7 , 4 , '麦乳精' from dual
union all
select 8 , 5 , '糖' from dual
union all
select 9 , 5 , '麦乳精' from dual
union all
select 10 , 5 , '盐' from dual
)
select distinct pid from t a where
a.detail in ('糖','盐') and
exists(select 1 from t b where a.pid = b.pid and detail = '糖')
and exists(select 1 from t b where a.pid = b.pid and detail = '盐')
--------------------
pid
-----
3
5
select pid
from (select pid, t.detail from test t group by t.pid, t.detail) a
where a.detail = '糖'
INTERSECT
select pid
from (select pid, t.detail from test t group by t.pid, t.detail) a
where a.detail = '小麦'
INTERSECT
select pid
from (select pid, t.detail from test t group by t.pid, t.detail) a
where a.detail = '酵母'
scott@ORCL> create table t (id varchar2(4) primary key,pid varchar2(4) , detail varchar2(20))
2 ;
scott@ORCL> desc t
名称 是否为空? 类型
----------------------------------------------------- -------- -------------------------------
ID NOT NULL VARCHAR2(4)
PID VARCHAR2(4)
DETAIL VARCHAR2(20)
scott@ORCL> insert into t values(1,3,'葱花');
scott@ORCL> insert into t values(2,3,'糖');
scott@ORCL> insert into t values(3,3,'盐');
scott@ORCL> insert into t values(4,4,'小麦');
scott@ORCL> insert into t values(5,4,'糖');
scott@ORCL> insert into t values(6,4,'酵母');
scott@ORCL> insert into t values(7,4,'麦乳精');
scott@ORCL> insert into t values(8,5,'糖');
scott@ORCL> insert into t values(9,5,'麦乳精');
scott@ORCL> insert into t values(10,5,'盐');
scott@ORCL> commit;
CREATE OR REPLACE TYPE testArr AS VARRAY(50) of varchar(20);
create or replace procedure p_t_test(i_timeArr in testArr)
is
type c_ref_type is ref cursor;
dataCur c_ref_type;
v_sqlFragment varchar2(500);
type rec_type is record(
pid t.pid%type
);
rec rec_type;
begin
for i in 1..i_timeArr.COUNT-1 loop
v_sqlFragment:=v_sqlFragment||' detail like ''%'||i_timeArr(i)||'%'' and ';
end loop;
v_sqlFragment :=v_sqlFragment||'detail like ''%'||i_timeArr(i_timeArr.COUNT)||'%''';
open dataCur for q'!with tmp as (select pid , wmsys.wm_concat(detail) detail from t group by pid)
select pid from tmp
where !' ||v_sqlFragment ;
loop
fetch dataCur
into rec;
exit when dataCur%notfound;
DBMS_OUTPUT.put_line('rec.pid : ' ||rec.pid );
end loop;
close dataCur;
exception
when others then
DBMS_OUTPUT.put_line(q'!with tmp as (select pid , wmsys.wm_concat(detail) detail from t group by pid)
select pid from tmp
where!' ||v_sqlFragment) ;
raise;
end p_t_test;
SQL> declare
2 test_arr testArr := testArr();
3 begin
4 test_arr.EXTEND;
5 test_arr(1) := '糖';
6 test_arr.EXTEND;
7 test_arr(2) := '盐';
8 dbms_output.put_line(test_arr(1)||' '||test_arr(2));
9 p_t_test(test_arr);
10 end;
11 /
糖 盐
rec.pid : 3
rec.pid : 5
PL/SQL procedure successfully completed
with tmp as (select pid , wmsys.wm_concat(detail) detail from t group by pid)
select pid from tmp
where detail like '%糖%' and detail like '%盐%'

select pid from tb where id in (
select max(id) from tb group by detail having count(*) >=2)
这样行不?