简体   繁体   English

MySQL交叉表查询百分比计数?

[英]Mysql crosstab query percentage on count?

Good morning, is it possible to calculate the percentage of 2 calculated columns from a crosstab query, my cut down query is below? 早上好,是否可以从交叉表查询中计算2个计算列的百分比,我的缩减查询如下?

Select
  a.ContactFullName As Adviser,
  Sum(Month(b.CaseDate) = 1) As Jan,
  Sum(Month(b.CaseDate) = 2) As Feb,
  Sum(Month(b.CaseDate) = 3) As Mar,
  Count(b.CaseID) As Total,
  Count(b.StatusSubmittedDate) As Comms

From
  tblcontacts a Inner Join
  tblcases b On a.ContactID = b.ContactAssignedTo
Group By
  a.ContactFullName With Rollup

This is outputting the months as columns displaying cases in each month, Total columns counts the row while Comms column counts only cases with a date. 这会将月份输出为显示每个月案例的列,总计列对行进行计数,而通讯列仅对具有日期的案例进行计数。 What I would like to do is calculate the percentage of Total/Comms; 我想做的是计算Total / Comms的百分比;

Total | Comms | %_Share
100   | 50    | 50%
346   | 53    | 15%
278   | 89    | 32%

Am I looking at the the wrong way, should I be creating multiple queries to achieve? 我在寻找错误的方式吗,我应该创建多个查询来实现吗?

Thanks for looking 谢谢看

Simply select another column: 只需选择另一列:

Select …,
  100*Count(b.StatusSubmittedDate)/Count(b.CaseID) As `%_Share`
From …

This is 100*Comms/Total, the way your expected output indicates, not Total/Comms as you wrote in the text. 这是100 * Comms / Total,这是预期输出指示的方式,而不是您在文本中编写的Total / Comms。

Directly reusing one calculated column to compute the value in another calculated column isn't possible, as far as I know. 据我所知,不可能直接重用一个计算列来计算另一计算列中的值。 But the overhead for repeated evaluation of the Count shouldn't be too severe, and perhaps MySQL will even detect such common terms and compute them only once, I don't know. 但是,重复Count的开销应该不会太严重,也许MySQL甚至会检测到这些通用术语并只计算一次,我不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM