我有一个表sql server如下,从那个表我想得到quesno,字段名称[该字段有什么价值]
QuesNo A B C D
1 1 0 1 0
2 0 0 0 1
输出
QuesNo Result
1 A,C
2 D
有没有可能获得outpu的方法?
答案 0 :(得分:1)
应该这样做......
SELECT QuesNo, SUBSTRING(Answers, 1, LENGTH(Answers) - 1) AS Answers
FROM (
SELECT QuesNo,
CASE
WHEN A <> 0 THEN 'A,'
ELSE ''
END +
CASE
WHEN B <> 0 THEN 'B,'
ELSE ''
END +
CASE
WHEN C <> 0 THEN 'C,'
ELSE ''
END +
CASE
WHEN D <> 0 THEN 'D,'
ELSE ''
END AS Answers
FROM yourtable
) Foo