SQL Brain

This is your Brain on SQL

  • About this Blog

    I use PostgreSQL and love it a lot.

    Whenver I discover or conjeur something neat, I post it here!

  • Subscribe

Using DISTINCT with CASE

Posted by jrotenstein on September 23, 2010

I had an interesting situation where I wanted to count DISTINCT rows with variable values.

Imagine a table like this:

Customer City VIP
Acme Sydney true
Bally Sydney false
Bally Melbourne true
Costco Melbourne false
David Jones Sydney true
David Jones Melbourne true
Embraer Sydney false
Fastway Sydney true

Now, how would you count the number of distinct customers in each city by VIP status?

I’m looking for this result:

City VIPs Not VIPs
Melbourne 2 1
Sydney 3 2

You can’t just group by City and use COUNT(DISTINCT customer) because it has to be divided by VIP too. That’s when I realised that the DISTINCT keyword looks at the following expression rather than just a column name.

Here’s the code that can be used:

select
  city,
  count(distinct case when     vip then customer else null end) as VIPs,
  count(distinct case when not vip then customer else null end) as Not_VIPs
from table
group by city

The COUNT automatically ignores the null values, so it’s only count the number of distinct cities that are/are not VIPs.

That’s a very different way to use DISTINCT.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.