[snip] ++ | code | bin | min | ain | cin | dur | ++ | NNE | 103939170759 | 485089817 | 3739.1827 | 27797297 | 11681839027 | Now, what i need todo is exclude certain info from the above NNE entry if code2 is equal to something. So for example (in php terminology): if(code == 'NNE') { if(code2 == 'DENIED') { continue; } else { bin += bytes; if(bytes min) { min = bytes; } cin++; dur += dur; } } after that i could work out the average by dividing bin / cin for what in the report is called ain. Is there any way of achieving this via the sql query because the above is a hugely tedious way of doing it. I know mysql has an if() statement, but I have no idea how to implement it using what i want to achieve above. [/snip] Basically; SELECT SUM(IF(`code` = 'NNE', <calculation>, 0)) AS `ain` FRM `table`