如何在表中为另一个表的每个记录查找特定行

时间:2018-01-31 18:27:24

标签: sql sybase sybase-ase

这就是表格的样子 -

time                    id     code

8/22/1999 12:00:00 AM   0001    A <------------ no
8/24/2001 6:03:02 AM    0001    A
6/27/2002 4:45:20 PM    0001    B  
5/8/2003 9:03:13 AM     0001    B  
5/8/2003 10:02:34 AM    0001    A <------------ no
6/9/2008 10:43:03 AM    0001    A
11/22/2011 3:42:10 PM   0001    A
4/11/2012 2:03:49 PM    0001    D  
4/11/2012 2:04:00 PM    0001    D  
12/6/2017 9:30:17 PM    0001    A <------------ yes
12/6/2017 9:30:17 PM    0001    A
12/6/2017 10:06:11 PM   0001    A
12/7/2017 3:24:58 AM    0001    C  
1/3/2018  5:02:13 PM    0001    C

0001是另一个表中的ID。 CODE可以随时以任何顺序从A切换到B到C到D.当代码从任何其他代码翻转到A时,我需要找到最近发生的事件。在这个例子中,它发生在用“是”表示的行中。 “否”记录无效,因为稍后会出现这种情况。

无法弄清楚如何解决这个问题。这可能需要一个存储过程,按记录记录,维护状态并计算出值。

编辑:

我想知道我是否能够以某种方式添加一个名为grp的列 -

id code

A 1
A 1
B 2
B 2
A 3
A 3
A 3
D 4
D 4
A 5
A 5
A 5
C 6
C 6

然后我就能得到 -

max of the min(grp) group by grp

1 个答案:

答案 0 :(得分:1)

假设这是Sybase ASE,虽然以下大多数是相当简单的SQL,它应该很容易转换为其他RDBMS产品

首先,我们会看到我们是否可以找到code更改为'A'的3行

select  f1.[time],
        f1.id,
        f1.code
from    fliptable f1
where   f1.code = 'A'
and     (   -- see if the previous record has a code!='A';  to find the 'previous' 
            -- record we find the row with max(time) < current record's time

                exists (select  1
                        from    fliptable f2
                        where   f2.id = f1.id
                        and     f2.code != f1.code
                        and     f2.[time] = (select     max(f3.[time])
                                                from    fliptable f3
                                                where   f3.id = f1.id
                                                and     f3.[time] < f1.[time]))
        or
            -- catch case where the 'first' row in the table has code='A'

            not exists (select  1
                        from    fliptable f4
                        where   f4.id = f1.id
                        and     f4.[time] < f1.[time])
        )

order by f1.[time]
go

 time                            id   code
 ------------------------------- ---- ----
             Aug 22 1999 12:00AM 0001 A
             May  8 2003 10:02AM 0001 A
             Dec  6 2017  9:30PM 0001 A
             Dec  6 2017  9:30PM 0001 A     <=== side effect of having a dup row in the data

从这里我们应该可以添加top 1并翻转到order by / desc以提取最新/最新记录......

select  top 1
        f1.[time],
        f1.id,
        f1.code
from    fliptable f1
where   f1.code = 'A'
and     (       exists (select  1
                        from    fliptable f2
                        where   f2.id = f1.id
                        and     f2.code != f1.code
                        and     f2.[time] = (select     max(f3.[time])
                                                from    fliptable f3
                                                where   f3.id = f1.id
                                                and     f3.[time] < f1.[time]))
        or
            not exists (select  1
                        from    fliptable f4
                        where   f4.id = f1.id
                        and     f4.[time] < f1.[time])
        )

order by f1.[time] desc
go

 time                            id   code
 ------------------------------- ---- ----
             Dec  6 2017  9:30PM 0001 A

在ASE 15.7 SP138上测试