Top "Greatest-n-per-group" questions

Query the row with the greatest/least value per group.

How do I get the oldest date without using a correlated subquery in SQL?

The following code list all the invoices, and I just want the oldest invoice from a vendor: SELECT DISTINCT vendor_…

sql oracle greatest-n-per-group correlated-subquery
Selecting all corresponding fields using MAX and GROUP BY

I have this table : And I would like to make a request that would return for each deal_id the …

sql mysql group-by max greatest-n-per-group
Joining onto a subquery in SQLAlchemy

I have the follwing SQL query (It get's the largest of a certain column per group, with 3 things to group …

python sql sqlalchemy greatest-n-per-group
SqlAlchemy group_by and return max date

I have a table such has identifier date value A 2017-01-01 2 A 2017-01-02 1 A 2017-01-03 7 B 2017-01…

python sqlalchemy greatest-n-per-group
Selecting rows ordered by some column and distinct on another

Related to - PostgreSQL DISTINCT ON with different ORDER BY I have table purchases (product_id, purchased_at, address_id) …

postgresql greatest-n-per-group distinct-on
mysql select top n max values

How can you select the top n max values from a table? For a table like this: column1 column2 1 foo 2 …

mysql greatest-n-per-group
Average of latest N records per group

My current application calculates a point average based on all records for each user: SELECT `user_id`, AVG(`points`) AS …

mysql average greatest-n-per-group limit-per-group
MySQL return first row of a joined table

I have two tables (country & ducks) where the country table has every country in the world and the ducks …

php mysql sql join greatest-n-per-group
SQL Select only rows with Minimum Value on a Column with Where Condition

Table: | id | productId | orderIndex | rejected | ------------------------------------------ | 1 | 1 | 0 | 1 | | 2 | 1 | 1 | 0 | | 3 | 1 | 2 | 0 | | 4 | 2 | 0 | 0 | | 5 | 2 | 1 | 1 | | 6 | 3 | 0 | 0 | How can I select one row per productId with minimum orderIndex that not rejected? …

mysql greatest-n-per-group
How do I limit the number of rows per field value in SQL?

For example, I have a table like this in Hive: 1 1 1 4 1 8 2 1 2 5 3 1 3 2 and I want to only return the first two rows …

mysql sql greatest-n-per-group hive