读书人

求一条行列转换的SQL,大家帮小弟我写下

发布时间: 2013-07-01 12:33:04 作者: rapoo

求一条行列转换的SQL,大家帮我写下。
数据如下

求一条行列转换的SQL,大家帮小弟我写下

格式转成
Key Value
总数 2169192
素 216
荤 42760


大家帮帮忙,谢谢。语句越简单越好。 行列转换
[解决办法]
select '总数' key,总数 value from 表
union all
select '素',素 from 表
union all
select '荤',荤 from 表
[解决办法]
declare @demo table (su int,hun int ,total int)
insert into @demo(su,hun,total) select 216,42760,2169192

select
SUM(case when total <>0 then (total) else 0 end ) as total,
SUM(case when su <>0 then (su) else 0 end ) as su,
SUM(case when hun <>0 then (hun) else 0 end ) as hun
from @demo


[解决办法]
declare @demo table (su int,hun int ,total int)
insert into @demo(su,hun,total) select 216,42760,2169192

select
SUM(case when total <>0 then (total) else 0 end ) as total,
SUM(case when su <>0 then (su) else 0 end ) as su,
SUM(case when hun <>0 then (hun) else 0 end ) as hun
from @demo

读书人网 >SQL Server

热点推荐