mysql - Sum query results -


i have query count of columns problem counting these counts results in new field

select  concat(u.firstname,' ',u.lastname) 'agent', count(case when pa.answer_text '%yes%' pa.answer_id end) yes, count(case when pa.answer_text '%no%' pa.answer_id end) no, count(case when l2u.lead_status_id in (4,5,8,39) l2u.lead_id end) pending, count(case when l2u.lead_status_id in (7,14,43) l2u.lead_id end) wrong_number, 

here problem .. correct syntax
count(pending + wrong_number + yes + no) 'total'

from user u, poll_votes pv, poll_answers pa, lead_to_user_original l2u  u.user_id = pv.user_id , pv.answer_id = pa.answer_id , l2u.lead_id = pv.vote_lead_id , (pa.answer_client_one = '869' or pa.answer_client_two = '869') , pv.vote_date between '2013-07-01 00:00:01' , '2013-07-17 23:59:59' group u.user_id 

you can't access aliases in select clause in field in select. can "push" down sub-query, , this:

select a.*, pending + wrong_number + yes + no 'total' (     select          concat(u.firstname,' ',u.lastname) 'agent',         count(case when pa.answer_text '%yes%' pa.answer_id end) yes,         count(case when pa.answer_text '%no%' pa.answer_id end) no,         count(case when l2u.lead_status_id in (4,5,8,39) l2u.lead_id end) pending,         count(case when l2u.lead_status_id in (7,14,43) l2u.lead_id end) wrong_number,     user u     join poll_votes pv       on u.user_id = pv.user_id     join poll_answers pa       on pv.answer_id = pa.answer_id     join lead_to_user_original l2u       on l2u.lead_id = pv.vote_lead_id     (pa.answer_client_one = '869' or pa.answer_client_two = '869')       , pv.vote_date between '2013-07-01 00:00:01' , '2013-07-17 23:59:59'     group u.user_id ) 

i changed query use "ansi-92 style" joins, think it's more readable...


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -