-
Website
http://oracletoday.blogspot.com -
Original page
http://oracletoday.blogspot.com/2008/04/relational-algebra-division-in-sql.html -
Subscribe
All Comments -
Community
-
Top Commenters
-
wheels for sale
1 comment · 1 points
-
Replica handbags
1 comment · 1 points
-
brijeshchauhan
1 comment · 1 points
-
TongucY
1 comment · 1 points
-
samiuet
1 comment · 1 points
-
-
Popular Threads
select distinct sid
from catalog c, parts p
where c.pid = p.pid and color = 'Red'
group by sid
having count(distinct c.pid) =
(select count(distinct pid)
from parts
where color = 'Red');
select distinct sid
from catalog c1
where not exists (
(select pid
from parts
where color = 'Red')
minus
(slect pid
from catalog c2
where c1.sid = c2.sid));
select sid, count(*) total
from catalog
group by sid having count(*) =
(select count(*) from parts);
select sid from catalog
group by sid
having collect(pid) = (select collect(pid) from parts);
well, it is maybe slower
create or replace type parts_t as object (pid number);
/
create or replace type parts_tt as table of parts_t;
/
select sid from
(select sid,cast(collect(parts_t(pid)) as parts_tt) x from catalog group by sid)
where
((select cast(collect(parts_t(pid))as parts_tt) from parts)) submultiset x;
For the ones interested: submultiset does not exist in 9.2, it comes with 10G R1. It is used to find out if a nested table is a subset of another nested table. Documentation about it is here
Thanks again Laurent.
SQL> select sid
2 from catalog
3 group by sid
4 having
5 (select cast(collect(parts_t(pid))as parts_tt) from parts)
6 submultiset
7* cast(collect(parts_t(pid)) as parts_tt)
select sid
*
ERROR at line 1:
ORA-00979: not a GROUP BY expression
the ORA-979 makes no sense. I will open a SR and it may be fixed in Oracle 15
SQL> r
1 select sid from
2 (select sid,cast(collect(parts_t(pid)) as parts_tt) x from catalog group by sid)
3 where
4* ((select cast(collect(parts_t(pid))as parts_tt) from parts)) submultiset x
SID
----------
1
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bi
PL/SQL Release 10.2.0.2.0 - Production
CORE 10.2.0.2.0 Production
TNS for Solaris: Version 10.2.0.2.0 - Production
NLSRTL Version 10.2.0.2.0 - Production
select distinct sid
from catalog c, parts p
where c.pid = p.pid
group by sid
having count(distinct c.pid) =
(select count(distinct pid)
from parts);
select distinct sid
from catalog c1
where not exists (
(select pid
from parts)
minus
(slect pid
from catalog c2
where c1.sid = c2.sid));
Thanks for your postings and comments regarding Relational Algebra: division in sql!
elai
select sid from (select sid, count(sid) count
from (select distinct sid, pid from catalog) c,
parts p where c.pid = p.pid group by sid) s
where s.count = (select count(distinct pid) from parts)
SELECT name
FROM Student
WHERE SID IN ( SELECT SID
FROM Register
GROUP BY SID
HAVING COUNT(*) = (SELECT COUNT(*) FROM Course)
)
SELECT name
FROM Student
WHERE SID IN ( SELECT SID
FROM Register
GROUP BY SID
HAVING COUNT(*) = (SELECT COUNT(*) FROM Course)