SELECT only latest record of an ID from given rows

前端 未结 4 397
生来不讨喜
生来不讨喜 2021-01-26 01:58

I have this table shown below...How do I select only the latest data of the id based on changeno?

+----+--------------+------------+--------+
| id |  data   | ch         


        
相关标签:
4条回答
  • 2021-01-26 02:05

    This is typically referred to as the "greatest-n-per-group" problem. One way to solve this in SQL Server 2005 and higher is to use a CTE with a calculated ROW_NUMBER() based on the grouping of the id column, and sorting those by largest changeno first:

    ;WITH cte AS 
    (
      SELECT id, data, changeno,
        rn = ROW_NUMBER() OVER (PARTITION BY id ORDER BY changeno DESC)
      FROM dbo.Table1
    )
    SELECT id, data, changeno
      FROM cte
      WHERE rn = 1
      ORDER BY id;
    
    0 讨论(0)
  • 2021-01-26 02:13

    for other alternatives :

    DECLARE @Table1 TABLE
    (
     id INT, data VARCHAR(5), changeno INT
    );
    
    INSERT INTO @Table1
    SELECT 1,'Yes',1
    UNION ALL
    SELECT 2,'Yes',2
    UNION ALL
    SELECT 2,'Maybe',3
    UNION ALL
    SELECT 3,'Yes',4
    UNION ALL
    SELECT 3,'Yes',5
    UNION ALL
    SELECT 3,'No',6
    UNION ALL
    SELECT 4,'No',7
    UNION ALL
    SELECT 5,'Maybe',8
    UNION ALL
    SELECT 5,'Yes',9
    
    SELECT Y.id, Y.data, Y.changeno
    FROM @Table1 Y
    INNER JOIN (
        SELECT id, changeno = MAX(changeno) 
        FROM @Table1 
        GROUP BY id
    ) X ON X.id = Y.id 
    WHERE X.changeno = Y.changeno
    ORDER BY Y.id
    
    0 讨论(0)
  • 2021-01-26 02:24

    You want to use row_number() for this:

    select id, data, changeno
    from (SELECT t.*,
                 row_number() over (partition by id order by changeno desc) as seqnum
          FROM Table1 t
         ) t
    where seqnum = 1;
    
    0 讨论(0)
  • 2021-01-26 02:24

    Not a well formed or performance optimized query but for small tasks it works fine.

    SELECT * FROM TEST WHERE changeno IN (SELECT MAX(changeno) FROM TEST GROUP BY id)

    0 讨论(0)
提交回复
热议问题