SQL 求和

来源:百度知道 编辑:UC知道 时间:2024/07/03 01:29:02
先有一个表A
name money date
张三 75 2008-1-5
李四 80 2008-2-20
张三 115 2008-6-11
李四 150 2008-10-10
如何实现:
name money date
张三 75 2008-1-5
张三 115 2008-6-11
合计 190
李四 80 2008-2-20
李四 150 2008-10-10
合计 230
总计 420
请高手帮忙,急!

select name,money,date from (
select '1'+name as ord1, name,money,date from A
union
select '1'+name+'1' as ord1,'合计',sum(money),'' from A group by name
union
select '2','总计',sum(money),'' from A ) tmp order by ord1,date

其中日期在合计和总计时,如果‘’提示说不行,就用null吧,看你数据库存的日期的格式是什么

select rtrim(name)+'(合计)' as name,sum(money) as money,'' as date from A group by name
union
select * from A
order by name

select su as sum(money) from 表名 where name = '张三'
if not rs.eof the
response.write(su)