Skip to content
Advertisement

How can I count rows into more than one ‘then’ of a Postgres searched case expression? (reporting)

Background

I have a Postgres 11 database running on RDS. I need to produce a report which shows counts of events over a certain period, grouping, renaming, and counting them based on description strings into categories according to business requirements.

Problem For this sort of thing, I typically go for a searched case expression, which is working well except: Part of the spec requires some events must be counted into more than one category. But when the case expression is evaluated, it counts [or appears to count] each row into only one category [the first matching one in the expression].

Example:

So, given data like

output expected is like so:

However, the actual output is being returned as

How can I get records with description containing ‘foobar’ to be counted into both ‘foo’ and ‘bar’ categories? And if a case expression is the wrong method for this reporting goal, what should be used? All guidance appreciated!

Advertisement

Answer

The output stops at the first match. If you want both, use a lateral join:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement