【问题标题】:group by clause with rollup带有汇总的分组子句
【发布时间】:2011-09-17 18:43:38
【问题描述】:

我正在尝试在 sql server 2005 中使用 group by 和 rollup 子句,但我遇到了一些问题。

这是一个简单的转储

create table group_roll (
id int identity,
id_name int,
fname varchar(50),
surname varchar(50),
qty int
)

go
insert into group_roll (id_name,fname,surname,qty) values (1,'john','smith',10)
insert into group_roll (id_name,fname,surname,qty) values (1,'john','smith',30)
insert into group_roll (id_name,fname,surname,qty) values (2,'frank','white',5)
insert into group_roll (id_name,fname,surname,qty) values (1,'john','smith',8)
insert into group_roll (id_name,fname,surname,qty) values (2,'frank','white',10)
insert into group_roll (id_name,fname,surname,qty) values (3,'rick','black',10)
go

如果我运行这个简单的查询

select id_name,fname,surname,sum(qty) as tot
from group_roll
group by id_name,fname,surname

我明白了

1   john    smith   48
2   frank   white   15
3   rick    black   10

我想要

1   john    smith   48
2   frank   white   15
3   rick    black   10
Total                73

这就是我努力实现的目标

select 
case when grouping(id_name) = 1 then 'My total' else cast(id_name as char) end as Name_id ,
fname,surname,sum(qty) as tot
from group_roll
group by id_name,fname,surname
with rollup
order by case when id_name is null then 1 else 0 end, tot desc

但我的结果是

1                               john    smith   48
1                               john    NULL    48
1                               NULL    NULL    48
2                               frank   white   15
2                               frank   NULL    15
2                               NULL    NULL    15
3                               rick    black   10
3                               rick    NULL    10
3                               NULL    NULL    10
My total                         NULL   NULL   73

我的错误在哪里?

编辑。 我可以解决我的问题

select * from (
select cast(id_name as char) as id_name,fname,surname,sum(qty) as tot
from group_roll
group by id_name,fname,surname 
union
select 'Total',null,null,sum(qty) from group_roll ) as t
order by case when id_name = 'Total' then 1 else 0 end,tot desc

但我想了解汇总是否可以解决我的问题。

【问题讨论】:

  • 我在出门的路上,周六还早,我想清楚,但也许可以尝试按 fname + surname 分组,而不是单独两个字段

标签: sql-server-2005 tsql group-by rollup


【解决方案1】:

您不能在语句本身内执行此操作,但是您可以过滤 ROLLUP 集,不包括中间汇总,即任何一行但不是所有行都被分组:

select
    case when grouping(id_name) = 1 then 'My total' else cast(id_name as char) end as Name_id,
    fname,
    surname,
    sum(qty) as tot
from group_roll
group by id_name, fname, surname
with rollup
having grouping(id_name) + grouping(fname) + grouping(surname) in (0 , 3)

或类似于您的解决方案但引用原始查询;

;with T as (
    select cast(id_name as varchar(128)) as id_name,fname,surname,sum(qty) as tot
    from group_roll
    group by id_name,fname,surname
) select * from T union all select 'Total:',null,null, SUM(tot) from T

FWIW SQL 2008 允许;

select
    case when grouping(id_name) = 1 then 'My total' else cast(id_name as char) end as Name_id,
    fname,
    surname,
    sum(qty) as tot
from group_roll
group by grouping sets((id_name, fname, surname), ())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 2011-04-14
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多