Skip to content

Instantly share code, notes, and snippets.

@dpetrov
Created June 13, 2013 06:34
Show Gist options
  • Save dpetrov/5771628 to your computer and use it in GitHub Desktop.
Save dpetrov/5771628 to your computer and use it in GitHub Desktop.
SELECT
total.count,
detail.basetag,
detail.alarms_id
FROM
(
SELECT
basetag,
count(*)
FROM
alarms
GROUP BY basetag
ORDER BY
count DESC,
basetag ASC
) as total
JOIN
(
SELECT *,
row_number() OVER (PARTITION BY basetag ORDER BY firstoccurence ASC, alarms_id ASC)
FROM
alarms
) AS detail ON detail.basetag = total.basetag
WHERE row_number = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment