当前位置: 代码迷 >> Sql Server >> 求一条行列转换的SQL,大家帮小弟我写下
  详细解决方案

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

热度:96   发布时间:2016-04-24 21:38:13.0
求一条行列转换的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
  相关解决方案