Given a JSON payload containing an array of medical diagnoses, write a query to unpack the array and count occurrences of each diagnosis.
Use the medical_payloads table. Ignore rows with a missing, non-array, null, or blank diagnosis value. Preserve distinct diagnosis text after trimming surrounding whitespace.
diagnosis and occurrence_count.occurrence_count descending, then diagnosis ascending.| Column | Type | Description |
|---|---|---|
| payload_idPK | INT | Unique identifier for the JSON payload |
| payload | JSONB | JSON document that may contain a diagnoses array |