对于Excel导出,我必须为每个项目添加两列,其中包含所有“has-reference-to”和“is-referenced-from”项目。
Here is my SQL Fiddle with my tables and my results.
我的表格
CREATE TABLE Item (ItemId INT, Title NVARCHAR(100))
INSERT INTO Item VALUES(1, 'Title 1')
INSERT INTO Item VALUES(2, 'Title 2')
INSERT INTO Item VALUES(3, 'Title 3')
INSERT INTO Item VALUES(4, 'Title 4')
INSERT INTO Item VALUES(5, 'Title 5')
CREATE TABLE ItemReference (ReferenceId INT, FromItemId INT, ToItemId INT)
INSERT INTO ItemReference VALUES(1, 1, 2)
INSERT INTO ItemReference VALUES(2, 1, 3)
INSERT INTO ItemReference VALUES(3, 4, 5)
INSERT INTO ItemReference VALUES(4, 5, 1)
FromItemId
和ToItemId
都包含表ItemId
中的Item
。
预期输出
+---------+------------------+--------------------+
| Title | Has reference to | Is referenced from |
+---------+------------------+--------------------+
| Title 1 | Title 2, Title 3 | Title 5 |
+---------+------------------+--------------------+
| Title 2 | | Title 1 |
+---------+------------------+--------------------+
| Title 3 | | Title 1 |
+---------+------------------+--------------------+
| Title 4 | Title 5 | |
+---------+------------------+--------------------+
| Title 5 | Title 1 | Title 4 |
+---------+------------------+--------------------+
我的查询
SELECT item.Title,
ISNULL([Has reference to], '') [Has reference to],
ISNULL([Is referenced from], '') [Is referenced from]
FROM Item item
OUTER APPLY
(
SELECT i.Title + ', '
FROM ItemReference ref
INNER JOIN Item i ON ref.FromItemId = i.ItemId
WHERE ref.FromItemId = item.ItemId
ORDER BY ref.ToItemId DESC
FOR XML PATH('')
) OURT ([Has reference to])
OUTER APPLY
(
SELECT i.Title + ', '
FROM ItemReference ref
INNER JOIN Item i ON ref.ToItemId = i.ItemId
WHERE ref.ToItemId = item.ItemId
ORDER BY ref.FromItemId DESC
FOR XML PATH('')
) OURF ([Is referenced from])
当前输出
+---------+------------------+--------------------+
| Title | Has reference to | Is referenced from |
+---------+------------------+--------------------+
| Title 1 | Title 1, Title 1 | Title 1 |
+---------+------------------+--------------------+
| Title 2 | | Title 2 |
+---------+------------------+--------------------+
| Title 3 | | Title 3 |
+---------+------------------+--------------------+
| Title 4 | Title 4 | |
+---------+------------------+--------------------+
| Title 5 | Title 5 | Title 5 |
+---------+------------------+--------------------+
正如您所看到的,项目引用的数量与我的预期结果相符,但我无法获得引用的正确标题。谁能帮我拿到正确的头衔?
答案 0 :(得分:1)
很抱歉,我的无知是ref没有项目列,没有数据样本我看不到我想到的树:)
SELECT item.Title,
ISNULL([Has reference to], '') [Has reference to],
ISNULL([Is referenced from], '') [Is referenced from]
FROM @Item item
OUTER APPLY
(
SELECT i.Title + ', '
FROM @ItemReference ref
INNER JOIN @Item i ON ref.ToItemId = i.ItemId
WHERE ref.FromItemId = item.ItemId
ORDER BY ref.ToItemId DESC
FOR XML PATH('')
) OURT ([Has reference to])
OUTER APPLY
(
SELECT i.Title + ', '
FROM @ItemReference ref
INNER JOIN @Item i ON ref.FromItemId = i.ItemId
WHERE ref.ToItemId = item.ItemId
ORDER BY ref.FromItemId DESC
FOR XML PATH('')
) OURF ([Is referenced from]);
链接应该是ref.To和ref.From(ref.From和ref.To)。
编辑:BTW,因为它的顺序是标题3,标题2.我不确定它是否是你想要的。