sonic1234
asked on
Help With Database JOIN
Hello Experts,
I have a table of Organisations, each organisation is associated with a primary category via a field 'teOrgCategory'
SELECT [teOrgTitle]
,[teOrgGUID]
,[teOrgCategory]
FROM [my_database].[dbo].[tblCs dOrg]
WHERE teOrgCategory = 'CAT-CSD-DBH-30-14-85'
So I can also associate other Organisations with additional categories I have created another table that allows additional associations between organisations and categories
SELECT [teOrgAsOrgGUID]
,[teOrgAsOrgCatID]
FROM [my_database].[dbo].[tblCs dOrgToCats ]
To clarify;
tblCsdOrg.teOrgGUID = tblCsdOrgToCats.teOrgAsOrg GUID
I'd like guidance on the most efficient query to list all Organisations in the Category 'CAT-CSD-DBH-30-14-85', Ordered by teOrgTitle
Thanks for your help.
Sample MSSQL Data Attached
ee_csd_sample.sql
I have a table of Organisations, each organisation is associated with a primary category via a field 'teOrgCategory'
SELECT [teOrgTitle]
,[teOrgGUID]
,[teOrgCategory]
FROM [my_database].[dbo].[tblCs
WHERE teOrgCategory = 'CAT-CSD-DBH-30-14-85'
So I can also associate other Organisations with additional categories I have created another table that allows additional associations between organisations and categories
SELECT [teOrgAsOrgGUID]
,[teOrgAsOrgCatID]
FROM [my_database].[dbo].[tblCs
To clarify;
tblCsdOrg.teOrgGUID = tblCsdOrgToCats.teOrgAsOrg
I'd like guidance on the most efficient query to list all Organisations in the Category 'CAT-CSD-DBH-30-14-85', Ordered by teOrgTitle
Thanks for your help.
Sample MSSQL Data Attached
ee_csd_sample.sql
ASKER
Let's say I want to show all organisations associated with the category CAT-CSD-DBH-30-14-85
So I can find any this way.
SELECT [teOrgTitle]
,[teOrgGUID]
,[teOrgCategory]
FROM [test].[dbo].[tblCsdOrg]
WHERE teOrgCategory = 'CAT-CSD-DBH-30-14-85'
This finds the Organisation CSD-TCQ-35-20-81
However, I also need to add in the additional Organisations linked via tblCsdOrgToCats
SELECT [teOrgAsID]
,[teOrgAsOrgGUID]
,[teOrgAsOrgCatID]
FROM [test].[dbo].[tblCsdOrgToC ats]
WHERE teOrgAsOrgCatID = 'CAT-CSD-DBH-30-14-85'
This gives me these additional Organisations
CSD-AVI-78-53-34
CSD-ASN-48-22-04
CSD-HSS-11-05-76
CSD-NTO-23-17-88
So basically I want a single query where I input CAT-CSD-DBH-30-14-85 and I get the equivalent output to running
SELECT [teOrgTitle]
,[teOrgGUID]
,[teOrgCategory]
FROM [test].[dbo].[tblCsdOrg]
WHERE teOrgGUID IN ('CSD-TCQ-35-20-81','CSD-A VI-78-53-3 4','CSD-AS N-48-22-04 ','CSD-HSS -11-05-76' ,'CSD-NTO- 23-17-88')
ORDER BY teOrgTitle ASC
So I can find any this way.
SELECT [teOrgTitle]
,[teOrgGUID]
,[teOrgCategory]
FROM [test].[dbo].[tblCsdOrg]
WHERE teOrgCategory = 'CAT-CSD-DBH-30-14-85'
This finds the Organisation CSD-TCQ-35-20-81
However, I also need to add in the additional Organisations linked via tblCsdOrgToCats
SELECT [teOrgAsID]
,[teOrgAsOrgGUID]
,[teOrgAsOrgCatID]
FROM [test].[dbo].[tblCsdOrgToC
WHERE teOrgAsOrgCatID = 'CAT-CSD-DBH-30-14-85'
This gives me these additional Organisations
CSD-AVI-78-53-34
CSD-ASN-48-22-04
CSD-HSS-11-05-76
CSD-NTO-23-17-88
So basically I want a single query where I input CAT-CSD-DBH-30-14-85 and I get the equivalent output to running
SELECT [teOrgTitle]
,[teOrgGUID]
,[teOrgCategory]
FROM [test].[dbo].[tblCsdOrg]
WHERE teOrgGUID IN ('CSD-TCQ-35-20-81','CSD-A
ORDER BY teOrgTitle ASC
not sure if this is what you want:
SELECT [teOrgTitle]
,[teOrgGUID]
,[teOrgCategory]
,[teOrgAsOrgCatID]
FROM [test].[dbo].[tblCsdOrg]
JOIN [test].[dbo].[tblCsdOrgToCats] on [test].[dbo].[tblCsdOrg].teOrgCategory=[test].[dbo].[tblCsdOrgToCats].teOrgAsOrgCatID
WHERE teOrgCategory = 'CAT-CSD-DBH-30-14-85'
ORDER BY teOrgTitle ASC
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Sonic1234,
Any luck the solution i gave or do you need any more changes in that ?
Any luck the solution i gave or do you need any more changes in that ?
ASKER
Thanks that worked perfectly.
Welcome, Glad to help!
Open in new window
Above is some sample data from each of your tables. So far, I have not been able sense of what exactly you are asking for. Most likely, you have done a very good job describing what you want but I am not able to understand it. Could you please, based on the data above, show what is the output your are looking for. If it helps, use your own sample data for each of these three tables.Thanks,
Mike