You can use window functions and aggregation:
select date, iso_week, sum(case when rn = 1 then 1 else 0 end) cnt
from (
select t.*,
row_number() over(partition by userid, iso_week order by date) min_date
from mytable t
) t
group by date, iso_week
Better yet, using the standard where
clause to aggregate functions, which Presto supports:
select date, iso_week, count(*) filter(where rn = 1) cnt
from (
select t.*,
row_number() over(partition by userid, iso_week order by date) min_date
from mytable t
) t
group by date, iso_week
CLICK HERE to find out more related problems solutions.