关于汇总的事

Ryo_Hazuki 2005-11-22 06:24:22
我想把汇总后年度的那个NULL用∑替换,请问该如何做呢
use northwind
select year(birthdate) as '年度', avg(cast(extension as int)) as '範囲' from employees group by year(birthdate) with rollup
...全文
135 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lengxiaowei 2005-11-23
  • 打赏
  • 举报
回复
select '年度'=case when grouping(birthdate)=1 then '∑' else cast(birthdate as varchar) end,
avg(cast(extension as int)) as '範囲' from
(select birthdate=year(birthdate),extension from employees) a
group by birthdate with rollup
duoluohuifeng 2005-11-23
  • 打赏
  • 举报
回复
关注
Ryo_Hazuki 2005-11-23
  • 打赏
  • 举报
回复
不行啊
服务器: 消息 156,级别 15,状态 1,行 1
在关键字 'as' 附近有语法错误。

我如果是
select case when(grouping(year(birthdate)) = 1) then '∑' end as '年度', avg(cast(extension as int)) as '範囲' from employees group by year(birthdate) with rollup

汇总的那个∑可以出但分类的年度都显示为NULL了

如果写成
select case when(grouping(year(birthdate)) = 1) then '∑' else isnull(year(birthdate), 'N/A') end as '年度', avg(cast(extension as int)) as '範囲' from employees group by year(birthdate) with rollup

则抛错
服务器: 消息 8120,级别 16,状态 1,行 1
列 'employees.BirthDate' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

我该怎么办
Ryo_Hazuki 2005-11-23
  • 打赏
  • 举报
回复
可以不用子查询的方法吗
ReViSion 2005-11-22
  • 打赏
  • 举报
回复
--试下这个
select case grouping(year(birthdate)) when 1 then '∑' else year(birthdate) as '年度' end, avg(cast(extension as int)) as '範囲' from employees
group by year(birthdate) with rollup

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧