两张表,payment表为充值记录,Consume表为消费记录
create table payment
(PaymentID int IDENTITY(1,1) NOT NULL,
userid int not null,
coin int not null,
PaymentTs datetime not null)
insert into payment values
(1001,100,'2012-09-20 02:50:06.000'),
(1001,50,'2012-09-21 12:17:07.000'),
(1002,100,'2012-09-22 13:31:46.000'),
(1001,20,'2012-09-22 14:51:26.000')
create table consume
(HistoryID bigint IDENTITY(1,1) NOT NULL,
userid int not null,
coin int not null,
[desc] nvarchar(255) null,
CreateTS datetime not null)insert into consume  values
(1001,24,'a','2012-09-20 02:55:32.000'),
(1001,68,'b','2012-09-20 03:02:24.000'),
(1001,37,'c','2012-09-21 12:25:06.000'),
(1002,48,'d','2012-09-22 13:42:46.000'),
(1002,39,'e','2012-09-22 13:42:46.000'),
(1001,41,'f','2012-09-22 15:01:06.000')
要求得到userid为1001的用户的消费记录
sumcoin为消费前的金钱,Balance为消费后剩余的金钱
userid sumcoin coin Balance Desc CreateTS
1001 100 24 76 a 2012-09-20 02:55:32.000
1001 76  68 8  b 2012-09-20 03:02:24.000
1001 58  37 21 c 2012-09-21 12:25:06.000
1001 41  41 0  f 2012-09-22 15:01:06.000