如何在sql中的组内的某个记录之后选择所有记录?

时间:2013-05-03 06:54:25

标签: sql sql-server-2008

给出以下表格结构

Col1, Col2, EventType,  DateTime

如何选择Col1Col2的每个分组记录,该记录位于EventType = 3 Col1特定组Col2的最高记录之后}。

例如,包含以下数据

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

5 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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值大于子集中的相应值。

  5. 这是一个完整的查询:

    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
    ;
    

答案 2 :(得分: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

答案 3 :(得分:0)

获得预期结果

       Select   Col1, Col2, EventType, DateTime  from table where EventType<3

答案 4 :(得分: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