一个sql不可以吧
每个写一个吧
在jsp上分开写也很简单啊
主要你这省和地区有办法区分吗?
字符串长度?
按照省统计一下种类为A,种类为B,种类为C的数目 count groupby~和按地区分不同种类的总数 count groupby不按地区分按种类分种类A,B,C的总数 count union整体的总数量 上面相加就好了阿

解决方案 »

  1.   

    具体sql语句怎么写啊??真的不太懂!!谢谢哥哥了哦!!
      

  2.   

    我在数据库里边的地区是用代码存的,比如江苏省是01,北京是02,河北是03,河北下面的省市是0301,0302,...,这样还可以用一个sql搞定啊???
      

  3.   

    是这样的!!
    地区类别 种类A数目 种类B的数目      种类c的数目    总数(不同种类)  
      江苏      2          1                 0             3    
      北京      1          1                 0             2   
      河北      0          0                 1             1
                     
      总数      3          2                 1             6
     
    从一张表tableA中统计出来
    tableA中字段:
     地区名称                种类A   种类B   种类c
       0101(代表江苏南京)      1       1       0
       0102(江苏苏州)          1       0       0
       02(北京)                1       1       0
       03(河北)               0       0       1
    然后按照省统计一下种类为A,种类为B,种类为C的数目,和按地区分不同种类的总数,
    和不按地区分按种类分种类A,B,C的总数和整体的总数量,一个sql可以吗??
      

  4.   

    这个相当复杂
    假设你的property表中保存地区的名称(name)/代码(code)对,
    tableA表结构code(地区编码),typeA(种类A),typeB(种类B),typeC(种类C)
    整个汇总的过程是这样的:
    1、先按地区汇总各个类别的总数,得到三张表,并且这里如果某地区没有某个种类,则会导致空记录,因此要做处理,转换成汇总数为0
    2、连接这三张汇总表得到三类的总汇总表
    3、连接地区名称表(property)得到地区的名称
    4、统计总的类别数
    5、两个结果集合并SQL语句如下:
    select property.name,dbf.typeAC,dbf.typeBC,dbf.typeCC,dbf.total from 
    (
    select dbfA.code,dbfA.typeAC,dbfB.typeBC,dbfC.typeCC,(dbfA.typeAC+dbfB.typeBC+dbfC.typeCC) as total from 
    (select code,count(typeA) as typeAC from tableA where typeA=true group by code
    union select code,0 as typeAC from tableA where typeA=false group by code
    ) as dbfA,
    (select code,count(typeB) as typeBC from tableA where typeB=true group by code
    union select code,0 as typeBC from tableA where typeB=false group by code
    ) as dbfB,
    (select code,count(typeC) as typeCC from tableA where typeC=true group by code
    union select code,0 as typeCC from tableA where typeC=false group by code
    ) as dbfC
    where dbfA.code=dbfB.code and dbfB.code=dbfC.code
    ) as dbf left join property on property.code=dbf.code以上SQL得到的结果集不包含行汇总数,因此还需要在该结果集上,再联合一个汇总的结果集
    select '总数',dbfA.typeAC,dbfB.typeBC,dbfC.typeCC,(dbfA.typeAC+dbfB.typeBC+dbfC.typeCC) as total from 
    (select count(typeA) as typeAC from tableA where typeA=true ) as dbfA,
    (select count(typeB) as typeBC from tableA where typeB=true ) as dbfB,
    (select count(typeC) as typeCC from tableA where typeC=true ) as dbfC
    因此,完整的SQL语句就应该是:
    select property.name,dbf.typeAC,dbf.typeBC,dbf.typeCC,dbf.total from 
    (
    select dbfA.code,dbfA.typeAC,dbfB.typeBC,dbfC.typeCC,(dbfA.typeAC+dbfB.typeBC+dbfC.typeCC) as total from 
    (select code,count(typeA) as typeAC from tableA where typeA=true group by code
    union select code,0 as typeAC from tableA where typeA=false group by code
    ) as dbfA,
    (select code,count(typeB) as typeBC from tableA where typeB=true group by code
    union select code,0 as typeBC from tableA where typeB=false group by code
    ) as dbfB,
    (select code,count(typeC) as typeCC from tableA where typeC=true group by code
    union select code,0 as typeCC from tableA where typeC=false group by code
    ) as dbfC
    where dbfA.code=dbfB.code and dbfB.code=dbfC.code
    ) as dbf left join property on property.code=dbf.code
    union
    select '总数',dbfA.typeAC,dbfB.typeBC,dbfC.typeCC,(dbfA.typeAC+dbfB.typeBC+dbfC.typeCC) as total from 
    (select count(typeA) as typeAC from tableA where typeA=true ) as dbfA,
    (select count(typeB) as typeBC from tableA where typeB=true ) as dbfB,
    (select count(typeC) as typeCC from tableA where typeC=true ) as dbfC
      

  5.   

    用两个可以解决。
    因为最下边的总数要单独统计一次select count(种类A) xa,count(种类B) xb,count(种类C) xc from tableA group by 地区名
    select count(种类A) xa,count(种类B) xb,count(种类C) xc from tableA
      

  6.   

    不知道你用的是什么数据库?
    是SQL Server里面可以这样做
    SELECT  LEFT(area, 2) AS area, 
            SUM(CAST(A AS int)) AS a, 
            SUM(CAST(B AS int)) AS b, 
            SUM(CAST(C AS int)) AS c, 
            (SUM(CAST(A AS int)) + SUM(CAST(B AS int)) + SUM(CAST(C AS int))) SUM
    FROM area
    GROUP BY LEFT(area, 2)
    查出来就是
    area  a    b    c    sum
    01    2    1    0    3    
    02    1    1    0    2   
    03    0    0    1    1
    然后再怎么写你就应该会了吧
      

  7.   

    对应的表为:
    CREATE TABLE [dbo].[area] (
    [area] [varchar] (50) NULL ,
    [A] [bit] NULL ,
    [B] [bit] NULL ,
    [C] [bit] NULL 
    ) ON [PRIMARY]
    GO
      

  8.   

    SELECT  LEFT(area, 2) AS area,
            SUM(CAST(A AS int)) AS a,
            SUM(CAST(B AS int)) AS b, 
            SUM(CAST(C AS int)) AS c,
            (SUM(CAST(A AS int)) + SUM(CAST(B AS int)) + SUM(CAST(C AS int))) SUM
    FROM area
    GROUP BY LEFT(area, 2)
    UNION
    SELECT  '总数',
            SUM(t.a),
            SUM(t.b),
            SUM(t.c),
            SUM(t.SUM)
    FROM (
        SELECT  LEFT(area, 2) AS area,
                SUM(CAST(A AS int)) AS a,
                SUM(CAST(B AS int)) AS b, 
                SUM(CAST(C AS int)) AS c,
                (SUM(CAST(A AS int)) + SUM(CAST(B AS int)) + SUM(CAST(C AS int))) SUM
        FROM area
        GROUP BY LEFT(area, 2)
    ) t
    出来的就是:
    area  a    b    c    sum
    01    2    1    0    3
    02    1    1    0    2
    03    0    0    1    1
    总数  3    2    1    6
      

  9.   

    基本上同意 fosking(寒羽良天)但没有测试过,只有楼上自己试试了
      

  10.   

    上面的可以用,但是有一个问题,就是如果有一个数据为空的话,它的行统计出来就是空的!
    列统计是正确的,我发现那个列是字符型的,不是boolean型的,所以to_number的时候如果以前是空字符窜,它就不统计了!!