Here last column name parentid carry information of customerid of his parent.
customerid,customername,flag,parentid are four column of table.
1)start with customerid who is parent(You can know by its flag I=parent and S=child).
2)Retrive all child of that customerid.(mean all customerid whose parentid =selected customerid).Sort them by name
repeat (1) and (2) for all customer id who is parent.
i can understand what to do but i can't convert that logic in SQL form.
Thanks.