2

给定以下表结构

Col1, Col2, EventType,  DateTime

如何选择每组的记录Col1,这些记录出现在该特定组Col2的顶部记录之后。EventType = 3Col1Col2

例如下面的数据

Col1, Col2, EventType, DateTime
A       B       1      2012-1-1
A       B       3      2011-1-1
A       B       1      2010-1-1
C       D       1      2012-1-1
C       D       2      2011-1-1
C       D       2      2010-1-1
C       D       3      2009-1-1
C       D       2      2008-1-1
C       D       3      2007-1-1
C       D       1      2006-1-1
C       D       2      2005-1-1

我要选择

Col1, Col2, EventType, DateTime
A       B       1      2012-1-1

C       D       1      2012-1-1
C       D       2      2011-1-1
C       D       2      2010-1-1
4

5 回答 5

3

您可以在子查询上使用 max 函数:

SELECT Col1, Col2, EventType, DateTime 
FROM theTable A
WHERE DateTime > 

(SELECT  MAX(DateTime)
FROM    theTable SUB
WHERE   EventType = 3
AND SUB.COL1 = A.COL1
AND SUB.COL2 = A.COL2)
于 2013-05-03T07:04:15.400 回答
1

可以使用以下方法解决此问题ROW_NUMBER()

  1. 将行划分为 的组,(Col1, Col2)并按 的升序排列每组中的行DateTime

    Col1  Col2  EventType  DateTime  EventRank
    ----  ----  ---------  --------  ---------
    A     B     1          2012-1-1  3
    A     B     3          2011-1-1  2
    A     B     1          2010-1-1  1
    C     D     1          2012-1-1  8
    C     D     2          2011-1-1  7
    C     D     2          2010-1-1  6
    C     D     3          2009-1-1  5
    C     D     2          2008-1-1  4
    C     D     3          2007-1-1  3
    C     D     1          2006-1-1  2
    C     D     2          2005-1-1  1
    
  2. 此外,将行分区并按 的降序排列(Col1, Col2, EventType)它们。DateTime

    Col1  Col2  EventType  DateTime  EventRank  EventSubRank
    ----  ----  ---------  --------  ---------  ------------
    A     B     1          2012-1-1  3          1
    A     B     3          2011-1-1  2          1
    A     B     1          2010-1-1  1          2
    C     D     1          2012-1-1  8          1
    C     D     2          2011-1-1  7          1
    C     D     2          2010-1-1  6          2
    C     D     3          2009-1-1  5          1
    C     D     2          2008-1-1  4          3
    C     D     3          2007-1-1  3          2
    C     D     1          2006-1-1  2          2
    C     D     2          2005-1-1  1          4
    
  3. 选择一个子集,其中EventType = 3 AND EventSubRank = 1

    Col1  Col2  EventType  DateTime  EventRank  EventSubRank
    ----  ----  ---------  --------  ---------  ------------
    A     B     3          2011-1-1  2          1
    C     D     3          2009-1-1  5          1
    
  4. 通过将其连接回已排序的行集并选择后者的EventRank值大于子集中相应值的行,将其用作过滤器。

这是一个完整的查询:

WITH ranked AS (
  SELECT
    *,
    EventRank    = ROW_NUMBER() OVER (PARTITION BY Col1, Col2            ORDER BY DateTime ASC ),
    EventSubRank = ROW_NUMBER() OVER (PARTITION BY Col1, Col2, EventType ORDER BY DateTime DESC)
  FROM atable
),
filtered AS (
  SELECT *
  FROM ranked
  WHERE EventType    = 3
    AND EventSubRank = 1
)
SELECT
  r.Col1,
  r.Col2,
  r.EventType,
  r.DateTime
FROM ranked
INNER JOIN filtered f
   ON r.Col1 = f.Col1
  AND r.col2 = f.Col2
  AND r.EventRank > f.EventRank
;
于 2013-05-03T07:33:19.083 回答
0
select Col1, Col2, EventType, DateTime 
From yourtable A, 
(select Col1, Col2 from yourtable  group by Col1, Col2) B 
where A.EventType<3 and A.Col1 =B.Col1 And A.Col2=B.Col2
于 2013-05-03T06:59:13.307 回答
0

得到预期的结果

       Select   Col1, Col2, EventType, DateTime  from table where EventType<3
于 2013-05-03T07:00:21.560 回答
0

试试这个,

Select a.* FROm yourtable A JOIN

(SELECT * FROM yourTable WHERE EventType=3) b
ON A.Col1=B.Col1
AND A.Col2 = B.Col2
WHERE A.dateTime>B.DateTime
于 2013-05-03T07:07:17.133 回答