请教一个较麻烦的sql语句

niuniuma 2004-11-11 03:15:30
select 标签 from ..... where ..........
得到的结果集是

管理 1
经济 1
研发 1
爱情 1
生活 1
朋友1 1
管理 2
经济 2
研发 2
爱情 2
生活 2
朋友1 2

我想得到的是:

1 管理,经济,研发,爱情,生活,朋友1
2 管理,经济,研发,爱情,生活,朋友1

不用游标、临时表,有办法一句话搞定吗?
多谢了!!!
...全文
89 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
adaizi1980 2004-11-11
  • 打赏
  • 举报
回复
假设楼主最上面表的两列名称分别为name和id,表名为table,试试这个:
select id,max(clum2),max(clum3),max(clum4),max(clum5),max(clum6),max(clum7) from
(
select id,
(case when name='管理' then name else null end) as clum2,
(case when name='经济' then name else null end) as clum3,
(case when name='研发' then name else null end) as clum4,
(case when name='爱情' then name else null end) as clum5,
(case when name='生活' then name else null end) as clum6,
(case when name='朋友1' then name else null end) as clum7
from table
) group by id
niuniuma 2004-11-11
  • 打赏
  • 举报
回复
case肯定搞不定拉,不用函数可以吗?
lsxaa 2004-11-11
  • 打赏
  • 举报
回复
用函数行么?
davorsuker39 2004-11-11
  • 打赏
  • 举报
回复
CASE就可以搞定
colinliu 2004-11-11
  • 打赏
  • 举报
回复
不會,不過用臨時表我會實現,幫你頂 。。。。。。。。

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧