SQL来查找按时间顺序排序的表中两行之间的最大值

SQL来查找按时间顺序排序的表中两行之间的最大值,sql,max,Sql,Max,我有一张表,里面有按时间顺序排列的事件。他们有类型、时间和身高 数据可以总结如下: ID, Type, Time, Height 1, A, time, 0 2, XX, time, 0 3, B, time, 3 4, B, time, 6 5, C, time, 0 6, XX, time, 0 7, A, time, 0 8, C, time, 0 9, A, time, 0 10, B, time, 2 11, C, time, 0 e

我有一张表,里面有按时间顺序排列的事件。他们有类型、时间和身高

数据可以总结如下:

ID, Type, Time, Height  
1, A, time, 0  
2, XX, time, 0   
3, B, time, 3  
4, B, time, 6  
5, C, time, 0  
6, XX, time, 0  
7, A, time, 0  
8, C, time, 0  
9, A, time, 0  
10, B, time, 2  
11, C, time, 0  
etc时间列按升序排序

我想找到一个SQL语句来列出所有类型的a/B/C,其中B是类型a和C之间高度列的最大值。 因此,输出将如下所示:

1, A, time, 0   
4, B, time, 6  
5, C, time, 0  
7, A, time, 0  
8, C, time, 0  
9, A, time, 0  
10, B, time, 2  
11, C, time, 0  
A/B和C的顺序始终正确,即B始终在A和C之间,但可能根本没有B,或者A和C之间可能有多个B

如果a和C之间没有B,则输出可能/可能不列出具有空数据的B事件。 保证每个a型事件后都有一个C

输出中应忽略所有XX事件。列表上的时间戳永远不会重复-没有两个事件包含相同的时间

我猜在某处使用MAX函数,并根据A和C的时间选择A和C之间的所有B行


TIA

不确定我是否100%正确,但我发现最好将这些内容分解成更小的查询并放入临时表中。这里有个裂缝。。。顺便说一句,这是SQL Server T-SQL

-- get all the type 'a' and type 'c' IDs to represent time spans
if object_id('tempdb..#tab_ac') is not null drop table #tab_ac
select
    a.ID as A_ID,
    (
        select top 1 c.ID
        from tab c
        where c.Time > a.Time
        and c.Type = 'C'
        order by c.Time
    ) as C_ID
into
    #tab_ac
from
    tab a
where
    a.Type = 'A'

create index ix_#tab_ac on #tab_ac (A_ID, C_ID)    

-- get the id with the max height between those spans
if object_id('tempdb..#result1') is not null drop table #result1
select
    ac.*,
    (
        select x.ID
        from tab x
        where x.Time between ta.Time and tc.Time
        order by a.Height desc
    ) as ID_With_Max_Height
into
    #result1
from
    #tab_ac ac join
    tab ta on ac.A_ID = t.ID join
    tab tc on ac.C_ID = t.ID

-- see if that id is type 'B'
select
    *
from
    #result1 r join
    tab t on r.ID_With_Max_Height = t.ID
where
    t.Type = 'B'

根据您希望如何处理“最大高度”的关系,您可能需要修改第二个查询的ORDER BY子句。祝你好运。

我认为这是可行的。我所做的是首先构建时间范围,确定每个A和C之间的时间,然后将B分组到这些范围中,并从每个范围中提取具有最大高度的记录,然后将其合并回A和C的原始条目。您可以分割出cte_源_数据,并用实际表替换对它的引用。我在PostgreSQL 9.0中编写并测试了它,但它可以在任何主流数据库中工作,只需对特定的SQL方言稍加修改

select * from table as t1 where `type`!='XX' and height=(
    select MAX(height) from table as t2 where type=t1.type
) order by id
结果:

id type time height 1 A 2010-12-10 21:00:00 1:00 0 4 B 2010-12-10 21:00:00 4:00 6 5 C 2010-12-10 21:00:00 5:00 0 7 A 2010-12-10 21:00:00 7:00 0 8 C 2010-12-10 21:00:00 8:00 0 9 A 2010-12-10 21:00:00 9:00 0 10 B 2010-12-10 21:00:00 10:00 2 11 C 2010-12-10 21:00:00 11:00 0
你没有说过如果a第一个B在第一个a之前,B最后一个B在最后一个C之后,C两个a之间没有C,d两个C之间没有a,e两行有完全相同的时间戳,f两个B有完全相同的高度但不同的距离,等等…感谢mark-编辑以消除一些歧义。 id type time height 1 A 2010-12-10 21:00:00 1:00 0 4 B 2010-12-10 21:00:00 4:00 6 5 C 2010-12-10 21:00:00 5:00 0 7 A 2010-12-10 21:00:00 7:00 0 8 C 2010-12-10 21:00:00 8:00 0 9 A 2010-12-10 21:00:00 9:00 0 10 B 2010-12-10 21:00:00 10:00 2 11 C 2010-12-10 21:00:00 11:00 0