CREATE TABLE #tblTemplates
(
TemplateID int,
TemplateName varchar(50)
)
GO
INSERT INTO #tblTemplates (TemplateID, TemplateName)
SELECT 1, 'Template 1' UNION ALL
SELECT 2, 'Template 2'
GO
CREATE TABLE #tblBlocks
(
TemplateID int,
BlockID int,
FieldID int,
BaseStage bit
)
GO
INSERT INTO #tblBlocks (TemplateID, BlockID, FieldID, BaseStage)
SELECT 1, 1, 1, 0 UNION ALL
SELECT 1, 1, 2, 0 UNION ALL
SELECT 1, 1, 3, 0 UNION ALL
SELECT 1, 2, 4, 0 UNION ALL
SELECT 1, 2, 5, 1 UNION ALL
SELECT 1, 2, 6, 0 UNION ALL
SELECT 2, 1, 7, 1 UNION ALL
SELECT 2, 1, 8, 0 UNION ALL
SELECT 2, 1, 9, 0 UNION ALL
SELECT 2, 2, 10, 1 UNION ALL
SELECT 2, 2, 11, 0 UNION ALL
SELECT 2, 2, 12, 0
GO
SELECT Distinct #tblTemplates.TemplateName FROM #tblTemplates
INNER JOIN #tblBlocks ON #tblTemplates.TemplateID = #tblBlocks.TemplateID -- plus some more code to satisfy the condition below
DROP TABLE #tblBlocks
DROP TABLE #tblTemplates
A Templates table with two templates in it.
A Blocks table with BlockIDs in it for each template.
Each template has 6 BlockID records in #tblBlocks - in two groups of 3. There are 3 BlockID records with a value of 1 and 3 with a value of 2 for each template.
When I run the statement SELECT * FROM #tblTemplates ... I only want Templates returned that, in the Blocks table, each group of BlockIDs has at least one record with a value of 1 for BaseStage.
So, if I run the Select statement to return a list of Templates using the data above, I only want to see Template 2 returned. Template 1 does not qualify because it has a group of Block IDs none of which have a value of 1 for BaseStage.
Thanks again for your help.