Hi Sourav,
In order to get this case you need not loop over every value.
Only join the resultset obtained from the first select with X, Y and Z tables and create calculated columns to get the desired result.
As previously said, do not use Cursors until necessary due to performance issue.
Regards,
Anjali