MS Access“分组依据”连续值

发布于 2024-11-29 00:12:45 字数 688 浏览 1 评论 0原文

我有以下访问表(主键=日期+Id):

Date                     Id       Value
01/07/2011 00:10:00      5        200
01/07/2011 00:30:00      5        210
01/07/2011 00:40:00      2        458
01/07/2011 00:50:00      2        500
01/07/2011 01:00:00      4        600
01/07/2011 01:10:00      5        359
01/07/2011 01:20:00      5        360
01/07/2011 01:30:00      5        370
01/07/2011 01:40:00      5        380

当然,查询“SELECT Id, MAX(Value) FROM DATAS GROUP BY Id;”返回:

Id  Max
2   500
4   600
5   380

但是在 < strong>MS Access 查询按 Id 的“序列”分组? 预期结果:

Id  Max
5   210
2   500
4   600
5   380

I Have the following Access table (Primary Key = Date+Id):

Date                     Id       Value
01/07/2011 00:10:00      5        200
01/07/2011 00:30:00      5        210
01/07/2011 00:40:00      2        458
01/07/2011 00:50:00      2        500
01/07/2011 01:00:00      4        600
01/07/2011 01:10:00      5        359
01/07/2011 01:20:00      5        360
01/07/2011 01:30:00      5        370
01/07/2011 01:40:00      5        380

Of course, the query "SELECT Id, MAX(Value) FROM DATAS GROUP BY Id;" returns:

Id  Max
2   500
4   600
5   380

But is it possible in MS Access to have a query which groups by "sequences" of Id?
Expected result:

Id  Max
5   210
2   500
4   600
5   380

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧幽蓝 2024-12-06 00:12:45

没有简单的方法可以解决您的问题。这是一个解决方法。

declare @t table(date datetime, id int, value int)

insert @t values('01/07/2011 00:10:00',5,200) 
insert @t values('01/07/2011 00:30:00',5,210)
insert @t values('01/07/2011 00:40:00',2,458)
insert @t values('01/07/2011 00:50:00',2,500) 
insert @t values('01/07/2011 01:00:00',4,600) 
insert @t values('01/07/2011 01:10:00',5,359) 
insert @t values('01/07/2011 01:20:00',5,360) 
insert @t values('01/07/2011 01:30:00',5,370) 
insert @t values('01/07/2011 01:40:00',5,380) 

;with a as
(
select date, id, value, (select count(*) from @t where t.date > date) rn
from @t t
), b as
(
select a.date, a.id, a.value, coalesce(cast(b.id - a.id as bit),1) d
from a left join a b on a.rn -1 = b.rn
), c as 
(
select date,id, value, (select sum(d) from b b2 where date <=b.date) e from b
)
select id, max(value) max 
from c group by id, e

结果:

id    max
----- ---
5     210
2     500
4     600
5     380

There is no easy way to solve your problem. Here is a workaround.

declare @t table(date datetime, id int, value int)

insert @t values('01/07/2011 00:10:00',5,200) 
insert @t values('01/07/2011 00:30:00',5,210)
insert @t values('01/07/2011 00:40:00',2,458)
insert @t values('01/07/2011 00:50:00',2,500) 
insert @t values('01/07/2011 01:00:00',4,600) 
insert @t values('01/07/2011 01:10:00',5,359) 
insert @t values('01/07/2011 01:20:00',5,360) 
insert @t values('01/07/2011 01:30:00',5,370) 
insert @t values('01/07/2011 01:40:00',5,380) 

;with a as
(
select date, id, value, (select count(*) from @t where t.date > date) rn
from @t t
), b as
(
select a.date, a.id, a.value, coalesce(cast(b.id - a.id as bit),1) d
from a left join a b on a.rn -1 = b.rn
), c as 
(
select date,id, value, (select sum(d) from b b2 where date <=b.date) e from b
)
select id, max(value) max 
from c group by id, e

Result:

id    max
----- ---
5     210
2     500
4     600
5     380
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文