It will be good for us to solve your problem if you provide some more details like table structure, sample data and resultant output....
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/