I have a SQL Server database with these pages:
+------------+--------------+-------------------------------+ | pageid | parentid | title | +------------+--------------+-------------------------------+ | 1 | null | Home | +------------+--------------+-------------------------------+ | 2 | 1 | News | +------------+--------------+-------------------------------+ | 3 | 1 | User | +------------+--------------+-------------------------------+ | 4 | 3 | Edit profile | +------------+--------------+-------------------------------+ | 5 | 3 | Messages | +------------+--------------+-------------------------------+ | 6 | 5 | View all | +------------+--------------+-------------------------------+
How do I select the second-highest (in level) parentid for any row? So for pageid=6 (View all) it should return parentid->3 (User).