0

I have the following mysql query

select registration, 
sum(`week01`) as `2015/01`, 
sum(`week02`) as `2015/02`
from (select registration, 
case when concat(YEAR(act_del_time_arrive),'/',week(act_del_time_arrive))='2015/01' then round(costed_amount,0) else '' end 'week01' , 
case when concat(YEAR(act_del_time_arrive),'/',week(act_del_time_arrive))='2015/02' then round(costed_amount,0) else '' end 'week02'
from vw_tekwani_schedule_main
                       where act_del_time_arrive between '2015-01-04' and '2015-02-28'
                       and haulier_id = 4 and registration is not null
                       group by registration, date(act_del_time_arrive)
                       order by registration) as t group by registration

which is producing the following results

reg   2015/01 2015/02
'A10'  , '0'  , '0'
'A2'   , '0'  , '0'
'A3'   , '0'  , '0'
'A4'   , '0'  , '0'
'A5'   , '0'  , '0'
'A6'   , '0'  , '0'
'A7'   , '0'  , '0'
'A8'   , '0'  , '0'
'A9'   , '0'  , '0'

I desperately need to find out how to filter the rows off that have a zero value on 2015/01 and 2015/02.

So theoretically I would not get a result being returned from this query

3
  • from what I see all your rows have zero values over there? Commented Feb 28, 2015 at 15:29
  • wouldn't a WHERE '2015/02' != 0 ... not work? Commented Feb 28, 2015 at 15:33
  • I have tried the filter you sugget thomas and it still returns zero value rows ;( Commented Feb 28, 2015 at 15:50

1 Answer 1

1

Just use the having clause: http://dev.mysql.com/doc/refman/5.0/en/group-by-handling.html

select registration, 
sum(`week01`) as `2015/01`, 
sum(`week02`) as `2015/02`
from (select registration, 
case when concat(YEAR(act_del_time_arrive),'/',week(act_del_time_arrive))='2015/01' then round(costed_amount,0) else '' end 'week01' , 
case when concat(YEAR(act_del_time_arrive),'/',week(act_del_time_arrive))='2015/02' then round(costed_amount,0) else '' end 'week02'
from vw_tekwani_schedule_main
                       where act_del_time_arrive between '2015-01-04' and '2015-02-28'
                       and haulier_id = 4 and registration is not null
                       group by registration, date(act_del_time_arrive)
                       order by registration) as t group by registration
HAVING sum(`week01`) > 0 OR sum(`week02`) > 0
Sign up to request clarification or add additional context in comments.

3 Comments

unfortunately, the having clause seems to be removing those rows where one of the two cols is zero and the other has a value
Oops, i just edited the HAVING condition to use an OR instead of an AND. Is it working now?
sorry to throw a spanner in the works but if the very first row has a value of zero in it then all the rest are dropped as well ;(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.