【问题标题】:T-SQL 2008- Get the highest pathT-SQL 2008-获取最高路径
【发布时间】:2017-02-08 01:22:03
【问题描述】:

我有一个表,其中有一个名为 PATH 的字段;该字段的格式如下(每条记录都是一个新行):

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

...

我想为每个用户获取最高的文件夹。对于这个例子,我想得到:

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

【问题讨论】:

    标签: tsql substring string-length


    【解决方案1】:

    我并不是说您应该或不应该重组数据,但如果数据是分层的,这将更容易解决 - 无论是使用规范化还是分层数据类型。但是......只是偷偷摸摸:

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

    (以及您需要的任何分组等)

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多