Sounds like a homework question .. in case it is NOT ... please post the table definition(s), sample data and required results.
You can do this quickly and very easily..... click on the first link in my signature block and read the article. Now the article contains all the necessary code for table definition, creating sample data and last but not least the required results.