I have a table:
Date | ID | Company | Click
-----------+----+---------+--------
01/01/2021 | 01 | Us | 1
01/01/2021 | 01 | Us | 1
01/01/2021 | 01 | Other | 1
01/01/2021 | 02 | Us | 0
01/01/2021 | 02 | Other | 0
02/01/2021 | 03 | Us | 1
02/01/2021 | 03 | Us | 1
02/01/2021 | 04 | Us | 0
I want to group by date and count: how many different IDs there are per day, how many unique IDs have clicked=1
and Company="Us"
My current code is:
create table grouped as
select date
, count(distinct ID) as ID_count
, sum(case when company="Us" and clicked=1 then 1 else 0 end) as Click_count
from have
group by 1
The result should look like:
Date | ID_count | Click_count
-----------+----------+------------
01/01/2021 | 2 | 1
02/01/2021 | 2 | 1
You'll notice that my code counts duplicated ID's, so the click_count
column takes the value 2 in both dates. How can I fix that?