mysql pivot不计算Date列

时间:2015-06-09 11:44:02

标签: mysql pivot

我希望有人可以帮助我查看已经在另一个数据库上工作但现在失败的查询。

下面是一个数据透视表,左边是源,左边是几个月,所有工作正常,只有一列拒绝计算!

Count(l.CompDate) As Submitted输出与Count(l.id) As Leads相同的数字,l.ComDate字段是基于日期的列,如果列中有日期,则应该计算但不是,它只是计数所有记录。

有人能找到我错的地方吗?我已经好几天了。

Select *,   If(q.Source Is Null, 1, 0) As remove From  
(Select l.Source,      
Sum(Month(l.CompDate) = 1) As Jan, 
Sum(Month(l.CompDate) = 2) As Feb,
Sum(Month(l.CompDate) = 3) As Mar,       
Sum(Month(l.CompDate) = 4) As Apr,      
Sum(Month(l.CompDate) = 5) As May,       
Sum(Month(l.CompDate) = 6) As Jun,       
Sum(Month(l.CompDate) = 7) As Jul,       
Sum(Month(l.CompDate) = 8) As Aug,       
Sum(Month(l.CompDate) = 9) As Sep,       
Sum(Month(l.CompDate) = 10) As Oct,       
Sum(Month(l.CompDate) = 11) As Nov,       
Sum(Month(l.CompDate) = 12) As Decb,
Count(l.id) As Leads,
Count(l.CompDate) As Submitted,
ROUND(Count(l.id)*100 / (Count(l.CompDate)), 2) As Conversion
From       tbl_lead l     
Where       Year(l.LeadDate) = 2015     
Group By  l.Source With Rollup) q

1 个答案:

答案 0 :(得分:0)

表达式:

Count(l.CompDate) As Submitted,

你正在表达的是什么。它计算non-NULL的{​​{1}}值。

目前还不清楚你想要什么。如果您需要不同 date 值的数量,则可以使用以下其中一项:

l.CompDate