在oracle数据库中
统计人员表的籍贯人员表 table_persid province dept
001 北京 A
002 上海 A
003 深圳 A
004 广州 A
005 北京 B
006 北京 B
007 上海 C
008 上海 C想得到像这样的关于籍贯的统计结果province total dept_A dept_B dept_C
北京          3 1 2 0
上海          3 1 0 2
深圳          1 1 0 0
广州          1 1 0 0sql语句应该怎样写
谢谢大家

解决方案 »

  1.   

    select province,
           count(1) total,
           sum(decode(dept, A, 1, 0)) dept_A,
           sum(decode(dept, B, 1, 0)) dept_B,
           sum(decode(dept, C, 1, 0)) dept_C
      from table_pers
     group by province
      

  2.   

    select province,count(*) total ,sum(decode(dept,'A',1,0)) dept_A,
           sum(decode(dept,'B',1,0)) dept_B,
           sum(decode(dept,'C',1,0)) dept_C
    from 表名
    group by province
      

  3.   

    FYI:http://topic.csdn.net/u/20080918/11/6fa9bc6c-efc3-4ee2-95d0-02a989d9de0e.html
      

  4.   

    谢谢各位高手
    再问一个问题
    这种方式的查询支持对部门(dept)的模糊搜索吗
      

  5.   

    比如说dept字段包含某个字符串
      

  6.   

    有个like '%...'的。可以试下。没明白你说的意思。
      

  7.   

    select province,count(*) total ,sum(case when instr(dept,'包含的')>0 then 1 else 0 end) dept_A,
           sum(case when instr(dept,'包含的')>0 then 1 else 0 end) dept_B,
           sum(case when instr(dept,'包含的')>0 then 1 else 0 end) dept_C
    from 表名
    group by province