I am trying to make a new table that shows the number of Active Credit Cards each customer has in my dataset using:
SELECT
sk_id_curr,
credit_type,
credit_active,
COUNT(credit_type)
FROM
BUREAU
WHERE
credit_active='Active' AND credit_type='Credit card'
GROUP BY
sk_id_curr,
credit_type,
credit_active
The problem is that it removes ID's where there are no Active Credit Cards. I want it to return ID's with no credit cards with count 0. How would I do this?
Ex:
Original Data
-------------------------------------------------
id type activity
--------------------------------------------------
10001 Consumer Credit Active
10002 Credit Card Active
10002 Credit Card Active
10003 Credit Card Closed
Current result
-----------------------------------------------------------------------------
id, type, activity, count
------------------------------------------------------------------------------
10002 Credit Card Active 2
desired result
-----------------------------------------------------------------------------
id type activity count
------------------------------------------------------------------------------
10001 Credit Card Active 0
10002 Credit Card Active 2
10003 Credit Card Active 0
Best Answer
db<>fiddle here
Or using the conditional aggregation pointed out by a_horse_with_no_name:
db<>fiddle here