日期:2014-05-18  浏览次数:20504 次

求一SQL语句的写法!解决马上给分!
有一表结构如下:

totalmoney       custerNum
109.75               k003
45                       k003
76                       k004
430                     k005
107                     k006
43                       k007
98                       k006
...
....
--------------------
求一条语句即可算出每个custerNum的sum(totalmoney)的总额,即每个客户的购物总额!谢谢.本人较菜

------解决方案--------------------
select custerNum,sum(totalmoney)
from 表
group by custerNum
------解决方案--------------------
select sum(totalmoney) 总额,custerNum from tb group by custerNum
--group by 起汇总作用,即当custerNum时计算所有字段为custerNum的totalmoney和
或用
--
select totalmoney,custerNum,总额=(select sum(totalmoney) from tb where custerNum=a.custerNum) from tb a
------解决方案--------------------
按 custerNum  分组
------解决方案--------------------
select sum(totalmoney) as 总额,custerNum from tb group by custerNum
--group by 起汇总作用,即当custerNum时计算所有字段为custerNum的totalmoney和
或用
--
select custerNum,总额=sum(totalmoney) from tb group by custerNum
------解决方案--------------------
create table t(
totalmoney decimal(10,2),
custerNum varchar(10)
)
insert into t select 109.75 , 'k003 '
union all select 45 , 'k003 '
union all select 76 , 'k004 '
union all select 430 , 'k005 '
union all select 107 , 'k006 '
union all select 43 , 'k007 '
union all select 98 , 'k006 '

select custerNum,sum(totalmoney)as totalmoney
from t
group by custerNum

drop table t
--------------------
k003 154.75
k004 76.00
k005 430.00
k006 205.00
k007 43.00

------解决方案--------------------
select custerNum,sum(totalmoney) as 购物总额
from 表
group by custerNum
------解决方案--------------------
select custerNum,sum(totalmoney) as 购物总额
from 表
group by custerNum
order by custerNum
------解决方案--------------------
都是对的,不说了
------解决方案--------------------


select * from T
order by custerNum
compute sum(totalmoney)as totalmoney by custerNum
compute sum(totalmoney)as totalmoney2