簡體   English   中英

從表中計算申報的百分比

[英]Calculate percentage of declaration from table

我有以下領域。

 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在數字上下文中將布爾值視為整數,“ 1”為true,“ 0”為false。 因此, avg()返回真實值的比例。

編輯:

如果要使用帶有declaration = 1的版本百分比,則一種方法是:

select count(distinct case when declaration = 1 then edition end) / count(distinct edition)
from t;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM