select h.cus, h.ta, h.gd1 - c.gd1 as diff1, h.gd2 - c.gd2 as diff2, from hist h, hist c where h.cus = c.cus and h.ta = c.ta order by h.cus, h.ta