Skip to content
Advertisement

Get rows with a condition

Each customer can have one or multiple accounts (account_id)

How can I get max closed_date for the customers who closed all their accounts and the rest of the active customers?*

*active customers are the ones with at least one open account.

Desired output:

Advertisement

Answer

You can use distinct on for this:

This returns one row per customer_id based on the order by clause.

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