Home / Companies / Yugabyte / Blog / Post Details
Content Deep Dive

How to Select the First Row of Each Set of Grouped Rows Using GROUP BY

Blog post from Yugabyte

Post Details
Company
Date Published
Author
Franck Pachot
Word Count
2,204
Language
English
Hacker News Points
-
Summary

In summary, PostgreSQL allows the use of `DISTINCT ON` to select the first row of each group in a query, which is useful for retrieving specific data such as the latest measure for each metric in a timeseries database or the last contract with each supplier. This approach can be more efficient than using window functions or recursive common table expressions, especially when combined with YugabyteDB's storage layer optimizations. However, it requires careful consideration of index definitions and may not be standard SQL.