• I also posted this question in StackOverflow. Here is the solution they came up with:

    select DealerId, Tire, count(*) as RepairsOfThisTire,

    max(cnt) as TotalRepairs,

    count(*) * 1.0 / max(cnt) as PercentOfTotal

    from (select t1.*, t2.Tire,

    row_number() over (partition by t1.DealerId

    order by (case when t2.tire is null then 1 else 0 end),

    t1.RepairId

    ) as seqnum,

    count(*) over (partition by t1.DealerId) as cnt

    from table1 t1 left join

    table2 t2

    on t1.RepairId = t2.RepairId

    ) t

    where seqnum <= 0.8*cnt and tire is not null

    group by DealerId, Tire

    order by 1, 2;