select count(decode(substr(家庭住址,1,4 ),'河北',1,0)) as 河北省 
from 病人信息

解决方案 »

  1.   

    select count(*) from 病人信息 where 家庭住址 like '河北%';
      

  2.   

    select count(*) from 病人信息 where 家庭住址 like '%河北省%'
      

  3.   

    为什么一定要用decode 和 substr函数呢?用like不行吗?
    select count(*) from 病人信息
    where 家庭住址 like %'河北'%
      

  4.   

    楼上各位你们都误会了,我是这样子的,我不只是检索河北省的我要检索的是全国的各个省份的.
    select 
    count(decode(substr('家庭住址',1,5),'北京',1,null)) as 北京市,
    count(decode(substr('家庭住址',1,5),'天津',1,null)) as 天津市,
    count(decode(substr('家庭住址',1,5),'上海',1,null)) as 上海市,
    count(decode(substr('家庭住址',1,5),'黑龙',1,null)) as 黑龙江,
    count(decode(substr('家庭住址',1,5),'吉林',1,null)) as 吉林省,
    count(decode(substr('家庭住址',1,5),'辽宁',1,null)) as 辽宁省,
    count(decode(substr('家庭住址',1,5),'河北',1,null)) as 河北省,
    count(decode(substr('家庭住址',1,5),'内蒙',1,null)) as 内蒙古,
    count(decode(substr('家庭住址',1,5),'甘肃',1,null)) as 甘肃省,
    count(decode(substr('家庭住址',1,5),'青海',1,null)) as 青海省,
    count(decode(substr('家庭住址',1,5),'新疆',1,null)) as 新疆,
    count(decode(substr('家庭住址',1,5),'西藏',1,null)) as 西藏,
    count(decode(substr('家庭住址',1,5),'河南',1,null)) as 河南省,
    count(decode(substr('家庭住址',1,5),'山东',1,null)) as 山东省,
    count(decode(substr('家庭住址',1,5),'山西',1,null)) as 山西省,
    count(decode(substr('家庭住址',1,5),'浙江',1,null)) as 浙江省,
    count(decode(substr('家庭住址',1,5),'江西',1,null)) as 江西省,
    count(decode(substr('家庭住址',1,5),'江苏',1,null)) as 江苏省,
    count(decode(substr('家庭住址',1,5),'湖北',1,null)) as 湖北省,
    count(decode(substr('家庭住址',1,5),'湖南',1,null)) as 湖南省,
    count(decode(substr('家庭住址',1,5),'广东',1,null)) as 广东省,
    count(decode(substr('家庭住址',1,5),'福建',1,null)) as 福建省,
    count(decode(substr('家庭住址',1,5),'广西',1,null)) as 广西省,
    count(decode(substr('家庭住址',1,5),'云南',1,null)) as 云南省,
    count(decode(substr('家庭住址',1,5),'香港',1,null)) as 香港,
    count(decode(substr('家庭住址',1,5),'澳门',1,null)) as 澳门,
    count(decode(substr('家庭住址',1,5),'四川',1,null)) as 四川省
    from 病人信息 where 门诊号 is not null只有这样全国的各省份一次才可以全部检索出来的!
    楼上说得也对,是可以,可是要写好多SQL语句才可以检索出来了啊!
    希望大家再给指教!
      

  5.   

    经过努力,这个问题我已经解决了!
    是这样子
    select count(decode(substr('家庭住址',1,5),'河北',1,null)) as 河北省
    from 病人信息 
    改为
    select  count(decode(substr('家庭住址',1,2),'河北',1,null)) as 河北省
    from 病人信息 

    select  count(decode(substrb('家庭住址',1,4),'河北',1,null)) as 河北省
    from 病人信息 问题解决了,谢谢大家参与,开始结贴!
      

  6.   

    count(decode(substr('家庭住址',1,5),'河北',1,null)) as 河北省是
    count(decode(substr('家庭住址',1,4),'河北',1,null)) as 河北省1个汉字2个字节
      

  7.   

    这样也可以:
    select  sum(decode(substrb('家庭住址',1,4),'河北',1,0)) 河北省
    from 病人信息