Hi,
Instead of trying to work out the output in the query through SQL, why don't you try the lógica at report side through variables and filters?
I think it will be easier this way... And you can also define your aggregation at report level.
Regards,
Rogerio