2016-09-29 1 views
0

Ich habe eine Tabelle mit einem Feld namens PATH; Dieses Feld hat das folgende Format (jeder Datensatz ist eine neue Reihe):T-SQL 2008- Ermitteln Sie den höchsten Pfad

PartitionA \ PathA 
PartitionA \ PathA \ Location 
PartitionA \ PathA \ Location \ Share 
PartitionA \ PathA \ Location \ Share \ FolderUserA 
PartitionA \ PathA \ Location \ Share \ FolderUserA \ fileA.txt 
PartitionA \ PathB 
PartitionA \ PathB \ Location 
PartitionA \ PathB \ Location \ Share 
PartitionA \ PathB \ Location \ Share \ FolderUserB 
PartitionA \ PathB \ Location \ Share \ FolderUserB \ fileB.txt 

...

Ich mag die höchsten Ordner für jeden Benutzer erhalten. In diesem Beispiel möchte ich erhalten:

PartitionA \ PathB \ Location \ Share \ FolderUserA 
PartitionA \ PathB \ Location \ Share \ FolderUserB 

Antwort

2

Ich sage nicht, Sie sollten oder die Daten nicht neu strukturieren, aber das wäre einfacher zu lösen, wenn die Daten hierarchisch sind - entweder mit Normalisierung oder den hierarchischen Datentyp. Allerdings ... wäre es hinterhältig sein, nur:

order by len(path) - len(replace(path,' \ ','') desc 

(zusammen mit dem, was Gruppierung usw. Sie brauchen)

Verwandte Themen