问题描述
在我的数据库中,有七个列,分别为值"Y"和"N".我想取值为"Y"的列标题",并用逗号将其连接起来.谁能帮我解决这个问题.我的要求是这样的:
列标题:TE HE FE RE YE WE DS
列值:Y N N Y N Y Y
N N N N Y N N
OutPut应该是这样的:
第一行:TE,RE,WE,DS
第二排:YE
在Advance中致谢
Hi,
In my database there are seven columns with the value "Y" and "N". I want to take the "Column Heading" Which the values are "Y" and I want to Concatenate it with Comma. Can any one help me to solve this problem. My requirement is like this:
Column Header: TE HE FE RE YE WE DS
Column Values: Y N N Y N Y Y
N N N N Y N N
OutPut Should be Like this:
for First Row: TE,RE,WE,DS
for Second Row: YE
Thanks in Advance
推荐答案
select case
when len([ekeke]) > 0 then (left([ekeke], len(ekeke) - 1))
else [ekeke]
end /* case */ as 'ekeke'
from
(
select
case when [TE]='Y' then 'te,' else '' end +
case when [HE]='Y' then 'he,' else '' end +
case when [FE]='Y' then 'fe,' else '' end +
case when [RE]='Y' then 're,' else '' end +
-- ... more lines here ...
case when [DS]='Y' then 'ds,' else '' end
as 'ekeke'
) v
希望这会有所帮助,
巴勃罗.
Hope this helps,
Pablo.
Select TE +'',''+RE+'',''+WE+'',''+DS from your_table
union
SElect YE from your_table
这篇关于如何将多列值连接为单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!