I would like to select all parent rows for which exists a combination of children rows with desired value. The easiest approach is to use multiple exists clauses, but this is not optimal - anyone knows better solution? Here is a SQL fiddle: http://sqlfiddle.com/#!6/8da76/5
Schema:
CREATE TABLE [dbo].[Parents](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[GroupId] [uniqueidentifier] NOT NULL
);
INSERT INTO [Parents] ([GroupId]) VALUES ('00000000-0000-0000-0000-000000000001');
INSERT INTO [Parents] ([GroupId]) VALUES ('00000000-0000-0000-0000-000000000002');
INSERT INTO [Parents] ([GroupId]) VALUES ('00000000-0000-0000-0000-000000000003');
INSERT INTO [Parents] ([GroupId]) VALUES ('00000000-0000-0000-0000-000000000004');
INSERT INTO [Parents] ([GroupId]) VALUES ('00000000-0000-0000-0000-000000000005');
CREATE TABLE [dbo].[Childrens](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[ParentId] [bigint] NOT NULL,
[TypeId] [int] NOT NULL
);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (1, 1);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (1, 2);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (1, 3);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (1, 4);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (1, 5);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (2, 1);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (2, 4);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (2, 5);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (3, 4);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (3, 5);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (3, 6);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (4, 2);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (4, 6);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (5, 1);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (5, 2);
INSERT INTO [Childrens] ([ParentId], [TypeId]) VALUES (5, 3);
Sample query to select all parent rows for which exists a combination of children rows with TypeId = 1 and 2 (query should return parent with Id = 1 and 5):
SELECT *
FROM Parents p
WHERE EXISTS(
SELECT *
FROM Childrens c
WHERE p.Id = c.ParentId
AND c.TypeId = 1 )
AND EXISTS(
SELECT *
FROM Childrens c
WHERE p.Id = c.ParentId
AND c.TypeId = 2 );
Sample query to select all parent rows for which exists children row with TypeId = 4, 5 and 6 (should return parent with Id = 3)
SELECT *
FROM Parents p
WHERE EXISTS(
SELECT *
FROM Childrens c
WHERE p.Id = c.ParentId
AND c.TypeId = 4 )
AND EXISTS(
SELECT *
FROM Childrens c
WHERE p.Id = c.ParentId
AND c.TypeId = 5 )
AND EXISTS(
SELECT *
FROM Childrens c
WHERE p.Id = c.ParentId
AND c.TypeId = 6 );
Number of conditions for children TypeId is variable.
Best Regards!
Count_TypeId_N > 0
as required.