我有以下字段。
state plant edition declaration version date state_by
s1 p1 e1 0 1 2019-07-22 null
s1 p1 e1 1 2 2019-07-22 1
需要在单个查询中计算 edition wise, declaration=1, max (version) of date wise and state_by=1
数据。
计算版本%的条件
declaration=1/ declaration *100
datewise
max version
state_by=1
edition wise
最佳答案
你的问题不太清楚,但你的标题表明你关注的是申报的平均值。
你可以用这样的逻辑来做到这一点:
select edition, avg( declaration = 1 ) as avg_1,
max(version)
from t
group by edition;
MySQL 将 bool 值视为数字上下文中的整数,“1”表示真,“0”表示假。因此,avg()
返回真实值的比例。
编辑:
如果您想要使用 declaration = 1
的版本百分比,那么一种方法是:
select count(distinct case when declaration = 1 then edition end) / count(distinct edition)
from t;
关于mysql - 从表中计算申报百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57145762/