문제

So I have a database where each record has a reference to a parent record in the same table (possibly null). Each record also has an 'active' flag that describes whether the single record is currently active.

I would like to end up with a flat list of records that have the following traits:

  • Is active
  • Contains no inactive ancestors

Instead of constructing the tree and doing a search to filter out the records I would like to accomplish this in SQL. Is there a way to join or aggregate to filter out the undesired records?

올바른 솔루션이 없습니다

다른 팁

You can achieve this using CTEs and recursive querying (if your database system supports it).

There is a SQL Server example here and a Postgres example here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top