Hello to all,
I have to design a SQL query on an Oracle 10 DB.
I want to do it in an elegant way and avoid, if possible, using sub-queries and in the most portable way possible.
Basically it's query involving 3 tables :
- A MEMBERS table. This table holds information about persons.
The relevant fields are first name, last name, member id (numeric auto-generated primary key), beginning of membership (a date), end of membership (date).
None of these fields can be null.
- A SESSIONS table. A session is a meeting between MEMBERS. It has an id (numeric auto-generated primary key), a date, that can't be null,
and other irrelevant information.
- Since sessions typically involve several MEMBERS, and a member can participate in several SESSIONS, there is an association table MEMBERS_SESSIONS
that has the member id, sessions id, (of course the combination of the two must be unique across the table) and various other irrelevant fields.
What I have to is to allow users to add members to an existing session, whose id is known.
For this I have to generate a list of all the members that satisfy three conditions :
-they are not currently a member of that session.
-their beginning of membership is before, or equal to the session date.
-their end of membership is after or equal to the session date.
I couldn't figure out a way to do this without sub-queries, and even the query I obtained looks clumsy because the session id is mentioned 3 times, once for each condition.
Is there a way to do this using only SQL operators like LEFT JOIN, RIGHT JOIN, MINUS, INTERSECT, etc. ? I haven't touched SQL for 3 years and could'nt figure it out.
Thanks in advance.
I have to design a SQL query on an Oracle 10 DB.
I want to do it in an elegant way and avoid, if possible, using sub-queries and in the most portable way possible.
Basically it's query involving 3 tables :
- A MEMBERS table. This table holds information about persons.
The relevant fields are first name, last name, member id (numeric auto-generated primary key), beginning of membership (a date), end of membership (date).
None of these fields can be null.
- A SESSIONS table. A session is a meeting between MEMBERS. It has an id (numeric auto-generated primary key), a date, that can't be null,
and other irrelevant information.
- Since sessions typically involve several MEMBERS, and a member can participate in several SESSIONS, there is an association table MEMBERS_SESSIONS
that has the member id, sessions id, (of course the combination of the two must be unique across the table) and various other irrelevant fields.
What I have to is to allow users to add members to an existing session, whose id is known.
For this I have to generate a list of all the members that satisfy three conditions :
-they are not currently a member of that session.
-their beginning of membership is before, or equal to the session date.
-their end of membership is after or equal to the session date.
I couldn't figure out a way to do this without sub-queries, and even the query I obtained looks clumsy because the session id is mentioned 3 times, once for each condition.
Is there a way to do this using only SQL operators like LEFT JOIN, RIGHT JOIN, MINUS, INTERSECT, etc. ? I haven't touched SQL for 3 years and could'nt figure it out.
Thanks in advance.