Mysql where子句打破计算查询

时间:2013-01-21 10:45:20

标签: mysql

想知道是否有人可以在下面的代码中指出错误。我有mysql查询,按月显示销售线索,然后计算转换为销售额的百分比。如果我在其中添加where子句,它会打破总列,因为它输出的内容与Comms列相同吗?

  Select
  q.*,
  ROUND(100 * Comms / Total, 2) As Conversion,
  If(q.Adviser Is Null, 1, 0) As remove
From
  (Select
      a.ContactFullName As Adviser,
      SUM(YEAR(b.CaseDate) = 2013 AND Month(b.CaseDate) = 1) AS Jan,
      SUM(YEAR(b.CaseDate) = 2013 AND Month(b.CaseDate) = 2) As Feb,
      SUM(YEAR(b.CaseDate) = 2013 AND Month(b.CaseDate) = 3) As Mar,
      SUM(YEAR(b.CaseDate) = 2013 AND Month(b.CaseDate) = 4) As Apr,
      SUM(YEAR(b.CaseDate) = 2013 AND Month(b.CaseDate) = 5) As May,
      SUM(YEAR(b.CaseDate) = 2013 AND Month(b.CaseDate) = 6) As Jun,
      SUM(YEAR(b.CaseDate) = 2013 AND Month(b.CaseDate) = 7) As Jul,
      SUM(YEAR(b.CaseDate) = 2013 AND Month(b.CaseDate) = 8) As Aug,
      SUM(YEAR(b.CaseDate) = 2013 AND Month(b.CaseDate) = 9) As Sep,
      SUM(YEAR(b.CaseDate) = 2013 AND Month(b.CaseDate) = 10) As Oct,
      SUM(YEAR(b.CaseDate) = 2013 AND Month(b.CaseDate) = 11) As Nov,
      SUM(YEAR(b.CaseDate) = 2013 AND Month(b.CaseDate) = 12) As Dece,
      Count(b.CaseID) As Total,
      Sum(Case When Year(b.StatusSubmittedDate) = 2013 Then 1 Else 0
      End) As Comms

    From
      tblcontacts a Inner Join

没有WHERE子句就输出;

Total - Comms - Conversion
479   -  148  - 30.9%

使用WHERE子句输出;

Total - Comms - Conversion
148   -  148  - 100%

我无法弄清楚为什么会这样?

亲切的问候

1 个答案:

答案 0 :(得分:1)

<强>更新

我重写了你的整个查询:

Select
  q.*,
  totals.total,
  ROUND(100 * Comms / totals.total, 2) As Conversion
  If(q.Adviser Is Null, 1, 0) As remove
From
  (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,
      Sum(Month(b.CaseDate) = 4) As Apr,
      Sum(Month(b.CaseDate) = 5) As May,
      Sum(Month(b.CaseDate) = 6) As Jun,
      Sum(Month(b.CaseDate) = 7) As Jul,
      Sum(Month(b.CaseDate) = 8) As Aug,
      Sum(Month(b.CaseDate) = 9) As Sep,
      Sum(Month(b.CaseDate) = 10) As Oct,
      Sum(Month(b.CaseDate) = 11) As Nov,
      Sum(Month(b.CaseDate) = 12) As Dece,
      Count(b.StatusSubmittedDate) As Comms

    From
      tblcontacts a Inner Join
      tblcases b On a.ContactID = b.ContactAssignedTo 
      WHERE Year(b.StatusSubmittedDate) = 2013
    Group By
      a.ContactFullName With Rollup) q
    inner join (select  a.ContactFullName As Adviser, count(*) as total from
    tblcontacts a Inner Join
      tblcases b On a.ContactID = b.ContactAssignedTo 
      WHERE Year(b.StatusSubmittedDate) = 2013
      group by Adviser
      )totals on q.Adviser = totals.Adviser

原始回答:

要拥有多个COUNTS,您可以解决此问题:

      Count(b.CaseID) As Total,
      SUM(CASE WHEN YEAR(b.StatusSubmittedDate) = 2013 THEN 1 ELSE 0 END) As Comms,

但是这个:

      ROUND(100 * Count(b.StatusSubmittedDate) / Count(b.CaseID),

不起作用,因为此计算必须在以后完成。

Select
  q.*,
  ROUND(100 * Comms / Total, 2) As Conversion,
  If(q.Adviser Is Null, 1, 0) As remove
From
  (Select
      a.ContactFullName As Adviser,
      Sum(Month(b.CaseDate) = 1 and year(b.CaseDate) = 2012) As Jan,
      ... Dec,
      Count(b.CaseID) As Total,
      SUM(CASE WHEN YEAR(b.StatusSubmittedDate) = 2013 THEN 1 ELSE 0 END) As Comms
    From
      tblcontacts a Inner Join
      tblcases b On a.ContactID = b.ContactAssignedTo 
    Group By
      a.ContactFullName With Rollup) q