Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 如何在SQL Server 2008中展平类别层次结构?_Sql Server 2008 - Fatal编程技术网

Sql server 2008 如何在SQL Server 2008中展平类别层次结构?

Sql server 2008 如何在SQL Server 2008中展平类别层次结构?,sql-server-2008,Sql Server 2008,我有一个包含类别层次结构的表。它可以有任意数量的类别级别 我需要以简单的字符串格式显示这些类别,即: >>parent>>child1>>subchild>>... 这是数据示例: Id Parent Name 1 NULL Categories 4 NULL Instrument 55 NULL Genre 65 NULL Geographical Place 8 1 CLASSICAL 47

我有一个包含类别层次结构的表。它可以有任意数量的类别级别

我需要以简单的字符串格式显示这些类别,即:

>>parent>>child1>>subchild>>...
这是数据示例:

Id  Parent  Name
1   NULL    Categories
4   NULL    Instrument
55  NULL    Genre
65  NULL    Geographical Place 
8   1   CLASSICAL
47  1   SOLO INSTRUMENTS
10694   1   STYLES
4521    4   Piano
1137    8   SOLO INSTRUMENTS
1140    8   WALTZES
841 47  PIANO
93328   55  CLASSICAL
93331   55  BLUES
93334   55  CLUB / ELECTRONICA
93339   55  CONTEMPORARY FOLK
93344   55  CHILDREN
94892   65  EUROPE
4180    10694   CLASSICAL - SOLO PIANO
94893   94892   Western & Southern  Europe
94900   94893   France

您的数据看起来有4个类别,因此您可以尝试以下方法:

    SELECT parent.Name, child.Name, subchild.Name, subchild2.Name
    FROM categories parent
    LEFT JOIN categories child ON parent.Id = child.Parent
    LEFT JOIN categories subchild ON child.Id = subchild.Parent
    LEFT JOIN categories subchild2 ON subchild.Id = subchild2.Parent
    WHERE parent.Parent is NULL;

当然,如果您最终拥有超过4个级别的类别,则需要对其进行调整。此方法还将最终为没有扩展到所有4个级别的层次结构生成空结果。

也许有人有更好的方法来完成最后一部分(去掉中间字符串),但这可以让您不用硬编码任何连接:

declare @Categories table
(
    categoryID int,
    categoryParentID int,
    categoryDesc nvarchar(100)
)
insert into @Categories values
(1,NULL,'Categories'),
(4,NULL,'Instrument'),
(55,NULL,'Genre'),
(65,NULL,'Geographical Place '),
(8,1,'CLASSICAL'),
(47,1,'SOLO INSTRUMENTS'),
(10694,1,'STYLES'),
(4521,4,'Piano'),
(1137,8,'SOLO INSTRUMENTS'),
(1140,8,'WALTZES'),
(841,47,'PIANO'),
(93328,55,'CLASSICAL'),
(93331,55,'BLUES'),
(93334,55,'CLUB / ELECTRONICA'),
(93339,55,  'CONTEMPORARY FOLK'),
(93344,55,'CHILDREN'),
(94892,65,'EUROPE'),
(4180,10694,'CLASSICAL - SOLO PIANO'),
(94893,94892,'Western & Southern  Europe'),
(94900,94893,'France')

;with CategoryCTE (categoryID, categoryString) as
(
    select categoryID, cast(categoryDesc as nvarchar(max)) as categoryString
    from @Categories 
    where categoryParentID is null
    
    union all
    
    select rsCat.categoryID, cast(CategoryCTE.categoryString + N'>>' + rsCat.categoryDesc as nvarchar(max))
    from @Categories rsCat
    inner join CategoryCTE on rsCat.categoryParentID = CategoryCTE.categoryID
)
select categoryString 
from CategoryCTE o
where not exists
(
    --eliminate intermediate strings
    select i.categoryString
    from CategoryCTE i
    where LEN(i.categoryString) != LEN(o.categoryString)
    and CHARINDEX(o.categoryString, i.categoryString) > 0
)

什么应用程序语言?如果是C#/VB,您是否能够使用
LINQ
?如果是这样的话,你可以这样做:用SQL Server 2008为你的数据层次结构建模:必须在一个函数中从TSQL完成,我写了一个cte,它在上面的表格布局中收集数据。我似乎不知道如何以线性形式显示。米奇·麦特-谢谢,这是一篇很棒的文章。让我更接近一个解决方案。谢谢,我将尝试一下,因为它看起来比我目前拥有的更有效。如果MS增加了对递归函数的支持,这将更容易。