Skip to content
Advertisement

In SQL, how do I filter a parent with its child records if either satisfies a condition

I have the following table as example. I want to retrieve only the records where the ScheduleDate is smaller than 2022-01-03 (Jan 3, 2022) for either the parent and child records but I want the whole family only (parents and children together). With this set of records, the results should return the records 1 to 9 only. ScheduleId 12 does not satisfy the condition and has relationship with 10 and 13. 10 has a relationship with 11.

Advertisement

Answer

I must say that your problem definition is a bit ambiguous. In writhing this solution I had to make the assumption that you expect to see a family where there are no children who have a ScheduleDate greater than 2022-01-03. If that is not the solution you looking for please clarify your problem definition. Here we go 😀

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement