Dette burde gøre tricket:
SELECT a
, sum(ab_ct)::int AS ct_total
, count(*)::int AS ct_distinct_b
, array_agg(b || ', ' || ab_ct::text) AS b_arr
FROM (
SELECT a, b, count(*) AS ab_ct
FROM tbl
GROUP BY a, b
ORDER BY a, ab_ct DESC, b -- append "b" to break ties in the count
) t
GROUP BY a
ORDER BY ct_total DESC;
Returnerer:
ct_total
:samlet antalb
pr.a
.ct_distinct_b
:antal forskelligeb
pr.a
.b_arr
:matrix afb
plus frekvens afb
, sorteret efter frekvens afb
.
Sorteret efter det samlede antal b
pr. a
.
Alternativt kan du bruge en ORDER BY
klausul i det samlede kald
i PostgreSQL 9.0 eller nyere. Ligesom:
SELECT a
, sum(ab_ct)::int AS ct_total
, count(*)::int AS ct_distinct_b
, array_agg(b || ', ' || ab_ct::text ORDER BY a, ab_ct DESC, b) AS b_arr
FROM (
SELECT a, b, count(*) AS ab_ct
FROM tbl
GROUP BY a, b
) t
GROUP BY a
ORDER BY ct_total DESC;
Kan være tydeligere. Men det er typisk langsommere. Og sortering af rækker i en underforespørgsel fungerer for simple forespørgsler som denne. Mere forklaring: