Top "Greatest-n-per-group" questions

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

PostgreSQL MAX and GROUP BY

I have a table with id, year and count. I want to get the MAX(count) for each id and …

sql postgresql group-by max greatest-n-per-group
In SQL, how to select the top 2 rows for each group

I have a table as following: NAME SCORE ----------------- willy 1 willy 2 willy 3 zoe 4 zoe 5 zoe 6 Here's the sample The aggregation …

mysql sql group-by greatest-n-per-group
How to get the last record per group in SQL

I am facing a rather interesting problem. I have a table with the following structure: CREATE TABLE [dbo].[Event] ( Id …

sql-server-2008 tsql sql-server-2005 greatest-n-per-group
How to make a SQL query for last transaction of every account?

Say I have a table "transactions" that has columns "acct_id" "trans_date" and "trans_type" and I want to …

sql greatest-n-per-group
PostgreSQL, SELECT from max id

By using libpq on PG 9.1, I am trying to write query to get values from row with highest index 'my_…

sql postgresql greatest-n-per-group
Select the top 1 row from each group

I have a table that lists the versions of software that are installed: id | userid | version | datetime ----+--------+---------+…

sql sql-server-2012 group-by greatest-n-per-group
How do I limit a LEFT JOIN to the 1st result in SQL Server?

I have a bit of SQL that is almost doing what I want it to do. I'm working with three …

sql sql-server sql-server-2000 greatest-n-per-group
Get most common value for each value of another column in SQL

I have a table like this: Column | Type | Modifiers ---------+------+----------- country | text | food_id | int | eaten | date | And …

sql postgresql greatest-n-per-group
Simple Query to Grab Max Value for each ID

OK I have a table like this: ID Signal Station OwnerID 111 -120 Home 1 111 -130 Car 1 111 -135 Work 2 222 -98 Home 2 222 -95 …

sql sql-server max greatest-n-per-group
Optimize GROUP BY query to retrieve latest row per user

I have the following log table for user messages (simplified form) in Postgres 9.2: CREATE TABLE log ( log_date DATE, user_…

sql postgresql indexing greatest-n-per-group postgresql-performance