ad_id
: Aggregate the data by ad_id
to calculate metrics for each ad.SUM(action = 'Clicked')
and SUM(action = 'Viewed')
.IFNULL
to replace NULL
with 0.ROUND
function to round the CTR to two decimal places.ad_id
in ascending order in case of a tie.ad_id
and the calculated ctr
for the final output.erDiagram Ads { int ad_id int user_id enum action ad_id user_id PK }