急求合计 高手帮忙~

mgaxin 2012-03-02 02:41:42
现有下面一段查询代码,怎样在最后一行 求一下各个 _cnt 的合计 只要求最后一行有一个总的合计


拜托各位高手帮忙 非常急 感激不尽

SELECT dept_nm, minor_nm ,
sum(dangri_cnt) as dangri_cnt,
sum(xuesheng_cnt) as xuesheng_cnt,
sum(chuqin_cnt) as chuqin_cnt
FROM
(
select dept_nm,minor_nm,dangri_cnt,0 as xuesheng_cnt,0 as chuqin_cnt from #A
union all
select dept_nm,minor_nm,0 as dangri_cnt,xuesheng_cnt,0 as chuqin_cnt from #B
union all
select dept_nm,minor_nm,0 as dangri_cnt,0 as xuesheng_cnt,chuqin_cnt from #C
) T
GROUP BY dept_nm, minor_nm

...全文
85 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
夜色镇歌 2012-03-02
  • 打赏
  • 举报
回复
勿勿 2012-03-02
  • 打赏
  • 举报
回复

union
select .....
就好了、
tomyuansir 2012-03-02
  • 打赏
  • 举报
回复
SELECT dept_nm, minor_nm ,
sum(dangri_cnt) as dangri_cnt,
sum(xuesheng_cnt) as xuesheng_cnt,
sum(chuqin_cnt) as chuqin_cnt
FROM
(
select dept_nm,minor_nm,dangri_cnt,0 as xuesheng_cnt,0 as chuqin_cnt from #A
union all
select dept_nm,minor_nm,0 as dangri_cnt,xuesheng_cnt,0 as chuqin_cnt from #B
union all
select dept_nm,minor_nm,0 as dangri_cnt,0 as xuesheng_cnt,chuqin_cnt from #C
) T
GROUP BY dept_nm, minor_nm
WITH ROLLUP
试一下吧 没自己看题目了
trunjun 2012-03-02
  • 打赏
  • 举报
回复
这个应该在写一个SELECT合计语句吧。或在程序里求。
貌似一条语句很难出来的。
  • 打赏
  • 举报
回复

;with T
as
(SELECT dept_nm, minor_nm ,
sum(dangri_cnt) as dangri_cnt,
sum(xuesheng_cnt) as xuesheng_cnt,
sum(chuqin_cnt) as chuqin_cnt
FROM
(
select dept_nm,minor_nm,dangri_cnt,0 as xuesheng_cnt,0 as chuqin_cnt from #A
union all
select dept_nm,minor_nm,0 as dangri_cnt,xuesheng_cnt,0 as chuqin_cnt from #B
union all
select dept_nm,minor_nm,0 as dangri_cnt,0 as xuesheng_cnt,chuqin_cnt from #C
) T
GROUP BY dept_nm, minor_nm
)
select *ftom T union all
select dept_nm, minor_nm ,
sum( dangri_cnt) as dangri_cnt,
sum(xuesheng_cnt) xuesheng_cnt,
sum(chuqin_cnt)as chuqin_cnt

from T group by dept_nm, minor_nm
order by dept_nm, minor_nm
mgaxin 2012-03-02
  • 打赏
  • 举报
回复
我是新手 如何做 能否在我的代码上给修改一下?



[Quote=引用 1 楼 jack15850798154 的回复:]
总合计SQL中用 UNION ALL ,或则 在后台代码中拼接
[/Quote]
jack15850798154 2012-03-02
  • 打赏
  • 举报
回复
总合计SQL中用 UNION ALL ,或则 在后台代码中拼接

34,588

社区成员

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

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