the json part was something OT, I was just struggling at using an ordered json entity, but chrome parsed into ordered by key...
anyway, if you can query that, then you should be able to calculate the difference between A and B in the same query, idk what db you using, but I guess there will be support for that as well if you can group :) else do it in code after that query :)