It would be helpful if you provided sample data that exhibits the problem, the output that you expect, the output that you're seeing along with the full code that you're using, and tag the actual RDBMS that you're using. Please see: http://stackoverflow.com/help/how-to-ask
– Tom HMay 26 '16 at 14:43