首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请教大家小弟我想要得到如下的结果该如何写吖? 小弟我是小白。

2012-03-13 
请问大家我想要得到如下的结果该怎么写吖?我是小白。。。SQL codeselect count(*) as 用户总数 from tb_user_

请问大家我想要得到如下的结果该怎么写吖? 我是小白。。。

SQL code
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

这是提取出来的用户总数,我想把这些得到的数据插入一张新表中,该怎么写啊?求大家指教

[解决办法]
SQL code
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
[解决办法]
显示为行的话,可以这样:
SQL code
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*/ 

热点排行