我有一张桌子:
-----------
-- Testy --
-----------
Dimension Attribute Value
---------- ---------- ------
PRODUCT Name T-Shirt
PRODUCT Name Pants
PRODUCT Name Sock
PRODUCT Name Wtf
STORE Name J-Store
STORE Name K-Store
STORE City Gotham
我试图使用FOR XML PATH
进行值的串联以得到如下结果:
Dimension Attribute Value
---------- ---------- ------
PRODUCT Name in ('T-Shirt', 'Pants', 'Sock', 'Wtf')
STORE Name in ('J-Store', 'K-Store')
STORE City in ('Gotham')
“ Testy”表是动态的,并且每天都有不同的值。
我的查询是下一个:
select
[Dimension],
[Attribute],
[Value] = STUFF((
SELECT N'', ' ,' + [AttributeValue] FROM Testy x
WHERE [Dimension] = x.[Dimension] and [Attribute] = x.[Attribute]
FOR XML PATH(''), TYPE).value(N'.[1]', N'nvarchar(max)'), 1, 2, N'')
from Testy
group by [Dimension], [Attribute]
我做错了什么?
答案 0 :(得分:3)
您没有限定列名,而是优先考虑内部查询列。
git clean -dfx