sql server 数据库的表中含有二个字段----客户,金额。
现要求在 asp 页面上显示:
1。显示客户,金额的记录;
2。显示金额的合计值。(要求在 asp 页面记录表格的后面再添加单独一行来显示金额的合计值)
如果只用一个 sql 语句能完成上述二个要求吗?
------解决方案--------------------------------------------------------
select a1,a2,a1+a2 as total from .....
------解决方案--------------------------------------------------------
select
case when grouping(客户)=1 then '合计 ' else 客户 end as 客户,
sum(金额) as 金额 from 表名
group by 客户 with rollup
------解决方案--------------------------------------------------------
select 客户,金额 from 客户表
union
select '合计 ' as 客户, sum(金额) as 金额 from 客户表
group by 客户
------解决方案--------------------------------------------------------
@amount money output
select @amount=sum(金额) from 客户表
select '合计 ' as 客户, sum(金额) as 金额 from 客户表
group by 客户
------解决方案--------------------------------------------------------
select 客户,金额,SUM(金额)from 表 where group by 客户
------解决方案--------------------------------------------------------
select a1,a2 from tb
UNION ALL
select ' ',sum(a2) from tb