I have a data set that looks like this:
structure(list(landings = c(116, 31, 0, 0, 0,
0, 0, 0, 0, 120, 0, 241, 9, 0, 64, 326, 142, 605, 139, 410,
212, 470, 416, 309, 1269, 474, 22, 135, 395, 464, 451, 32,
2537, 210, 299, 1522, 184, 550, 666, 429, 1372, 184, 147,
1208, 159, 951, 1000, 1100, 301, 144, 244, 0, 0, 281, 0,
0, 0, 0, 0, 0, 0, 0, 0, 42, 594, 26, 747, 436, 0, 914, 182,
8, 275, 175, 766, 130, 930, 31, 177, 123, 895, 88, 107, 0,
4, 481, 909, 511, 877, 402, 295, 336, 645, 310, 301, 398,
411, 0, 205, 293, 49, 454, 162, 138, 1171, 0, 138, 0, 111,
0, 0, 36, 78, 114, 0, 0, 134, 44, 549, 0, 378, 716, 739,
393, 203, 839, 70, 454, 132, 651, 63, 1850, 217, 403, 55,
0, 408, 43, 17, 12, 26, 2, 811, 581, 1216, 154, 1059, 89,
1862, 1310, 297, 29, 680, 0, 0, 29, 0, 0, 0, 0, 0, 0, 17,
6, 0, 0, 0, 44, 909, 0, 0, 0, 194, 0, 212, 18, 46, 44, 56,
365, 37, 0, 73, 11, 16, 19, 0, 0, 0, 23, 0, 92, 0, 216, 0,
16, 0, 80, 319, 59, 35, 929, 47, 0, 0, 356, 0, 0, 33, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 91, 362, 0,
0, 0, 0, 0, 29, 0, 0, 392, 105, 0, 94, 15, 222, 34, 44, 178,
1867, 0, 224, 241, 23, 1502, 492, 168, 0, 234, 299, 453,
0, 406, 149, 0, 39, 57, 86, 0, 28, 23, 265, 0, 0, 0, 168,
31, 20, 0, 28, 78, 244, 13, 0, 99, 168, 861, 52, 649, 0,
174, 0, 0, 2462, 64, 178, 0, 61, 0, 321, 391, 33, 17, 227,
241, 248, 294, 1119, 37, 90, 0, 85, 37, 89, 0, 0, 0), Date = c(2014,
2014.01916495551, 2014.03832991102, 2014.05749486653, 2014.07665982204,
2014.09582477755, 2014.11498973306, 2014.13415468857, 2014.15331964408,
2014.17248459959, 2014.1916495551, 2014.21081451061, 2014.22997946612,
2014.24914442163, 2014.26830937714, 2014.28747433265, 2014.30663928816,
2014.32580424367, 2014.34496919918, 2014.36413415469, 2014.3832991102,
2014.40246406571, 2014.42162902122, 2014.44079397673, 2014.45995893224,
2014.47912388775, 2014.49828884326, 2014.51745379877, 2014.53661875428,
2014.55578370979, 2014.5749486653, 2014.59411362081, 2014.61327857632,
2014.63244353183, 2014.65160848734, 2014.67077344285, 2014.68993839836,
2014.70910335387, 2014.72826830938, 2014.74743326489, 2014.7665982204,
2014.78576317591, 2014.80492813142, 2014.82409308693, 2014.84325804244,
2014.86242299795, 2014.88158795346, 2014.90075290897, 2014.91991786448,
2014.93908281999, 2014.9582477755, 2014.97741273101, 2014.99657768652,
2015.01574264203, 2015.03490759754, 2015.05407255305, 2015.07323750856,
2015.09240246407, 2015.11156741958, 2015.13073237509, 2015.1498973306,
2015.16906228611, 2015.18822724162, 2015.20739219713, 2015.22655715264,
2015.24572210815, 2015.26488706366, 2015.28405201916, 2015.30321697467,
2015.32238193018, 2015.34154688569, 2015.3607118412, 2015.37987679671,
2015.39904175222, 2015.41820670773, 2015.43737166324, 2015.45653661875,
2015.47570157426, 2015.49486652977, 2015.51403148528, 2015.53319644079,
2015.5523613963, 2015.57152635181, 2015.59069130732, 2015.60985626283,
2015.62902121834, 2015.64818617385, 2015.66735112936, 2015.68651608487,
2015.70568104038, 2015.72484599589, 2015.7440109514, 2015.76317590691,
2015.78234086242, 2015.80150581793, 2015.82067077344, 2015.83983572895,
2015.85900068446, 2015.87816563997, 2015.89733059548, 2015.91649555099,
2015.9356605065, 2015.95482546201, 2015.97399041752, 2015.99315537303,
2016.01232032854, 2016.03148528405, 2016.05065023956, 2016.06981519507,
2016.08898015058, 2016.10814510609, 2016.1273100616, 2016.14647501711,
2016.16563997262, 2016.18480492813, 2016.20396988364, 2016.22313483915,
2016.24229979466, 2016.26146475017, 2016.28062970568, 2016.29979466119,
2016.3189596167, 2016.33812457221, 2016.35728952772, 2016.37645448323,
2016.39561943874, 2016.41478439425, 2016.43394934976, 2016.45311430527,
2016.47227926078, 2016.49144421629, 2016.5106091718, 2016.52977412731,
2016.54893908282, 2016.56810403833, 2016.58726899384, 2016.60643394935,
2016.62559890486, 2016.64476386037, 2016.66392881588, 2016.68309377139,
2016.7022587269, 2016.72142368241, 2016.74058863792, 2016.75975359343,
2016.77891854894, 2016.79808350445, 2016.81724845996, 2016.83641341547,
2016.85557837098, 2016.87474332649, 2016.893908282, 2016.91307323751,
2016.93223819302, 2016.95140314853, 2016.97056810404, 2016.98973305955,
2017.00889801506, 2017.02806297057, 2017.04722792608, 2017.06639288159,
2017.0855578371, 2017.10472279261, 2017.12388774812, 2017.14305270363,
2017.16221765914, 2017.18138261465, 2017.20054757016, 2017.21971252567,
2017.23887748118, 2017.25804243669, 2017.2772073922, 2017.29637234771,
2017.31553730322, 2017.33470225873, 2017.35386721424, 2017.37303216975,
2017.39219712526, 2017.41136208077, 2017.43052703628, 2017.44969199179,
2017.4688569473, 2017.48802190281, 2017.50718685832, 2017.52635181383,
2017.54551676934, 2017.56468172485, 2017.58384668036, 2017.60301163587,
2017.62217659138, 2017.64134154689, 2017.6605065024, 2017.67967145791,
2017.69883641342, 2017.71800136893, 2017.73716632444, 2017.75633127995,
2017.77549623546, 2017.79466119097, 2017.81382614648, 2017.83299110199,
2017.85215605749, 2017.871321013, 2017.89048596851, 2017.90965092402,
2017.92881587953, 2017.94798083504, 2017.96714579055, 2017.98631074606,
2018.00547570157, 2018.02464065708, 2018.04380561259, 2018.0629705681,
2018.08213552361, 2018.12046543463, 2018.13963039014, 2018.15879534565,
2018.17796030116, 2018.19712525667, 2018.21629021218, 2018.23545516769,
2018.2546201232, 2018.27378507871, 2018.29295003422, 2018.31211498973,
2018.33127994524, 2018.35044490075, 2018.36960985626, 2018.38877481177,
2018.40793976728, 2018.42710472279, 2018.4462696783, 2018.46543463381,
2018.48459958932, 2018.50376454483, 2018.52292950034, 2018.54209445585,
2018.56125941136, 2018.58042436687, 2018.59958932238, 2018.61875427789,
2018.6379192334, 2018.65708418891, 2018.67624914442, 2018.69541409993,
2018.71457905544, 2018.73374401095, 2018.75290896646, 2018.77207392197,
2018.79123887748, 2018.81040383299, 2018.8295687885, 2018.84873374401,
2018.86789869952, 2018.88706365503, 2018.90622861054, 2018.92539356605,
2018.94455852156, 2018.96372347707, 2018.98288843258, 2019.00205338809,
2019.0212183436, 2019.04038329911, 2019.05954825462, 2019.07871321013,
2019.09787816564, 2019.11704312115, 2019.13620807666, 2019.15537303217,
2019.17453798768, 2019.19370294319, 2019.2128678987, 2019.23203285421,
2019.25119780972, 2019.27036276523, 2019.28952772074, 2019.30869267625,
2019.32785763176, 2019.34702258727, 2019.36618754278, 2019.38535249829,
2019.4045174538, 2019.42368240931, 2019.44284736482, 2019.46201232033,
2019.48117727584, 2019.50034223135, 2019.51950718686, 2019.53867214237,
2019.55783709788, 2019.57700205339, 2019.5961670089, 2019.61533196441,
2019.63449691992, 2019.65366187543, 2019.67282683094, 2019.69199178645,
2019.71115674196, 2019.73032169747, 2019.74948665298, 2019.76865160849,
2019.787816564, 2019.80698151951, 2019.82614647502, 2019.84531143053,
2019.86447638604, 2019.88364134155, 2019.90280629706, 2019.92197125257,
2019.94113620808, 2019.96030116359, 2019.9794661191))
I am running a GAM that looks like this:
gam1<-gam(landings~s(Date))
I am using draw to plot my data:
draw(gam1)
I have been looking to figure out what the uncertainty is measured by in draw() with no success. Is this a 95% confidence interval or standard error that is used to plot the uncertainty in this plot?
It's an approximate 95% credible interval (drawn at 2 * the standard error of the smooth), the same as you'd get from mgcv:::plot.gam().
I should make this clearer, and allow users to control what coverage they want for the interval, in the package.
I have a hierarchy table in SQL containing NodeId, ParentId, ChildId, NextId, NodeLevel, and a validity period (DateFrom, DateTo) since the location of a node within the hierarchy can change based on the date. I am able to loop through the records recursively using the NodeId and ParentId with no problem. The piece that I am missing is how to order the nodes. The order of the nodes is based on ChildId, NextId, and validity period. I need a way to dynamically add a sort column based on the validity period. This should give me a column called SortOrder that just numbers the nodes sequentially as they would appear if the hierarchy was displayed expanded down to the lowest level. The code to recreate the table I am using is:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[NextIDTest](
[NodeName] [nvarchar](60) NOT NULL,
[NodeId] [int] NOT NULL,
[ParentId] [int] NOT NULL,
[ChildId] [int] NOT NULL,
[NextId] [int] NOT NULL,
[NodeLevel] [int] NOT NULL,
[DateFrom] [date] NOT NULL,
[DateTo] [date] NOT NULL
) ON [PRIMARY]
GO
INSERT [dbo].[NextIDTest] ([NodeName], [NodeId], [ParentId], [ChildId], [NextId], [NodeLevel], [DateFrom], [DateTo])
VALUES
(N'TOTAL CAPITAL EXPENDITURE', 1, 0, 2, 0, 1, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'UTILITY CAPITAL EXPENDITURES', 2, 1, 3, 28, 2, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'CATEGORY 1', 3, 2, 4, 24, 3, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'NEW MAINS', 4, 3, 7, 9, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000713', 5, 4, 0, 8, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000712', 6, 4, 0, 5, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000711', 7, 4, 0, 6, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00007199', 8, 4, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'NEW SERVICES', 9, 3, 13, 14, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00007299', 10, 9, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000723', 11, 9, 0, 10, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000722', 12, 9, 0, 11, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000721', 13, 9, 0, 12, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000777', 14, 3, 0, 17, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000740', 15, 3, 0, 16, 4, CAST(N'1000-01-01' AS Date), CAST(N'2014-11-30' AS Date))
,(N'00000586', 16, 3, 0, 18, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000015', 17, 3, 0, 15, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'METERS', 18, 3, 20, 0, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000023', 19, 18, 0, 21, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000021', 20, 18, 0, 19, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'IND AND COMM SOLUTIONS', 21, 18, 23, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000024', 22, 21, 0, 0, 6, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000022', 23, 21, 0, 22, 6, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'CATEGORY 2', 24, 2, 25, 29, 3, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'BARE STEEL', 25, 24, 27, 40, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000319', 26, 25, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000119', 27, 25, 0, 26, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'NON-UTILITY CAPITAL EXPENDITURES', 28, 1, 35, 0, 2, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'CATEGORY 3', 29, 2, 52, 30, 3, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'CATEGORY 4', 30, 2, 55, 31, 3, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'CATEGORY 5', 31, 2, 57, 32, 3, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'CATEGORY 6', 32, 2, 125, 129, 3, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'NON-UTILITY', 33, 28, 122, 0, 3, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'BUSINESS DEVELOPMENT', 34, 28, 120, 33, 3, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'GAS STORAGE', 35, 28, 95, 34, 3, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000320', 37, 24, 0, 39, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000120', 38, 24, 0, 37, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000112', 39, 24, 0, 127, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'LEAKAGE', 40, 24, 59, 128, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'GENERAL', 41, 29, 79, 0, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'GAS SUPPLY', 42, 29, 71, 41, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'REGULATORS', 43, 29, 66, 42, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000336', 44, 29, 0, 46, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000318', 45, 29, 0, 43, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000317', 46, 29, 0, 45, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000140', 47, 29, 0, 50, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000136', 48, 29, 0, 44, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000115', 49, 29, 0, 47, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000014', 50, 29, 0, 48, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'RELOCATES/ABANDONMENTS', 51, 29, 64, 126, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'PUBLIC WORKS', 52, 29, 62, 51, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000585', 53, 30, 0, 0, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'LAND AND STRUCTURES', 54, 30, 86, 53, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'INFORMATION TECHNOLOGY', 55, 30, 83, 54, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'TOTAL NON-UTIL INTERSTATE STOR', 56, 31, 93, 0, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'TOTAL UTILITY STORAGE PROJECTS', 57, 31, 87, 56, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000313', 58, 40, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000113', 59, 40, 0, 58, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000314', 60, 52, 0, 61, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000117', 61, 52, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000114', 62, 52, 0, 60, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000316', 63, 51, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000116', 64, 51, 0, 63, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000019', 65, 43, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000013', 66, 43, 0, 65, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000529', 67, 42, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000524', 68, 42, 0, 67, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000018', 69, 42, 0, 68, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000012', 70, 42, 0, 69, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000011', 71, 42, 0, 70, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000571', 72, 41, 0, 73, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000509', 73, 41, 0, 74, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000044', 74, 41, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000043', 75, 41, 0, 76, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000040', 76, 41, 0, 80, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000035', 77, 41, 0, 78, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000033', 78, 41, 0, 75, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000032', 79, 41, 0, 77, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000031', 80, 41, 0, 72, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000039', 81, 55, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000038', 82, 55, 0, 81, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000017', 83, 55, 0, 82, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000511', 84, 54, 0, 85, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000036', 85, 54, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000034', 86, 54, 0, 84, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000569', 87, 57, 0, 88, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000562', 88, 57, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000581', 89, 56, 0, 90, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000563', 90, 56, 0, 124, 5, CAST(N'1000-01-01' AS Date), CAST(N'2014-11-30' AS Date))
,(N'00000514', 91, 56, 0, 92, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000513', 92, 56, 0, 89, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000512', 93, 56, 0, 91, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'GAS STORAGE LLC', 94, 35, 116, 0, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'OTHER STORAGE', 95, 35, 96, 94, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'OTHER (COMPANY 1234)', 96, 95, 111, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000480', 97, 96, 0, 112, 6, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000470', 98, 96, 0, 97, 6, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000460', 99, 96, 0, 98, 6, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000450', 100, 96, 0, 99, 6, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000440', 101, 96, 0, 100, 6, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000430', 102, 96, 0, 101, 6, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000420', 104, 96, 0, 102, 6, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000410', 111, 96, 0, 104, 6, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000401', 112, 96, 0, 0, 6, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000491', 113, 94, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000489', 114, 94, 0, 117, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000486', 115, 94, 0, 113, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000481', 116, 94, 0, 114, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000413', 117, 94, 0, 115, 5, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000508', 118, 34, 0, 119, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000507', 119, 34, 0, 0, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000506', 120, 34, 0, 121, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000505', 121, 34, 0, 118, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000587', 122, 33, 0, 123, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000510', 123, 33, 0, 0, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000564', 124, 56, 0, 0, 5, CAST(N'1000-01-01' AS Date), CAST(N'2014-11-30' AS Date))
,(N'00000760', 125, 32, 0, 0, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000752', 126, 29, 0, 49, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000751', 127, 24, 0, 0, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000750', 128, 24, 0, 38, 4, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'CATEGORY 7', 129, 2, 132, 0, 3, CAST(N'1000-01-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000740', 130, 129, 0, 0, 4, CAST(N'2014-12-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000564', 131, 129, 0, 130, 4, CAST(N'2014-12-01' AS Date), CAST(N'9999-12-31' AS Date))
,(N'00000563', 132, 129, 0, 131, 4, CAST(N'2014-12-01' AS Date), CAST(N'9999-12-31' AS Date))
GO
EDIT:
Expected output would look like this:
NodeName NodeId ParentId ChildId NextId NodeLevel SortOrder
------------------------------------------------------------ ----------- ----------- ----------- ----------- ----------- -----------
TOTAL CAPITAL EXPENDITURE 1 0 2 0 1 1
UTILITY CAPITAL EXPENDITURES 2 1 3 28 2 2
CATEGORY 1 3 2 4 24 3 3
NEW MAINS 4 3 7 9 4 4
00000713 5 4 0 8 5 7
00000712 6 4 0 5 5 6
00000711 7 4 0 6 5 5
00007199 8 4 0 0 5 8
I do not know if I really got this correctly, but this might be what you are looking for:
Btw: You do not need all these columns in your table (ChildId, NextId and NodeLevel will result out of the query). It is quite erronous actually to keep them as stored values...
Btw: Next time please try to reduce your example to a small amount of rows and add the expected output fitting to the given data. This makes it much easier to understand what you really need...
--A recursive CTE traverses down the hierarchy
WITH RecCTE AS
(
SELECT NodeName,NodeId,ParentId,1 AS NodeLevel,DateFrom,DateTo
FROM NextIDTest
WHERE ParentId=0
UNION ALL
SELECT y.NodeName,y.NodeId,y.ParentId,x.NodeLevel+1,y.DateFrom,y.DateTo
FROM RecCTE AS x
INNER JOIN NextIDTest AS y ON x.NodeId=y.ParentId
)
--the second CTE will take the ResultSet as it comes out of recCTE and number it
,Numbered AS --
(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RunningNumber,*
FROM RecCTE
)
--the third CTE will get another numbering, don't know if this really is what you need
,InternalSort AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY NodeLevel ORDER BY DateFrom DESC,RunningNumber) AS InternalSort,*
FROM Numbered
)
--And finally this is again numbered "as is"
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS YourSortColumn,*
FROM InternalSort