create table test1
(id int,name nvarchar(100),[desc] nvarchar(100),coin float,createts datetime)
insert into test1 values
(101,'tom','C',100,'2013-02-23 22:17:28.000'),
(102,'tom','A C',10,'2013-02-23 22:17:28.000'),
(103,'PETER','C',50,'2013-02-23 21:51:55.000'),
(104,'MARY','C',80,'2013-02-23 21:06:48.000'),
(105,'MARY','A C',20,'2013-02-23 21:06:48.000')
desc列为'C'表示这是一条充值记录,为'A C'表示这条记录是充值额外赠送的
要求当name列和createts列相同时合并coin列得到以下结果
id name coin createts
101 tom 110 2013-02-2322:17:28.000
103 PETER 50 2013-02-23 21:51:55.000
104 MARY 100 2013-02-23 21:06:48.000
------解决方案--------------------
select id=min(id),name,coin=sum(coin),createts from test1 group by name,createts order by min(id)
------解决方案--------------------
首先建议你才coin字段既然涉及到金额不要用float,float是近似类型,不精确
要用decimal或者numeric,金融类的可以用money类型
select min(id)id,name,sum(coin)coin,createts from tb group by name, createts