In this image,
- I would like to count the occurrence of header5 for each header1 group.
- Plus I want to know the percentage of each occurrence in header5 for each of the elements in header1
Advertisement
Answer
Try this:
with cte_count as( SELECT header1 , count(header1) as total FROM tb_name group by header1 ), cte_sum as ( SELECT header1, header5,count(header5) as ct FROM tb_name group by header1, header5 ) Select cte_sum.*, (cte_sum.ct*1.0*100 / cte_count.total) as percentage From cte_sum left join cte_count on cte_sum.header1 = cte_count.header1