SQL Server 数据库中行变列的SQL语句写法:
求面积的方法:
select LZND,
sum(case when lzfs='出租' then isnull(JFLZMJM,0)+isnull(YFLZMJM,0) else 0 end) as mjm_cz,
sum(case when lzfs='入股' then isnull(JFLZMJM,0)+isnull(YFLZMJM,0) else 0 end) as mjm_rg,
sum(case when lzfs='转包' then isnull(JFLZMJM,0)+isnull(YFLZMJM,0) else 0 end) as mjm_zb,
sum(case when lzfs='转让' then isnull(JFLZMJM,0)+isnull(YFLZMJM,0) else 0 end) as mjm_zr,
sum(case when lzfs='互换' then isnull(JFLZMJM,0)+isnull(YFLZMJM,0) else 0 end) as mjm_hh,
sum(case when lzfs='其他' then isnull(JFLZMJM,0)+isnull(YFLZMJM,0) else 0 end) as mjm_qt
from lz_ht_base
group by LZND
求个数的方法:
select LZND,
sum(case when lzfs='出租' then 1 else 0 end) as gs_cz,
sum(case when lzfs='入股' then 1 else 0 end) as gs_rg,
sum(case when lzfs='转包' then 1 else 0 end) as gs_zb,
sum(case when lzfs='转让' then 1 else 0 end) as gs_zr,
sum(case when lzfs='互换' then 1 else 0 end) as gs_hh,
sum(case when lzfs='其他' then 1 else 0 end) as gs_qt
from lz_ht_base
group by LZND
select LZND,lzfs from lz_ht_base
----the---end----