PostgreSQL select rows having same column values

IdleJatt
  | location_id  |  lat  |  long  |  speed  |
    ------------- ------- -------- --------- 
      101241        0.12    1.1       0.0    
    ------------- ------- -------- --------- 
      101242        0.12    1.1       0.0
    ------------- ------- -------- --------- 
      101243        0.12    1.1       0.0
    ------------- ------- -------- --------- 
      101244        1.25    0.74      7.4
    ------------- ------- -------- ---------

I want to select all locations where speed = 0 and lat && long are same

So from above example answer should be::

   | location_id  |
    --------------
        101241     
    --------------
        101242     
    --------------
        101243     
    --------------

Note:: Speed is constant 0 but lat and long depend on previous rows value

GMB

I actually read this as a gaps-and-islands problem, where you want adjacent rows that have the same latitude and longitude, and a speed of 0.

You could approach this with window functions: the difference between row numbers gives you the islands: you can then compute the lenght of each islands, and filter on those lenght is greater than 1 and whose speed is 0:

select *
from (
    select t.*, count(*) over(partition by lat, long, speed, rn1 - rn2) cnt
    from (
        select t.*, 
            row_number() over(order by location_id) rn1,
            row_number() over(partition by lat, long, speed order by location_id) rn2
        from mytable t
    ) t
) t
where speed = 0 and cnt > 1

Demo on DB Fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL Select the rows having same column value

From Dev

Subsetting rows with range of column having same values

From Dev

Select rows with same column values (Mysql)

From Dev

Select rows having same column value more than 3 times

From Dev

select rows in mysql having another column with same value

From Dev

MySQL - Select multiple rows with the same values with one having to be different

From Dev

Total Values of Rows having same value in specific column

From Dev

Select rows with same column

From Dev

SQL - Select rows where all values in a column are the same

From Dev

Select rows that have two different values in same column

From Dev

Select distinct rows where all values in a column are the same SQL Server

From Dev

SQL - Select rows where all values in a column are the same

From Dev

Select "group" of rows with same column values(1 or more)

From Dev

Find rows with same values PostgreSQL

From Dev

Find rows with same values PostgreSQL

From Dev

SQL Server : how to select the rows in a table with the same value on a column but some exact values on another column for the grouped rows

From Dev

Oracle select two (or more) adjacent rows having the same value for a given column

From Dev

Linux: Merging rows by the columns having same values

From Dev

Merge rows having same values in multiple columns

From Dev

Linux: Merging rows by the columns having same values

From Dev

How to select rows having column value as null?

From Dev

Select rows grouped by a column having max aggregate

From Dev

SQLAlchemy - Select rows with the same column values, excluding those with values from different column

From Dev

SQL find rows not having duplicate column values

From Dev

Select rows where column 1 value is the same but column 2 value is different in PostgreSQL

From Dev

Copy values of a row in to column having same key

From Dev

PostgreSQL Select rows based on combination of array values

From Dev

PostgreSQL Select rows based on combination of array values

From Dev

Aggregation of rows with interchanged column values in PostgreSQL

Related Related

  1. 1

    MySQL Select the rows having same column value

  2. 2

    Subsetting rows with range of column having same values

  3. 3

    Select rows with same column values (Mysql)

  4. 4

    Select rows having same column value more than 3 times

  5. 5

    select rows in mysql having another column with same value

  6. 6

    MySQL - Select multiple rows with the same values with one having to be different

  7. 7

    Total Values of Rows having same value in specific column

  8. 8

    Select rows with same column

  9. 9

    SQL - Select rows where all values in a column are the same

  10. 10

    Select rows that have two different values in same column

  11. 11

    Select distinct rows where all values in a column are the same SQL Server

  12. 12

    SQL - Select rows where all values in a column are the same

  13. 13

    Select "group" of rows with same column values(1 or more)

  14. 14

    Find rows with same values PostgreSQL

  15. 15

    Find rows with same values PostgreSQL

  16. 16

    SQL Server : how to select the rows in a table with the same value on a column but some exact values on another column for the grouped rows

  17. 17

    Oracle select two (or more) adjacent rows having the same value for a given column

  18. 18

    Linux: Merging rows by the columns having same values

  19. 19

    Merge rows having same values in multiple columns

  20. 20

    Linux: Merging rows by the columns having same values

  21. 21

    How to select rows having column value as null?

  22. 22

    Select rows grouped by a column having max aggregate

  23. 23

    SQLAlchemy - Select rows with the same column values, excluding those with values from different column

  24. 24

    SQL find rows not having duplicate column values

  25. 25

    Select rows where column 1 value is the same but column 2 value is different in PostgreSQL

  26. 26

    Copy values of a row in to column having same key

  27. 27

    PostgreSQL Select rows based on combination of array values

  28. 28

    PostgreSQL Select rows based on combination of array values

  29. 29

    Aggregation of rows with interchanged column values in PostgreSQL

HotTag

Archive