分组统计的简单问题

kkobebryant 2005-11-12 05:02:51
表: ID debit credit date
1 8.0 9.0 20051011
2 4.0 5.6 20051012
3 2.0 3.6 20051011

我要的查询结果:
ID debit credit date
1 8.0 9.0 20051011
3 2.0 3.6 20051011
合计 10.0 12.6
2 4.0 5.6 20051012
合计 4.0 5.6

请问这个SQL语句怎么写????????其实就是按日期分组统计
...全文
172 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
SQL147 2005-11-12
  • 打赏
  • 举报
回复
select id,debit,credit,date
from tb1
order by date
compute sum(debit),sum(credit) by date
kubou 2005-11-12
  • 打赏
  • 举报
回复
up
bitliuyang 2005-11-12
  • 打赏
  • 举报
回复
debit credit date
*********************
select id,sum(debit),sum(credit) from 表 group by date
zlp321002 2005-11-12
  • 打赏
  • 举报
回复

select id=case when grouping(id)=1 and grouping(date)=1 then '总计'
when grouping(id)=1 then date+':合计'
else convert(varchar(10),id) end,
debit=sum(debit),
credit=sum(credit),
date=isnull(date,'')
from @t
group by date,id
with rollup
txlicenhe 2005-11-12
  • 打赏
  • 举报
回复
create table tb(id int,debit numeric(6,1),credit numeric(10,2),date char(8))
insert tb select 1,8.0,9.0,'20051011'
union all select 2,4.0,5.6,'20051012'
union all select 3,2.0,3.6,'20051011'


select id,debit,credit,date1 from
(
select cast(id as varchar) id,debit,credit,date,date date1 from tb
union all
select '合计',sum(debit),sum(credit),date,''
from tb
group by date
) aa
order by date,id

呵呵,显示结果完全一样。

34,588

社区成员

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

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