SQL Query for max value from subsets

Aaron Watkins

I have a table bls_jobs with the following columns: city, state, occ_title, jobs_1000, and loc_quotient

I am trying to retrieve the highest loc_quotient for each city (each city has several occ_titles, and each occ_title has a loc_quotient)

Currently, I can use this query:

SELECT *
FROM bls_jobs
WHERE city = 'Hattiesburg'
ORDER BY loc_quotient DESC
LIMIT 1

Which does return what I'm looking for (the highest loc_quotient in the city, with each of the columns returned), but I'm struggling to figure out how to have it do this for all cities so I have a returned output of just each city's highest loc_quotient along with it's data from the other columns ...

Gordon Linoff

Use distinct on:

SELECT DISTINCT ON (j.city) j.*
FROM bls_jobs j
ORDER BY j.city, j.loc_quotient DESC;

DISTINCT ON is a convenient Postgres extension. It returns the first row in each group, where groups are the keys in the DISTINCT ON () clause (and the ORDER BY is consistent with them).

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

oracle-sql query select max from each base

分類Dev

Getting value from MAX(Date) Row in SQL Server

分類Dev

Find max value in sql table

分類Dev

SQL get value based on corresponding min/max(value) from column in another related table

分類Dev

Using SQL Query to return value from BigQuery User Defined Function

分類Dev

max value of no autonincrement column in update query - Oracle

分類Dev

No results from SQL Query

分類Dev

No results from SQL Query

分類Dev

SQL select distinct value where Status = MAX

分類Dev

Selecting MAX on column then MAX from column that is dependent on first value

分類Dev

Return decimal value in SQL query

分類Dev

How to get max value from a json?

分類Dev

dart flutter max value from list of objects

分類Dev

php get max key from same value

分類Dev

Selecting a max value from a pivot table

分類Dev

Mysql - Return max value from joined table

分類Dev

X++ query to SELECT MAX value of String type field

分類Dev

Oracle - SQL query to bin the data from one table to another based on a column value?

分類Dev

Oracle - SQL query to bin the data from one table to another based on a column value?

分類Dev

SQLITE_MAX_VARIABLE_NUMBER increase or break sql query into chunks

分類Dev

Substract query from another query in SQL Server

分類Dev

Get another value from row with max value with group by clause

分類Dev

Convert a query from SQL to CodeIgniter

分類Dev

Which method is better for finding max value in sql server?

分類Dev

SQL Returning rows with max value in column, within a specific range

分類Dev

SQL Query to group opening balance value

分類Dev

Can I use temporary value in sql query

分類Dev

Return value of sql query to textbox in C#

分類Dev

SQL query to get most prevalent value in a column

Related 関連記事

  1. 1

    oracle-sql query select max from each base

  2. 2

    Getting value from MAX(Date) Row in SQL Server

  3. 3

    Find max value in sql table

  4. 4

    SQL get value based on corresponding min/max(value) from column in another related table

  5. 5

    Using SQL Query to return value from BigQuery User Defined Function

  6. 6

    max value of no autonincrement column in update query - Oracle

  7. 7

    No results from SQL Query

  8. 8

    No results from SQL Query

  9. 9

    SQL select distinct value where Status = MAX

  10. 10

    Selecting MAX on column then MAX from column that is dependent on first value

  11. 11

    Return decimal value in SQL query

  12. 12

    How to get max value from a json?

  13. 13

    dart flutter max value from list of objects

  14. 14

    php get max key from same value

  15. 15

    Selecting a max value from a pivot table

  16. 16

    Mysql - Return max value from joined table

  17. 17

    X++ query to SELECT MAX value of String type field

  18. 18

    Oracle - SQL query to bin the data from one table to another based on a column value?

  19. 19

    Oracle - SQL query to bin the data from one table to another based on a column value?

  20. 20

    SQLITE_MAX_VARIABLE_NUMBER increase or break sql query into chunks

  21. 21

    Substract query from another query in SQL Server

  22. 22

    Get another value from row with max value with group by clause

  23. 23

    Convert a query from SQL to CodeIgniter

  24. 24

    Which method is better for finding max value in sql server?

  25. 25

    SQL Returning rows with max value in column, within a specific range

  26. 26

    SQL Query to group opening balance value

  27. 27

    Can I use temporary value in sql query

  28. 28

    Return value of sql query to textbox in C#

  29. 29

    SQL query to get most prevalent value in a column

ホットタグ

アーカイブ