早上好,是否可以从交叉表查询计算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
这是将月份输出为显示每个月的案例的列,总列数计算行,而Comms列仅计算具有日期的案例。我想做的是计算总数/通信的百分比;
Total | Comms | %_Share
100 | 50 | 50%
346 | 53 | 15%
278 | 89 | 32%
我是否以错误的方式查看,我应该创建多个查询来实现吗?
感谢您寻找
答案 0 :(得分:1)
只需选择其他列:
Select …,
100*Count(b.StatusSubmittedDate)/Count(b.CaseID) As `%_Share`
From …
这是100 * Comms / Total,这是您预期输出的方式,而不是您在文中所写的Total / Comms。
据我所知,直接重用一个计算列来计算另一个计算列中的值是不可能的。但重复评估Count
的开销不应该太严重,也许MySQL甚至会检测到这样的常用术语,只计算一次,我不知道。