有没有办法计算百分比
Is there a way to count and calculate percentages
在这张图片中,
- 我想计算每个 header1 组的 header5 的出现次数。
- 另外我想知道header5中每次出现的百分比
对于 header1
中的每个元素
试试这个:
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
在这张图片中,
- 我想计算每个 header1 组的 header5 的出现次数。
- 另外我想知道header5中每次出现的百分比 对于 header1 中的每个元素
试试这个:
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