How would you use SQL to find the second-highest value in a grouped dataset?
Write a PostgreSQL query using the provided grouped observations and group metadata. Treat the second-highest value as the second-highest distinct aggregate, and return all groups tied at that level.
group_name and grouped_value.grouped_value descending, then group_name ascending.| Column | Type | Description |
|---|---|---|
| group_idPK | INT | Unique identifier for a group |
| group_name | VARCHAR(100) | Readable group name |
| Column | Type | Description |
|---|---|---|
| observation_idPK | INT | Unique observation identifier |
| group_id | INT | Group associated with the observation |
| amount | NUMERIC(12,2) | Numeric value included in the group aggregate |