Only select first row of repeating value in a column in SQL

前端 未结 4 1875
南方客
南方客 2021-02-08 10:07

I have table that has a column that may have same values in a burst. Like this:

+----+---------+
| id |   Col1  | 
+----+---------+
| 1  | 6050000 |
+----+----         


        
相关标签:
4条回答
  • 2021-02-08 10:16

    Since id is always sequential, with no gaps or repetitions, as per your comment, you could use the following method:

    SELECT t1.*
    FROM atable t1
      LEFT JOIN atable t2 ON t1.id = t2.id + 1 AND t1.Col1 = t2.Col1
    WHERE t2.id IS NULL
    

    The table is (outer-)joined to itself on the condition that the left side's id is one greater than the right side's and their Col1 values are identical. In other words, the condition is ‘the previous row contains the same Col1 value as the current row’. If there's no match on the right, then the current record should be selected.


    UPDATE

    To account for non-sequential ids (which, however, are assumed to be unique and defining the order of changes of Col1), you could also try the following query:

    SELECT t1.*
    FROM atable t1
      LEFT JOIN atable t2 ON t1.id > t2.id
      LEFT JOIN atable t3 ON t1.id > t3.id AND t3.id > t2.id
    WHERE t3.id IS NULL
      AND (t2.id IS NULL OR t2.Col1 <> t1.Col1)
    

    The third self-join is there to ensure that the second one yields the row directly preceding that of t1. That is, if there's no match for t3, then either t2 contains the preceding row or it's got no match either, the latter meaning that t1's current row is the top one.

    0 讨论(0)
  • 2021-02-08 10:22

    If your RDBMS supports Window Aggregate functions and/or LEAD() and LAG() functions you can leverage them to accomplish what you are trying to report. The following SQL will help get you started down the right path:

    SELECT id
         , Col AS CurCol
         , MAX(Col)
           OVER(ORDER BY id ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS PrevCol
         , MIN(COL)
           OVER(ORDER BY id ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING) AS NextCol
    FROM MyTable
    

    From there you can put that SQL in a derived table with some CASE logic that if the NextCol or PrevCol is the same as CurCol then set CurCol = NULL. Then you can collapse eliminate all the id records CurCol IS NULL.

    If you don't have the ability to use window aggregates or LEAD/LAG functions your task is a little more complex.

    Hope this helps.

    0 讨论(0)
  • 2021-02-08 10:25
    select min(id), Col1 from tableName group by Col1 
    
    0 讨论(0)
  • 2021-02-08 10:38

    You can use a EXISTS semi-join to identify candidates:

    Select wanted rows:

    SELECT * FROM tbl t
    WHERE  NOT EXISTS (
        SELECT *
        FROM   tbl
        WHERE  col1 = t.col1
        AND    id = t.id - 1
        )
    ORDER  BY id;
    

    Get rid of unwanted rows:

    DELETE FROM tbl AS t
    -- SELECT * FROM tbl t  -- check first?
    WHERE EXISTS (
        SELECT *
        FROM   tbl
        WHERE  col1 = t.col1
        AND    id   = t.id - 1
        );
    

    This effectively deletes every row, where the preceding row has the same value in col1, thereby arriving at your set goal: only the first row of every burst survives.

    I left the commented SELECT statement because you should always check what is going to be deleted before you do the deed.

    Solution for non-sequential IDs:

    If your RDBMS supports CTEs and window functions (like PostgreSQL, Oracle, SQL Server, ... but not SQLite prior to v3.25, MS Access or MySQL prior to v8.0.1), there is an elegant way:

    WITH cte AS (
        SELECT *, row_number() OVER (ORDER BY id) AS rn
        FROM   tbl
        )
    SELECT id, col1
    FROM   cte c
    WHERE  NOT EXISTS (
        SELECT *
        FROM   cte
        WHERE  col1 = c.col1
        AND    rn   = c.rn - 1
        )
    ORDER  BY id;
    

    Another way doing the job without those niceties (should work for you):

    SELECT id, col1
    FROM   tbl t
    WHERE  (
        SELECT col1 = t.col1
        FROM   tbl
        WHERE  id < t.id
        ORDER  BY id DESC
        LIMIT  1) IS NOT TRUE
    ORDER  BY id;
    
    0 讨论(0)
提交回复
热议问题