gpt4 book ai didi

MySQL - 比较百分比与连接

转载 作者:行者123 更新时间:2023-11-29 06:34:56 28 4
gpt4 key购买 nike

我有两个表(主表和标签)和以下 SQL:

select t.label, m.demographic_sex, count(*) from 
Master m
LEFT OUTER JOIN tags t ON t.interaction_id = m.interaction_id
where m.demographic_sex is not null
group by t.label, m.demographic_sex
;

这个输出:

label           demographic_sex     count(*)
------------------------------
label_a Female 117845
label_a Male 45966
BOTH Female 6476
BOTH Male 4406
label_b Female 364984
label_b Male 261089

如何计算每个标签的每个性别的百分比?

最佳答案

你能试试这个吗?

SELECT t.label, m.demographic_sex, COUNT(*), COUNT(*) / total_t.total_cnt
FROM Master m LEFT OUTER JOIN tags t ON t.interaction_id = m.interaction_id
INNER JOIN (SELECT label, COUNT(*) AS total_cnt
FROM tags
GROUP BY label
) total_t ON total_t.label = t.label
WHERE m.demographic_sex is not null
GROUP BY t.label, m.demographic_sex ;

total_t 具有每个 label 的总计数。

关于MySQL - 比较百分比与连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25516981/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com