我有mysql查询,输出多年的销售额和销售额,并计算2012年的转化率,因为它只持有一年的数据。现在它的2013年我需要改变它的工作方式,但已经陷入困境。如果2012年12月有一个销售线索,然后在2013年成为销售,那么它将从我的查询输出中省略,因为它只要求2013年的CaseDate。
Select
q.*,
ROUND(100 * q.Comms / q.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,
..... Dec
Sum(Case
When Year(b.CaseDate) = 2013 And Month(b.CaseDate) Between '1'
And '12' Then 1 Else 0 End) As Total,
Sum(Case When (Year(b.CaseDate) = 2012 Or Year(b.CaseDate) = 2013) And Year(b.StatusSubmittedDate) = ".(int)$_POST['year']." And Month(b.StatusSubmittedDate) Between ".(int)$_POST['start']." And ".(int)$_POST['end']." 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
这有效;
Sum(Case When (Year(b.CaseDate) = 2012 Or Year(b.CaseDate) = 2013) And Year(b.StatusSubmittedDate) = 2013 And Month(b.CaseDate) Between '1' And '12' Then 1 Else 0 End) As Comms
这显示导致进入&于2013年提交;
Sum(Case When (Year(b.CaseDate) = 2012 Or Year(b.CaseDate) = 2013) And Year(b.StatusSubmittedDate) = ".(int)$_POST['year']." And Month(b.CaseDate) Between ".(int)$_POST['start']." And ".(int)$_POST['end']." Then 1 Else 0 End) As Comms
非常感谢
答案 0 :(得分:1)
这取决于您的业务逻辑。如果逻辑说您只显示同年出现的潜在客户,那么您的代码完全正确(原样?)。如果你想要包括2012年或2013年(你看起来像这样),你需要完全拼写出来,服务器不理解“2012或2013”
Sum(Case When (Year(b.CaseDate) = 2012 Or Year(b.CaseDate) = 2013) And Year(....
注意Or语句周围的括号,这是为了确保CaseYear=2012 or CaseYear=2013 and SubmitYear=2013
被视为(CaseYear=2012 or CaseYear=2013) and SubmitYear=2013
而不是CaseYear=2012 or (CaseYear=2013 and SubmitYear=2013)
当然,如果业务逻辑不关心CaseYear,那么将其从Sum中取出。