I can't get data in previous months in MySQL
I have this MySQL Query to display the current month and the 2 previous months according to my selection. For example, I choose in my date picker the date range 2011-10-01 to 2011-10-31 (October), it will display the October and the previous two months. I have this SQL query but it only displays data on the current month which is October but not the previous month.
SELECT jp.JO_partner_ID,ss.first_name as SS, ssa.first_name as SSA
,count(CASE WHEN jp.receivedDate between DATE_SUB('2011-10-01', INTERVAL 2 MONTH)
and DATE_SUB('2011-10-31', INTERVAL 2 MONTH) THEN jp.job_order_number ELSE null END) As aug
,count(CASE WHEN jp.receivedDate between DATE_SUB('2011-10-01', INTERVAL 1 MONTH)
and DATE_SUB('2011-10-31', INTERVAL 1 MONTH) THEN jp.job_order_number ELSE null END) As sep
,count(CASE WHEN jp.receivedDate between '2011-10-01'
and '2011-10-31' THEN jp.job_order_number ELSE null END) As oct
,count(case WHEN (jp.receivedDate between '2011-10-01'and '2011-10-31' and jo.job_order_type LIKE 'IT') then jp.job_order_number else null end) as IT
,count(case WHEN (jp.receivedDate between '2011-10-01' and '2011-10-31' and jo.job_order_type LIKE 'Non-IT') then j开发者_如何学运维p.job_order_number else null end) as NonIT
,count(jp.job_order_number) As Total FROM jo_partner jp
left join specialist_partner sp on jp.JO_partner_ID = sp.specialistPartnerID
left join staffing_specialist_asst ssa on jp.SSA_ID = ssa.SSA_ID
left join staffing_specialist ss on jp.SS_ID = ss.SS_ID
left join job_order jo on jp.job_order_number = jo.job_order_number
left join candidate_jo cjo on jp.JO_partner_ID= cjo.jo_partner_ID
left join candidate can on cjo.candidate_jo_ID= can.candidate_ID
WHERE jp.receivedDate BETWEEN '2011-10-01' AND '2011-10-31'
GROUP BY ss.SS_ID;
here is the screenshot of my result: http://www.fileden.com/files/2011/7/27/3174077//3.JPG
Can anyone help to correct my SQL or simplify???? thanks...
The problem is in your WHERE
clause, you are restricting jp.receivedDate BETWEEN '2011-10-01' AND '2011-10-31'
. Basically you are restricting to those records that have a value in jp.receivedDate
in October 2011.
Try:
where jp.receivedDate between date_sub('2011-10-01', interval 2 month) and '2011-10-31'
精彩评论