我有桌子:
NAME FACTOR
say_type1 A
(...)
say_type2 B
(...)
say_type3 A
(...)
say_type3 B
我想得到答案的结果(SELECT name,factor FROM ... WHERE some_condition):
say_type1 A
say_type2 B
say_type3 A,B
我正在尝试使用CASE,但它始终仅用于一条记录。
答案 0 :(得分:0)
我想你想把FACTOR列转换成行。您可以使用XML PATH:
进行制作--F.e. we have this in YourTable
--('say_type1', 'A'),
--('say_type2', 'B'),
--('say_type3', 'A'),
--('say_type3', 'B')
SELECT DISTINCT t1.NAME,
STUFF((SELECT ',' + FACTOR
FROM YourTable
WHERE NAME = t1.NAME
FOR XML PATH('')),1,1,'') as FACTOR
FROM YourTable t1
输出:
NAME FACTOR
say_type1 A
say_type2 B
say_type3 A,B