I will attach two examples database.
To understand what I want to do... let's take a look to this database:
Please, download this database:
customers and agents.odb
https://mega.nz/#!4VZCHJYD!Jg8jT1bV8fAD ... FeXR03hPng
To summarize, in that database you have an AGENTS table and a CUSTOMERS table.
Due agents who are selling stuff can operate in several places, the CUSTOMERS table have a lot of fields related to customers, but one of them is the field AGENT_CODE
That field is also present in the table agents
What happens here is every record from every customer, customers table, have present the field AGENT_CODE, indicating that particular agent is operating for that customer's area (place).
Let's imagine I want to retrieve agents operating in certain the customer country, exactly in the UK.
Then, I can tackle this subquery approach typing this code:
Code: Select all
SELECT
"AGENT_CODE",
"AGENT_NAME",
"WORKING_AREA",
"COMMISSION"
FROM "agents"
WHERE "AGENT_CODE" IN ( SELECT "AGENT_CODE" FROM "customers" WHERE "CUST_COUNTRY" = 'UK' )
// WHERE "AGENT_CODE" ANY ( SELECT "AGENT_CODE" FROM "customers" WHERE "CUST_COUNTRY" = 'UK' )
// ANY WOULD PROCUDE THE SAME EFFECT
What happened is the subquery retrieved those records from the table CUSTOMERS in which CUST_COUNTRY be UK, giving the associated agents to that particular record and showing them.
Okay.
That works fine.
My problem start now.
Please, download this database.
subqueryapproach.odb
https://mega.nz/#!lBQmiBra!XdN0jdfppmAr ... fDIg59Zvhw
I have a many to many relationship in three tables.
TABLE 1: EMPLEADOS (employees) a table in which I have the employees of a company
TABLE 2: DEPENDENCIAS (facilities) a table in which I have the places inside the building in which the employees are working. For example: ROOM 1A, ROOM 2B, MEETING ROOM, CELLAR 1, GENERAL PARKING, etc.
TABLE 3: USODEDEPENDENCIAS (a linking table to make possible the many to many relationship). The primary key from the tables above EMPLEADOS and DEPENDENCIAS, are present in this table, in this way I can assign rooms to employees granting them access (ACCESO in spanish). If you open this table you will see both primary keys, and the access granted to every employee.
Okay. That is perfect.
Let's imagine I want to see all my employees and I want to see the places they have granted access. Easy, I only have to type this code.
Code: Select all
SELECT
"tbl_EMPLEADOS"."Nombre",
"tbl_EMPLEADOS"."Apellido1ero",
"tbl_EMPLEADOS"."Apellido2seg",
"tbl_DEPENDENCIAS"."Dependencia",
"tbl_DEPENDENCIAS"."DepenDescripcion",
"tbl_DEPENDENCIAS"."Edificio",
"tbl_USODEDEPENDENCIAS"."MotivoAsignacion"
FROM
"tbl_USODEDEPENDENCIAS", "tbl_EMPLEADOS", "tbl_DEPENDENCIAS"
WHERE "tbl_USODEDEPENDENCIAS"."ID_Empleados" = "tbl_EMPLEADOS"."ID_Empleados" AND "tbl_USODEDEPENDENCIAS"."ID_Dependencia" = "tbl_DEPENDENCIAS"."ID_Dependencia"
Let's imagine I want to filter this and retrive only those employees that have access to the CELLAR 2 (SOTANO 2)
Then I would only have to type this code.
Code: Select all
SELECT
"tbl_EMPLEADOS"."Nombre",
"tbl_EMPLEADOS"."Apellido1ero",
"tbl_EMPLEADOS"."Apellido2seg",
"tbl_DEPENDENCIAS"."Dependencia",
"tbl_DEPENDENCIAS"."DepenDescripcion",
"tbl_DEPENDENCIAS"."Edificio",
"tbl_USODEDEPENDENCIAS"."MotivoAsignacion"
FROM
"tbl_USODEDEPENDENCIAS", "tbl_EMPLEADOS", "tbl_DEPENDENCIAS"
WHERE "tbl_USODEDEPENDENCIAS"."ID_Empleados" = "tbl_EMPLEADOS"."ID_Empleados" AND "tbl_USODEDEPENDENCIAS"."ID_Dependencia" = "tbl_DEPENDENCIAS"."ID_Dependencia"
AND "tbl_DEPENDENCIAS"."Dependencia" = 'SOTANO 2'
Okay, what I would like to do is exactly the same but typing a subquery, and my problem is I don't know how to type a subquery for this particular code.
In the first database customers and agents.odb, we did not have a relationship, because in every record from every customer we would have present the field AGENT_CODE, then we could type the subquery as follows...
Code: Select all
WHERE "AGENT_CODE" IN ( SELECT "AGENT_CODE" FROM "customers" WHERE "CUST_COUNTRY" = 'UK' )
The code I wrote give that information perfectly, hower I would like to do exactly the same, in a subquery approach.
I'm stuck with that. Please, could someone give me an example or a bit of code to show me how to do that?
My problem is due we have the WHERE stablishing the linking relationships between tables, I cannot use it as WHERE ... IN and I don't know how to tackle a subquery approach.
Any ideas or suggestions?
Cheers