try to write matrix query using DECODE function.