performance - Save Conditional sum result in variable MySQL -
i stuck in query want store sum result in variable status = 3, happening right store value of last row , show rows
here query
select request_made_on,driver_id, @sum_result = sum(status = 3) complete_count,@sum_result, sum(status = 6) missed_count, (status = 4) canceled_count, sum(actual_fare),sum(discount) tb_engagements group date(request_made_on),driver_id;
i'm guessing want sum of values:
select request_made_on, driver_id, (@sum_result := @sum_result + sum(status = 3)) complete_count, sum(status = 6) missed_count, sum(status = 4) canceled_count, sum(actual_fare), sum(discount) tb_engagements c cross join (select @sum_result := 0) params group date(accept_time), driver_id;
notes:
- you don't need return value of variable expression. expression adds column result set value.
- you don't need
if()
count values. mysql has nice short-hand, treating boolean expressions integers in numeric context.
Comments
Post a Comment