SQL query: same rows

ABC

I'm having trouble finding the right sql query. I want to select all the rows with a unique x value and if there are rows with the same x value, then I want to select the row with the greatest y value. As an example I've put a part of my database below.

    ID  x   y
    1   2   3
    2   1   5
    3   4   6
    4   4   7
    5   2   6

The selected rows should then be those with ID 2, 4 and 5.

This is what I've got so far

SELECT *
FROM base
WHERE x IN
     (
          SELECT x 
          FROM base
          HAVING COUNT(*) > 1
     )

But this only results in the rows that occur more than once. I've added the tags R, postgresql and sqldf because I'm working in R with those packages.

Gordon Linoff

Here is a typical way to formulate the query in ANSI SQL:

select b.*
from base b
where not exists (select 1
                  from base b2
                  where b2.x = b.x and
                        b2.y > b.y
                 );

In Postgres, you would use distinct on for performance:

select distinct on (x) b.*
from base b
order by x, y desc;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL query: same rows

From Dev

SQL query to join rows with same values

From Dev

SQL Query for finding rows with same set of values

From Dev

PHP SQL query not returning same rows as in phpMyAdmin

From Dev

SQL PIVOT query is repeating the same values for all pivoted rows

From Dev

SQL Delete rows based on query from same table?

From Dev

Count rows from another SQL table in the same query

From Dev

SQL Query - Return rows where based on dual conditioning of same column

From Dev

SQL - SELECT a column that compares multiple rows of the same query

From Dev

sql query to return all rows with same column value

From Dev

SQL Delete rows based on query from same table?

From Dev

SQL Query seems not to affect the same number of rows, Adding a count statement

From Dev

SQL Query: Deleting rows from PostgreSQL with same values

From Dev

How can I stop SQL Server query from returning multile rows for the same item ID in this query

From Dev

sql query to extract rows

From Dev

Sql query for AND on multiple rows

From Dev

SQL Query returns multiple rows of the same record when View includes one-to-many table

From Dev

SQL query that finds a negative change between two rows with the same name field

From Dev

Merge MY SQL query result with multiple rows for same item id into single row for json response

From Dev

SQL Query returns multiple rows of the same record when View includes one-to-many table

From Dev

SQL Query. limit an update per rows if condition is X and Y for the same ID number

From Dev

SQL Query to return the sum of balances from 1 or more rows from the same table

From Dev

sql: select rows which have the same column value in another column with a single query

From Dev

Get 'balanced' column value as 'Y' if both rows has same amt and 'N' if not in SQL query

From Dev

Merging sql rows with same values

From Dev

Merging sql rows with same values

From Dev

combining rows in same table sql

From Dev

Copying rows via sql query

From Dev

SQL Query fetching wrong rows

Related Related

  1. 1

    SQL query: same rows

  2. 2

    SQL query to join rows with same values

  3. 3

    SQL Query for finding rows with same set of values

  4. 4

    PHP SQL query not returning same rows as in phpMyAdmin

  5. 5

    SQL PIVOT query is repeating the same values for all pivoted rows

  6. 6

    SQL Delete rows based on query from same table?

  7. 7

    Count rows from another SQL table in the same query

  8. 8

    SQL Query - Return rows where based on dual conditioning of same column

  9. 9

    SQL - SELECT a column that compares multiple rows of the same query

  10. 10

    sql query to return all rows with same column value

  11. 11

    SQL Delete rows based on query from same table?

  12. 12

    SQL Query seems not to affect the same number of rows, Adding a count statement

  13. 13

    SQL Query: Deleting rows from PostgreSQL with same values

  14. 14

    How can I stop SQL Server query from returning multile rows for the same item ID in this query

  15. 15

    sql query to extract rows

  16. 16

    Sql query for AND on multiple rows

  17. 17

    SQL Query returns multiple rows of the same record when View includes one-to-many table

  18. 18

    SQL query that finds a negative change between two rows with the same name field

  19. 19

    Merge MY SQL query result with multiple rows for same item id into single row for json response

  20. 20

    SQL Query returns multiple rows of the same record when View includes one-to-many table

  21. 21

    SQL Query. limit an update per rows if condition is X and Y for the same ID number

  22. 22

    SQL Query to return the sum of balances from 1 or more rows from the same table

  23. 23

    sql: select rows which have the same column value in another column with a single query

  24. 24

    Get 'balanced' column value as 'Y' if both rows has same amt and 'N' if not in SQL query

  25. 25

    Merging sql rows with same values

  26. 26

    Merging sql rows with same values

  27. 27

    combining rows in same table sql

  28. 28

    Copying rows via sql query

  29. 29

    SQL Query fetching wrong rows

HotTag

Archive