这个句语能写得简单点吗?
表jblx
jblx_dm jblx_je
01 5
02 10
03 15
想得到结果
z x f
5 10 15
select sum(z) z,sum(x) x,sum(f) f from (
select case when jblx_dm= '01 ' then jblx_je end as z,case when jblx_dm= '02 ' then jblx_je end as x,case when jblx_dm= '03 ' then jblx_je end as f
from jblx) a
这条语句能写的简单点吗?
------解决方案--------------------還有一種寫法
select
sum(case when jblx_dm= '01 ' then jblx_je Else 0 end) as z,
sum(case when jblx_dm= '02 ' then jblx_je Else 0 end) as x,
sum(case when jblx_dm= '03 ' then jblx_je Else 0 end) as f
from jblx
不過是差不多的
------解决方案--------------------declare @d varchar(1000)
set @d= ' '
select @d=@d+ ', ' ' '+jb1+ ' ' '= ' +cast(jb2 as varchar(20)) from tb
set @d=stuff(@d,1,1, ' ')
exec ( 'select ' + @d )