iam777here4u (4/25/2013)
Thank you so much Kingston... that works. I will be implementing your logic in my original script. I hope using CTE for millions of data is okay. Let you know the result soon.
The CTE here is just to make the script look neat. It should not cause any problems as such.
You can post your results and we can see if it can be improved further in case of any performance troubles.