统计数据问题求教

lene3 2012-06-26 01:37:16
有两个表 一个购买表,一个使用表,字段如下
a表 时间 用户 道具id 购买数量 用完时间

b表 时间 用户 道具id 使用数量

现在a表某个道具买了M个,b表记录了这个道具每次使用量n,求这个道具累计使用量sum(n)>=M的最小时间
...全文
93 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lene3 2012-06-29
  • 打赏
  • 举报
回复
很悲剧,结贴早了,那个语句根本行不通,哎!
lene3 2012-06-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
SQL code


--> 测试数据:[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表]
se……
[/Quote]



这么写能算出来,可是要往a表那个用完时间更新的话,就会报错“消息 512,级别 16,状态 1,第 1 行
子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的”,更新的话,改怎么写。
  • 打赏
  • 举报
回复

--> 测试数据:[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
*/
  • 打赏
  • 举报
回复

--> 测试数据:[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
)
select MIN([时间]) as [时间]
from t
where t.累计使用数量>=(select [购买数量]
from [a表] a where t.道具id=a.道具id and t.用户=a.用户)
/*
时间
-----------
270
*/
Mr_Nice 2012-06-26
  • 打赏
  • 举报
回复

if object_id('[a]') is not null drop table [a]
go
create table [a] (时间 int,用户 nvarchar(2),道具id nvarchar(2),购买数量 int,用完时间 nvarchar(2))
insert into [a]
select 123,'a','b',20,'X'


if object_id('[b]') is not null drop table [b]
go
create table [b] (时间 int,用户 nvarchar(2),道具id nvarchar(2),使用数量 int)
insert into [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

select * from [a]
select * from [b]

SELECT MIN(TB.时间)
FROM ( SELECT b.时间 ,
b.道具id ,
SUM(b2.使用数量) AS 使用数量
FROM b
INNER JOIN b b2 ON b.道具id = b2.道具id
AND b.时间 >= b2.时间
GROUP BY b.时间 ,
b.道具id
) tb
INNER JOIN a ON TB.道具id = a.道具id
AND a.购买数量 <= tb.使用数量

lene3 2012-06-26
  • 打赏
  • 举报
回复
补加两个要求,刚看表时发现的。b表的累计开始时间应大于a表的购买时间,而且时间的取值应该是从b表取,不是a表取。
  • 打赏
  • 举报
回复

with t
as(
select
用户,道具id,sum(使用数量) as 使用数量
from b表
group by 用户,道具id
)'
m as
(
select a.* from a表 a,t
where a.用户=t.用户,
and a.道具id=t.道具id
a.购买数量<=t.使用数量
)
select * from m a
where a.时间=(select min(时间) from m b where a.道具id=b.道具id and a.购买数量<=b.使用数量 )
lene3 2012-06-26
  • 打赏
  • 举报
回复
a表
时间 用户 道具id 购买数量 用完时间
123 a b 20 X
b表
时间 用户 道具id 使用数量
235 a b 5
250 a b 10
270 a b 6
285 a b 7
时间270的b道具累计使用数量是21,时间285的累计使用数量27.X取时间的最小值,即270
  • 打赏
  • 举报
回复
给出具体测试数据和期待结果

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧