I read your question thoroughly and summarized below:
- Child rows may exist before, on, or after Date X
- I want all Parents who's children all have a date on/before Date X
See code below. We use the HAVING statement to make sure children do not have a date after X.
SELECT P.*
FROM Parent P
WHERE P.id IN
(
SELECT C.parentID
FROM Child C
GROUP BY C.parentID
HAVING MAX(CASE WHEN date > '2010-10-13' THEN 1 ELSE 0 END) = 0
/* do not return children that have a date after 2010-10-13 */
)
Sample Schema for those who want to play along. (SQL Server)
("date" is called "mydate" to avoid having to escape the reserved word.)
CREATE TABLE Parent (id INT PRIMARY KEY);
CREATE TABLE Child (id INT IDENTITY PRIMARY KEY, parentID INT NOT NULL REFERENCES Parent(id), mydate DATE );
INSERT INTO Parent VALUES (1);
INSERT INTO Parent VALUES (2);
INSERT INTO Parent VALUES (3);
INSERT INTO Parent VALUES (4);
INSERT INTO Child (parentID, mydate) VALUES (1,'2010-10-11')
INSERT INTO Child (parentID, mydate) VALUES (1,'2010-10-12')
INSERT INTO Child (parentID, mydate) VALUES (1,'2010-10-13')
INSERT INTO Child (parentID, mydate) VALUES (2,'2010-10-12')
INSERT INTO Child (parentID, mydate) VALUES (2,'2010-10-13')
INSERT INTO Child (parentID, mydate) VALUES (2,'2010-10-14')
INSERT INTO Child (parentID, mydate) VALUES (3,'2010-10-14')
INSERT INTO Child (parentID, mydate) VALUES (3,'2010-10-15')
INSERT INTO Child (parentID, mydate) VALUES (3,'2010-10-16')