I want to get last reference Id from same table. I have following table.
ID UserId DelegatedToUserId
1 100 101
2 101 102
3 102 103
4 103 NULL
5 104 109
I just can't get my head around. I know what I want, just simply can't get it out on the screen. So when I ask for 100 query should return 103, when I ask 101 or 102 again it should return 103. when user enters 104 it should return 109
And when I ask for 103 it should return 103 as there is no delegate for this.
can this be done in single sql query ?