skip to Main Content

I am trying to count the unique values of a position_id while also satisfy another criteria of "true" in another column. I keep getting all values returned since I am counting them.

COUNT(DISTINCT CASE WHEN p.position_id IS NOT NULL THEN 1
    WHEN internal_only = 'true' THEN 1 ELSE 0 END) AS I_Roles 

Any help would be much appreciated.

2

Answers


  1. You can count values for true values of internal_only using:

    count(distinct case when internal_only = 'true' then p.position_id end)
    
    Login or Signup to reply.
  2. You can use filtered aggregation:

    count(distinct position_id) filter (where internal_only = 'true')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search