try:
--------------------------------------------------------------------------------
select
    (case when b.description then '小计' else d.description end) as B级部门名称,
    b.description as 类型,
    count(*)      as 纪录条数
from
    inner_charge a,
    charge_type  b,
    (select * from inner_cust where cust_level = 'C') c,
    (select * from inner_cust where cust_level = 'B') d
where
    a.charge_type = b.charge_type
    and
    a.cust_num    = c.cust_num
    and
    c.super_cust  = d.cust_num
group by
    d.description,b.description
with rollup 
having 
    grouping(d.description)=0

解决方案 »

  1.   

    select A.cust_level as 'B级部门名称'
           ,C.description as '类型'
           ,sum(B.num) as '纪录条数'
    from inner_cust A
    left join (
                select cust_num
                       ,charge_type
                       ,count(1) as 'num'
                from inner_charge 
                group by cust_num,charge_type
              )B on A.cust_num=B.cust_num
    left join charge_type C on C.charge_type=B.charge_type
    where A.cust_level='B'
    group by A.cust_level
             ,C.description
    with rollup
      

  2.   

    select (case when grouping(C.description)=1 
                 then '小计' 
                 else A.cust_level
            end
           ) as 'B级部门名称'
           ,C.description as '类型'
           ,sum(B.num) as '纪录条数'
    from inner_cust A
    left join (
                select cust_num
                       ,charge_type
                       ,count(1) as 'num'
                from inner_charge 
                group by cust_num,charge_type
              )B on A.cust_num=B.cust_num
    left join charge_type C on C.charge_type=B.charge_type
    where A.cust_level='B'
    group by A.cust_level
             ,C.description
    with rollup
      

  3.   

    select (case when grouping(C.description)=1 
                 then '小计' 
                 else A.cust_level
            end
           ) as 'B级部门名称'
           ,C.description as '类型'
           ,sum(B.num) as '纪录条数'
    from inner_cust A
    left join (
                select cust_num
                       ,charge_type
                       ,count(1) as 'num'
                from inner_charge 
                group by cust_num,charge_type
              )B on A.cust_num=B.cust_num
    left join charge_type C on C.charge_type=B.charge_type
    where A.cust_level='B'
    group by A.cust_level
             ,C.description
    with rollup
    ====
    好象不对!!!
      

  4.   

    =============SELECT (CASE WHEN grouping(C.description) = 1 THEN '小计' ELSE A.cust_level END) 
          AS '部门名称', C.description AS '类型', SUM(B.num) AS '纪录条数'
    FROM inner_cust A LEFT JOIN
              (SELECT to_kebie, charge_type, COUNT(1) AS 'num'
             FROM inner_charge
             GROUP BY to_kebie, charge_type) B ON A.cust_num = B.to_kebie LEFT JOIN
          charge_type C ON C.charge_type = B.charge_type
    WHERE A.cust_level = 'B'
    GROUP BY A.cust_level, C.description WITH rollup===================
    好象不对!!!
      

  5.   

    重新给出需求:===================================
    三个表
    1、inner_charge表
       主键:charge_num 编码
       字段:charge_type 类型[关联charge_type表]
             cust_num 部门编码[关联inner_cust表的C级编码]
     creat_time 创建时间
    2、charge_type表
       主键:charge_type 类型码
       字段:description 描述
    3、inner_cust表
       主键:cust_num 内部客户编码
       字段:description 内部客户名称
             cust_level 内部客户等级,请注意:[B为部,C为科],提表时,以部汇总
             super_cust 上级编码现在统计以下分布报表:
    给出具体一天,统计该天inner_charge表信息
    --------------------------------------------------------------------------
    |     B级部门名称           |             类型          |纪录条数         |
    |[inner_cust].[description] |[charge_type].[description]|SUM(满足的纪录数)|
    ---------------------------------------------------------------------------
    |部门1 [cust_level]='B'     |     类型1                 |    2            |
                                 ----------------------------------------------
    |                           |     类型2                 |    4            |
                                 ----------------------------------------------
    |                           |     类型3                 |    5            |
                                 ----------------------------------------------
    |                           |     类型....              |    6            |
    ---------------------------------------------------------------------------
    |小计                       |                           |    XX(2+4+5+6)  |
    ---------------------------------------------------------------------------
    |部门2 [cust_level]='B'     |     类型1                 |    1            |
                                 ----------------------------------------------
    |                           |     类型2                 |    2            |
                                 ----------------------------------------------
    |                           |     类型3                 |    3            |
                                 ----------------------------------------------
    |                           |     类型....              |    4            |
    ---------------------------------------------------------------------------
    |小计                       |                           |    XX(1+2+3+4)  |
    ---------------------------------------------------------------------------
    |部门3 [cust_level]='B'     |     类型1                 |    4            |
                                 ----------------------------------------------
    |                           |     类型2                 |    3            |
                                 ----------------------------------------------
    |                           |     类型3                 |    2            |
                                 ----------------------------------------------
    |                           |     类型....              |    1            |
    ---------------------------------------------------------------------------
    |小计                       |                           |    XX(4+3+2+1)  |
    ---------------------------------------------------------------------------