0

早上好,mysql 查询显示已完成日期字段和总行数的案例,我想根据总案例计算这个,但不确定我是否可以在一个查询中完成所有这些。例如当前查询输出

Aug Sep Nov Total
10  20  20  50

那是的代码

     SELECT * from(
     Select Count(b.CaseID) As TotDB 
     from tblcontacts a 
     Inner Join  tblcases b 
     On a.ContactID = b.ContactAssignedTo)a
CROSS JOIN
    (Select
  Sum(Month(b.StatusSubmittedDate) = 8) As Aug,
  Sum(Month(b.StatusSubmittedDate) = 9) As Sep,
  Sum(Month(b.StatusSubmittedDate) = 10) As Oct,
  Count(b.CaseID) As Total,
  ROUND (100*Count(b.CaseID)/Count(b.CaseID),2) As Conversion
From
  tblcontacts a Inner Join
  tblcases b On a.ContactID = b.ContactAssignedTo
Where
  b.StatusSubmittedDate > '2012 - 01 - 01'
Group By
  a.ContactFullName With Rollup
Having
  Sum(b.CaseCommission) > 0.01)b 

我需要它的输出如下,所以我在上面添加了 TotDB 行,看看是否有帮助,但没有。我需要知道的是,我能否在此查询中有一个列绕过 where/have 子句来显示所有记录

Aug Sep Nov Tot TotDB %Converted
10  20  20  50  100   50%

谢谢

4

2 回答 2

1

也许你应该这样做:

select Aug,Sep, Nov, Tot,TotDB,(Tot/TotDB*1.0)*100 as '%Converted' 
 from 
(SELECT * from(
     Select Count(b.CaseDate) As TotDB 
     from tblcontacts a 
     Inner Join  tblcases b 
     On a.ContactID = b.ContactAssignedTo)a
CROSS JOIN
    (select  
      Sum(Month(b.StatusSubmittedDate) = 9) As Sep,
      Sum(Month(b.StatusSubmittedDate) = 10) As Oct,
      Sum(Month(b.StatusSubmittedDate) = 11) As Nov,
      Count(b.CaseID) As Total,
    From tblcontacts a 
    Inner Join tblcases b 
    On a.ContactID = b.ContactAssignedTo
    Where
      b.StatusSubmittedDate > '2012-01-01'
    Group By
      a.ContactFullName With Rollup
    Having
      Sum(b.CaseCommission) > 0.01)b)c
于 2012-09-06T12:43:24.643 回答
0

您不能在一个简单的查询中做到这一点。

正确的方法是执行第二个查询,不使用 where 子句。真的。

如果您必须在一个查询中执行此操作,请至少使用一个联合:

/* old query */
SELECT a,b,c from t1,t2,t3 where d=e group by a having b>f
UNION
select 'total',COUNT(*),NULL /*need the same amount of rows*/
from t1,t2,t3 

并让您的客户端以不同的方式处理该行a='total'。但这是一个黑客。我建议您使用两个查询。

于 2012-09-06T12:53:09.160 回答