请问大家我想要得到如下的结果该怎么写吖? 我是小白。。。
select count(*) as 用户总数 from tb_user_dvbinfoselect count(*) as 塔城市 from tb_user_dvbinfo where areaid=00select count(*) as 额敏县 from tb_user_dvbinfo where areaid=01select count(*) as 托里县 from tb_user_dvbinfo where areaid=02select count(*) as 裕民县 from tb_user_dvbinfo where areaid=03select count(*) as 特殊用户 from tb_user_dvbinfo where areaid=04
create table tb_user_dvbinfo(areaid varchar(20))insert into tb_user_dvbinfoselect '00' union allselect '00' union allselect '01' union allselect '02' union allselect '03' union allselect '03' union allselect '03' union allselect '04' union allselect '04' union allselect '04' select (select count(*) from tb_user_dvbinfo) as 用户总数,(select count(*) from tb_user_dvbinfo where areaid='00') as 塔城市,(select count(*) from tb_user_dvbinfo where areaid='01') as 额敏县,(select count(*) from tb_user_dvbinfo where areaid='02') as 托里县,(select count(*) from tb_user_dvbinfo where areaid='03') as 裕民县,(select count(*) from tb_user_dvbinfo where areaid='04') as 特殊用户into #tselect * from #t/*用户总数 塔城市 额敏县 托里县 裕民县 特殊用户----------- ----------- ----------- ----------- ----------- -----------10 2 1 1 3 3*/drop table #T
[解决办法]
显示为行的话,可以这样:
select case areaid when '00' then '塔城市' when '01' then '额敏县' when '02' then '托里县' when '03' then '裕民县' when '04' then '特殊用户' else isnull(areaid,'用户总数') end, count(*) from tb_user_dvbinfo group by areaid with rollup/*塔城市 2额敏县 1托里县 1裕民县 3特殊用户 3用户总数 10*/