MySQL查询错误...它不显示确切的数据值

时间:2011-09-30 21:54:18

标签: mysql date

我想问一下我的MySQL查询是否有什么问题(可能会就如何更好地提出建议)?我正在尝试在5天的范围内生成每日报告...问题是它没有显示确切的数据,它填补了没有数据的日子。

以下是MySQL查询示例: 在我的数据库中,星期一,星期三星期四与数据相关..但如果我将使用此查询运行报告,则星期二将有一个现有数据,它实际上并不存在在我的数据库中。虽然 IT,NonIt 总计表计数是正确的。我在中统计数据时出错。请帮帮我......我不知道错误在哪里。

为了更好地解释我的意思,这是我的查询的输出结果的屏幕截图: http://www.fileden.com/files/2011/7/27/3174077//daily.JPG

SELECT a.specialist_partner_ID, ss.first_name as SS, ssa.first_name as SSA 

,count(CASE WHEN a.receivedDate BETWEEN '2011-09-5' AND '2011-09-9' 
THEN a.job_order_number ELSE null END) As MON

,count(CASE WHEN a.receivedDate BETWEEN date_add('2011-09-5', INTERVAL 1 DAY) 
AND DATE_ADD('2011-09-9', INTERVAL 1 DAY) THEN a.job_order_number ELSE null END) As TUE 

,count(CASE WHEN a.receivedDate BETWEEN date_add('2011-09-5', INTERVAL 2 DAY) 
AND DATE_ADD('2011-09-9', INTERVAL 2 DAY) THEN a.job_order_number ELSE null END) As WED

,count(CASE WHEN a.receivedDate BETWEEN date_add('2011-09-5', INTERVAL 3 DAY)
AND DATE_ADD('2011-09-9', INTERVAL 3 DAY) THEN a.job_order_number ELSE null END) As THU 

,count(CASE WHEN a.receivedDate BETWEEN date_add('2011-09-5', INTERVAL 4 DAY) 
AND DATE_ADD('2011-09-9', INTERVAL 4 DAY) THEN a.job_order_number ELSE null END) As FRI

,count(case WHEN (a.receivedDate between '2011-09-5 00:00:00' and '2011-09-9 23:59:59' 
and jo.job_order_type LIKE 'IT') then a.job_order_number else null end) as IT 

,count(case WHEN (a.receivedDate between '2011-09-5 00:00:00' and '2011-09-9 23:59:59' 
and jo.job_order_type LIKE 'Non-IT') then a.job_order_number else null end) as NonIT 

,count(a.job_order_number) As Total FROM jo_partner a 

left join specialist_partner sp on a.specialist_Partner_ID = sp.specialistPartnerID       
left join staffing_specialist_asst ssa on sp.SSA_ID = ssa.SSA_ID 
left join staffing_specialist ss on sp.SS_ID = ss.SS_ID 
left join job_order jo on a.job_order_number = jo.job_order_number 
left join candidate_jo cjo on a.JO_partner_ID= cjo.jo_partner_ID 
left join candidate can on cjo.candidate_jo_ID= can.candidate_ID 
WHERE a.receivedDate BETWEEN '2011-09-5 00:00:00' AND '2011-09-9 23:59:59' 
GROUP BY a.specialist_partner_ID

1 个答案:

答案 0 :(得分:1)

我认为你的日期错了。对于每天的列,您选择5天的数据,这没有任何意义。 所以而不是:

count(CASE WHEN a.receivedDate BETWEEN date_add('2011-09-5', INTERVAL 1 DAY) 
AND DATE_ADD('2011-09-9', INTERVAL 1 DAY) THEN a.job_order_number ELSE null END) As TUE

你应该:

count(CASE WHEN a.receivedDate BETWEEN date_add('2011-09-5', INTERVAL 1 DAY) 
and DATE_ADD('2011-09-5', INTERVAL 2 DAY) THEN a.job_order_number ELSE null END) As TUE

甚至更好:

count(CASE WHEN a.receivedDate >= date_add('2011-09-5', INTERVAL 1 DAY) 
and a.receivedDate < DATE_ADD('2011-09-5', INTERVAL 2 DAY) THEN a.job_order_number ELSE null END) As TUE

其余时间也一样。