Capturing the record with the highest value for one column in a single query?

bpromas

I have a table that looks something like this:

| id | fk1 | fk2 | version |
|  1 |  1  |  1  |    1    |
|  2 |  1  |  1  |    2    |
|  3 |  1  |  1  |    3    |

Having on hand the values of fk1 and fk2 I am trying to get the record with the highest value for version. Currently what I am doing is this:

version = Project.where("fk1= ? AND fk2= ?", params.require(:fk1), params.require(:fk2)).maximum(:version)
@project = Project.find_by_fk1_and_fk2_and_version(params.require(:fk1), params.require(:fk2), version)

This gets me the correct record, but I have to execute 2 queries for something that seems really simple in theory, but after trying a number of different things I had no luck with doing this with a single query. I am envisioning something like:

version = Project.where("fk1= ? AND fk2= ? AND max(version)", params.require(:fk1), params.require(:fk2))

or something.

xlembouras

Well the rails way to do that is

Project.where(fk1: params.require(:fk1), fk2: params.require(:fk2)).
  order('version desc').first

Which translates to an sql query like:

SELECT * FROM projects WHERE fk1 = "fk1" AND fk2 = "fk2" ORDER BY version DESC LIMIT 1;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

SQL query to change column names to single column value

From Dev

Selecting single hash from array where one hash element has the highest value

From Dev

Get one record per unique column value

From Dev

Select Highest value against each record in SQL

From Dev

How do I select the highest column value in an SQL query IF another column is equal to a certain string

From Dev

Query: Select and display next highest value as new column

From Dev

MySQL select a single record from highest join value from multiple tables with multiple records

From Dev

how to get only one record based on column value in oracle join

From Dev

Sub query to fetch the highest value

From Dev

SQL Select Query Highest Value

From Dev

Transforming json with power query (mix of list and record in a single column)

From Dev

Sqlalchemy single query for multiple rows from one column in one table

From Dev

Query or Filter the Highest Value with Conditions

From Dev

Python: In DataFrame, add value in a new column for row with highest value in another column and string identical in a third one

From Dev

Update a single column on multiple rows with one SQL query

From Dev

Aerospike Query Return Highest Value

From Dev

Query to select Single Best Record based on a Column Value

From Dev

Find highest value in column insert a new record 1 number higher in said column

From Dev

Select top one record for each unique value of column

From Dev

output the column name with the highest value

From Dev

How do I record the highest value in a variable

From Dev

Finding second highest value from record in mongodb

From Dev

Coping value of one column entry to another of same record ,If the value is null

From Dev

How to check if the column value are the same among multiple record with one sql

From Dev

Extract value of single string record from PostgreSQL JSON column

From Dev

MySQL query with a date range selection but also need to grab the highest value of one column when another column has duplicates

From Dev

Excel - highlight the highest values in a column if there is more than one highest value when rounded

From Dev

How to insert data for one column for all the rows in single query in Mysql?

From Dev

Linux; sort data and print only highest value of one column

Related Related

  1. 1

    SQL query to change column names to single column value

  2. 2

    Selecting single hash from array where one hash element has the highest value

  3. 3

    Get one record per unique column value

  4. 4

    Select Highest value against each record in SQL

  5. 5

    How do I select the highest column value in an SQL query IF another column is equal to a certain string

  6. 6

    Query: Select and display next highest value as new column

  7. 7

    MySQL select a single record from highest join value from multiple tables with multiple records

  8. 8

    how to get only one record based on column value in oracle join

  9. 9

    Sub query to fetch the highest value

  10. 10

    SQL Select Query Highest Value

  11. 11

    Transforming json with power query (mix of list and record in a single column)

  12. 12

    Sqlalchemy single query for multiple rows from one column in one table

  13. 13

    Query or Filter the Highest Value with Conditions

  14. 14

    Python: In DataFrame, add value in a new column for row with highest value in another column and string identical in a third one

  15. 15

    Update a single column on multiple rows with one SQL query

  16. 16

    Aerospike Query Return Highest Value

  17. 17

    Query to select Single Best Record based on a Column Value

  18. 18

    Find highest value in column insert a new record 1 number higher in said column

  19. 19

    Select top one record for each unique value of column

  20. 20

    output the column name with the highest value

  21. 21

    How do I record the highest value in a variable

  22. 22

    Finding second highest value from record in mongodb

  23. 23

    Coping value of one column entry to another of same record ,If the value is null

  24. 24

    How to check if the column value are the same among multiple record with one sql

  25. 25

    Extract value of single string record from PostgreSQL JSON column

  26. 26

    MySQL query with a date range selection but also need to grab the highest value of one column when another column has duplicates

  27. 27

    Excel - highlight the highest values in a column if there is more than one highest value when rounded

  28. 28

    How to insert data for one column for all the rows in single query in Mysql?

  29. 29

    Linux; sort data and print only highest value of one column

HotTag

Archive