日期:2014-05-18 浏览次数:20686 次
--> 测试数据:[a表]
if object_id('[a表]') is not null
drop table [a表]
create table [a表](
[时间] int,
[用户] varchar(1),
[道具id] varchar(1),
[购买数量] int,
[用完时间] varchar(1)
)
insert [a表]
select 123,'a','b',20,'X'
--> 测试数据:[b表]
if object_id('[b表]') is not null
drop table [b表]
create table [b表](
[时间] int,
[用户] varchar(1),
[道具id] varchar(1),
[使用数量] int
)
insert [b表]
select 235,'a','b',5 union all
select 250,'a','b',10 union all
select 270,'a','b',6 union all
select 285,'a','b',7
with t
as(
select *,
(select SUM([使用数量]) from [b表] b where a.时间>=b.时间) as [累计使用数量]
from [b表] a where a.时间>=(select [时间] from [a表] b
where a.道具id=b.道具id and a.用户=b.用户)
)
select MIN([时间]) as [时间]
from t
where t.累计使用数量>=(select [购买数量]
from [a表] a where t.道具id=a.道具id and t.用户=a.用户)
/*
时间
-----------
270
*/