Skip to content
Advertisement

Using a sub-select with a group by in SQL

I’m trying to get a sub-select query to return certain data. The below example is simplified for convenience:

Desired return:

The data is in the following tables:

org_users: id, fullname orders: id, status, user_id

Status can be “pending” or “complete” on the order.

I’m writing my query like so:

But this only returns this for me:

How can I make my subselect tie to the user_id when it does its count? Been a while since I wrote raw SQL so I’m having trouble getting this right

Advertisement

Answer

Use conditional aggregation:

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