Forums
This topic is locked
sub-query trouble is making me so mad!
Posted 05 Nov 2004 01:26:22
1
has voted
05 Nov 2004 01:26:22 Andy Smith posted:
This may be a bit of a difficult one to get your head round but I have a database with charities that need to be selected. However some organisations need to be included and some excluded based on categories and then included or excluded again from the resulting dataset..The categories (about 70) are listed in a column relating to the charity like POL, CVA, BSOC etc.
So for example the selection needs to include all with BSOC, CVA then exclude those with POL from the results.
Or exclude all those with CVA then exclude all those with POL.
The current query to include or exclude is:
SELECT DISTINCT(ORG), dbo.categories.Include_exclude, dbo.categoriesmain.ID, dbo.categoriesmain.category, direct_id, org_name
FROM dbo.categoriesmain, dbo.categories, dbo.directory, dbo.selectionstemp
WHERE dbo.categoriesmain.category = dbo.categories.category AND org = dbo.categoriesmain.ID AND org_name > '' GROUP BY dbo.categories.Include_exclude, dbo.categoriesmain.ID, org_name, dbo.categoriesmain.category, direct_id, org HAVING dbo.categories.inc_exc1 = 1
The previous page dynamically gets all categories and then has 2 dropdowns listed by the side.
The first dropdown is include / exclude and the second is also include / exclude.
The result is then stored in a table like:
category inc_exc1 inc_exc2
POL 1 0
CVA 1 1
BSOC 0 1
Basically I need a way to run the same query on a 'child' dataset in one go.
Something like:
SELECT DISTINCT(ORG), dbo.categories.Include_exclude, dbo.categoriesmain.ID, dbo.categoriesmain.category, direct_id, org_name
FROM dbo.categoriesmain, dbo.categories, dbo.directory, dbo.selectionstemp
WHERE dbo.categoriesmain.category = dbo.categories.category AND org = dbo.categoriesmain.ID AND org_name > '' AND direct_id IN (SELECT DISTINCT(ORG), dbo.categories.Include_exclude, dbo.categoriesmain.ID, dbo.categoriesmain.category, direct_id, org_name
FROM dbo.categoriesmain, dbo.categories, dbo.directory, dbo.selectionstemp
WHERE dbo.categoriesmain.category = dbo.categories.category AND org = dbo.categoriesmain.ID AND org_name > '' GROUP BY dbo.categories.Include_exclude, dbo.categoriesmain.ID, org_name, dbo.categoriesmain.category, direct_id, org HAVING dbo.categories.inc_exc1 = 1) GROUP BY dbo.categories.Include_exclude, dbo.categoriesmain.ID, org_name, dbo.categoriesmain.category, direct_id, org HAVING dbo.categories.inc_exc2 = 1
If this makes any sense to anyone!!
Thanks in advance