如何处理除数字汇总字段外的非汇总关键字,例如
DeptId       DeptName         PersonId      PersonName    Quant
001          人事部            001001         张三          80 
001          人事部            001001         张三         180
001          人事部            001002         李四         280
001          人事部            001002         李四          80
002          财务部            002001         张三          80
002          财务部            002001         张三          80
需要结果:
001          人事部            001001         张三         260
001          人事部            001002         李四         360
001          人事部            小计                        620
002          财务部            002001         张三         160
002          财务部            小计                        160
合计                                                       780
 

解决方案 »

  1.   

    select 
        DeptId,
        DeptName,
        PersonId,
        PersonName,
        sum(Quant) as Quant
    from
        表
    group by
        DeptId,DeptName,PersonId,PersonName
    having
        grouping(PersonName)=0 or (grouping(DeptName)=0 and grouping(PersonId)=1) or grouping(DeptId)=1
      

  2.   

    select 
        DeptId   = isnull(DeptId,'合计'),
        DeptName ,
        PersonId = (case when DeptId is not null then isnull(PersonId,'小计') end),
        PersonName,
        Quant    = sum(Quant)
    from
        表
    group by
        DeptId,DeptName,PersonId,PersonName
    having
        grouping(PersonName)=0 
        or 
        (grouping(DeptName)=0 and grouping(PersonId)=1) 
        or 
        grouping(DeptId)=1
      

  3.   

    declare @t table(
        DeptId varchar(8),DeptName varchar(8),
        PersonId varchar(8),PersonName varchar(8),Quant int
     )
    Insert into @t
    select '001','人事部','001001','张三', 80 
    union select '001','人事部','001001','张三',          180
    union select '001','人事部','001002','李四',          280
    union select '001','人事部','001002','李四',           80
    union select '002','财务部','002001','张三',           80
    union select '002','财务部','002001','张三',           80Select DeptId   = isnull(DeptId,'合计'),isnull(DeptName,'') as DeptName , 
           PersonId = (case when DeptId is not null then isnull(PersonId,'小计')  else '' end),
           isnull(PersonName,'') as PersonName, Quant    = sum(Quant)
    from @t
    group by DeptId,DeptName,PersonId,PersonName
    with rollup
    having grouping(PersonName)=0 or (grouping(DeptName)=0 and
           grouping(PersonId)=1)  or grouping(DeptId)=1
      

  4.   

    select A.DeptId,B.DeptName,A.PersonId,C.PersonName,A.Quant
    from
    (select 
        DeptId   = case grouping(deptId) when 1 then '合计' else deptId end,
        PersonId = case when grouping(deptId)<>1 and grouping(PersonId)=1 then '小计' else PersonId end,
        Quant    = sum(Quant)
    from

    group by
        DeptId,PersonId
    with rollup) A left join 
    (select distinct DeptId,DeptName from 表) B
    on A.DeptId=B.DeptId left join
    (select distinct PersonId,personName from 表) C
    on A.PersonId = C.PersonId