MP2-431 Invalid SQL built when counting media item

Fixes from review