sql >> Database teknologi >  >> RDS >> Sqlserver

SQL:Find manglende mappestier i opdelingshierarkier

Brug af denne tilføjede sti (11,2,'U\V\Z\L\O\Q\R\S\T') for at vise flere manglende mapper i en sti:

with cte as (
select BaseDirID, DisplayPath = left(DisplayPath,len(DisplayPath)-charindex('\',reverse(DisplayPath)))
from t
where DirLevel > 1
  and not exists (
  select 1 
  from t i
  where t.BaseDirId = i.BaseDirId
    and i.DisplayPath = left(t.DisplayPath,len(t.DisplayPath)-charindex('\',reverse(t.DisplayPath)))
    )
union all 
select BaseDirID, DisplayPath = left(DisplayPath,len(DisplayPath)-charindex('\',reverse(DisplayPath)))
from cte t
where not exists (
  select 1 
  from t i
  where t.BaseDirId = i.BaseDirId
    and i.DisplayPath = left(t.DisplayPath,len(t.DisplayPath)-charindex('\',reverse(t.DisplayPath)))                                                   
    )
)
select distinct * 
from cte

rextester demo:http://rextester.com/CEVGZ96613

returnerer:

+-----------+-----------------+
| BaseDirID |   DisplayPath   |
+-----------+-----------------+
|         1 | A\B             |
|         1 | A\B\C\D         |
|         1 | A\B\F\G         |
|         2 | U\V             |
|         2 | U\V\M\L         |
|         2 | U\V\W\X         |
|         2 | U\V\Z           |
|         2 | U\V\Z\L         |
|         2 | U\V\Z\L\O       |
|         2 | U\V\Z\L\O\Q     |
|         2 | U\V\Z\L\O\Q\R   |
|         2 | U\V\Z\L\O\Q\R\S |
+-----------+-----------------+



  1. Er det nyttigt at starte SQL-forespørgslens WHERE-sætning med 1=1 i Oracle?

  2. Hvordan strukturerer man et databaseskema for at tillade 1 i en million tilfælde?

  3. MySQL - BETWEEN vil ikke vælge korrekte resultater

  4. PostgreSQL søg og erstat where condition