I'm not sure what you need. I couldn't understand. It would help a lot if you posted table definitions (DDL statements), sample data (Insert statements) and expected output based on that sample data.
Read the article on my signature for guidance on how to do it.