【发布时间】:2017-01-20 01:37:00
【问题描述】:
我有下表:
| Article-Material1 | Article-Material2 |
|-------------------|-------------------|
| article001 | article002 |
| article001 | article003 |
| article001 | material001 |
| material001 | |
| article002 | article004 |
| article002 | material002 |
| material002 | |
| article003 | material003 |
| material003 | |
| article004 | material004 |
| material004 | |
| article005 | article010 |
| article005 | article011 |
| article005 | material001 |
| article010 | material005 |
| material005 | |
| article011 | article012 |
| article011 | material004 |
| article011 | material006 |
| material006 | |
| article012 | material002 |
| article012 | material007 |
| material007 | |
我想实现这样的输出:
article001
|- article002
|- article004
|- material004
|- material002
|- article003
|- material003
|- material001
article005
|- article010
|- material005
|- article011
|- article012
|- material002
|- material007
|- material004
|- material006
|- material001
我不知道这是否可以通过 SQL 实现。如果不可能,我还能尝试什么方向正确?
【问题讨论】:
-
该输出在表格形式中的外观如何?还是以这种方式格式化的一个字符串?
-
有可能,但您的输出不是表格。您是否打算让 SQL 打印一些东西?返回 XML?包括空列?重复列值? SQL Server 没有嵌套表的概念(大多数数据库系统没有,有极少数例外)。
-
@Stephen 这是我的许多问题之一。我不知道怎么把它放在桌子上。我需要一行包含每篇主要文章的所有子元素(仅存在于第一个层次结构上的文章)。我需要知道子元素在哪个层次上存在。最终目标是将数据导出到 Excel 文件。在 excel 文件中,我需要上面的输出。
-
@JeroenMostert 我希望有类似嵌套数据的东西。这肯定会使任务变得更容易。
-
换句话说,像
Parent, child1, .... child(n)?
标签: sql sql-server-2008 tsql recursive-query