sql - MySQL - How to get a 0 when COUNT(*) returns null? -
i'm haing difficulties sql request. in request, wanna get, week (entered in parameter), , year (also in parameter) number of opened tickets tech. goal kind of table :
year week tech_id backlog_tickets 2017 1 5 11 2017 1 6 1 2017 1 6 0
but problem that, when tech has no ticket in backlog (0), record not created, because count(*) returns null value.
so here have : year week tech_id backlog_tickets 2017 1 5 11 2017 1 6 1
and here request :
set @selecteddate = date_add(str_to_date('01-01-2017', '%d-%m-%y'), interval _week week); insert whd_stats.backlog_tickets_by_tech_week (year, week, tech_id, backlog_tickets_number) select _year 'year', _week 'week', coalesce(j.assigned_tech_id , 99999) 'tech', @backlognumber := count(j.job_ticket_id) whd.job_ticket j left outer join whd.tech t on j.assigned_tech_id = t.client_id left outer join whd.status_type s on j.status_type_id = s.status_type_id j.deleted = 0 -- create date given year, add number of week , j.report_date <= @selecteddate , (j.close_date > @selecteddate or (j.close_date null , s.status_type_name in ('open', 'pending', 'approval pending'))) group year, week, tech on duplicate key update backlog_tickets_number = @backlognumber;
i have tried replace count(j.job_ticket_id) ifnull(count(j.job_ticket_id), 0), tried coalesce(count(j.job_ticket_id), 0) none working, , have no more idea...
can please me?
thanks!
one option here "calendar table" approach. can create new table looking this:
tech_id 1 2 3 4 5
you change query such begins calendar table , left joins outwards. guarantee every tech_id
appear in result set, if absent in recorded data.
Comments
Post a Comment