当前位置:首页 » 编程语言 » sql行列转换动态

sql行列转换动态

发布时间: 2023-06-09 23:55:09

sql server 行转列

创建测试表

createtabletest
(_keyvarchar(10),
_valuevarchar(10),
idint)

insertintotestvalues('ceshi','测试值',10)
insertintotestvalues('ceshi','测试值',11)
insertintotestvalues('ceshi2','测试值2',11)

执行

declare@sqlvarchar(4000)
set@sql='selectid'
select@sql=@sql+',max(case_keywhen'''+[_key]+'''then[_value]end)as
['+_key+']'
from(selectdistinct_keyfromtest)asa
select@sql=@sql+'fromtestgroupbyid'
exec(@sql)

结果


额,我那个第三条数据id写错了,不过方法还是这个

⑵ sql 2000 结合两张表“动态”实现 行转列

select
bdate,
max(case when area='保养一区' then 1 else 0 end),
max(case when area='保养二区' then 1 else 0 end),
max(case when area='保养三区' then 1 else 0 end),
……
from tablename
group by bdate

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:576
制作脚本网站 发布:2025-10-20 08:17:34 浏览:867
python中的init方法 发布:2025-10-20 08:17:33 浏览:564
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:746
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:667
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:988
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:236
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:96
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:789
python股票数据获取 发布:2025-10-20 07:39:44 浏览:694