• Please post table definitions, sample data and expected outcome based on the sample data as per the second link in my signature.

    With that, we will be able to help you better with your query.