Ich habe eine große Tabelle mit Zeile, die jeweils eine Standort-ID (location_id
) haben. In der Spalte location_id
gibt es mehrere Instanzen desselben Werts. Ich möchte zählen, wie oft jeder Wert angezeigt wird und dann diesen Wert in einen "Bucket" setzenPostgres COUNT Instanzen und in "Eimer"
Ich habe das versucht, aber endete mit allem im "500+" Eimer.
SELECT
CASE WHEN count(location_id) > 1 AND count(location_id) <= 25 THEN '1-25'
WHEN count(location_id) > 25 AND count(location_id) <= 500 THEN '26-500'
WHEN count(location_id) > 500 THEN '500+'
ELSE 'nothing'
end as bucket,
count(*) as Column1
FROM myTable
Was mache ich hier falsch?
Dank! Ich denke, das funktioniert perfekt. Ich habe jedoch 3 Spalten und ich bin nicht ganz sicher, was sie bedeuten. Der erste ist "Eimer", was klar ist. Was sind 'numlocations' und' numTotal'? – jonmrich