I have an SQL table with "lines" representing elements of chained lists. I could for example have the following records:
(id, previous_id)
------------------
(1, NULL)
(2, NULL)
(3, 2)
(4, 3)
(5, NULL)
(6, 4)
(7, 5)
We have 3 lists in this table:
(1,)
(2,3,4,6)
(5,7)
I would like to find the last element of each list and the number of elements in the list. The query I am looking for would output:
last, len
1, 1
6, 4
7, 2
Is this possible in SQL?