日期:2014-05-17 浏览次数:20652 次
CREATE TABLE [dbo].[c](
[帐号] [varchar](50) NULL,
[媒体] [varchar](50) NULL,
[注册时间] [datetime2](7) NULL
) ON [PRIMARY]
insert into [dbo].[c] values ('qqq','tx','2013-06-05 00:00:00.0000000'),
('qqq','pp','2013-06-23 00:00:00.0000000'),
('qqq','xx','2013-07-05 00:00:00.0000000'),
('wwwwww','pp','2013-06-01 00:00:00.0000000'),
('wwwwww','fb','2013-06-15 00:00:00.0000000'),
('wwwwww','tos','2013-06-25 00:00:00.0000000')
CREATE TABLE [dbo].[d](
[帐号] [varchar](50) NULL,
[充值金额] [money] NULL,
[充值时间] [datetime2](7) NULL
) ON [PRIMARY]
insert into [dbo].[d] values ('qqq','12.00','2013-06-18 00:00:00.0000000'),
('qqq','33.00','2013-06-28 00:00:00.0000000'),
('qqq','55.00','2013-08-01 00:00:00.0000000'),
('wwwwww','10.00','2013-06-03 00:00:00.0000000'),
('wwwwww','20.00','2013-06-04 00:00:00.0000000'),
('wwwwww','20.00','2013-06-16 00:00:00.0000000'),
('wwwwww','50.00','2013-06-26 00:00:00.0000000'),
('wwwwww','33.00','2013-06-29 00:00:00.0000000')
select a.帐号,a.媒体,sum(b.充值金额) 充值金额 from
( select 帐号,媒体,注册时间 注册时间A,
isnull((select min(注册时间) from c c2 where c1.帐号=c2.帐号 and c2.注册时间>c1.注册时间),'9999-01-01') 注册时间B
from c c1
) a
join d b on a.帐号=b.帐号 and a.注册时间A<=b.充值时间 and a.注册时间B>b.充值时间