I have a sql server table:
CREATE TABLE [Workflow].[MilestoneDate](
[MilestoneDateId] [int] IDENTITY(1,1) NOT NULL,
[SpecifiedDate] [datetime] NULL,
[RelativeDays] [int] NULL,
[RelativeMilestoneDateId] [int] NULL,
CONSTRAINT [PK_MilestoneDate] PRIMARY KEY CLUSTERED
(
[MilestoneDateId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [Workflow].[MilestoneDate] WITH CHECK ADD CONSTRAINT [FK_MilestoneDate_MilestoneDate] FOREIGN KEY([RelativeMilestoneDateId])
REFERENCES [Workflow].[MilestoneDate] ([MilestoneDateId])
GO
ALTER TABLE [Workflow].[MilestoneDate] CHECK CONSTRAINT [FK_MilestoneDate_MilestoneDate]
and it has data that might look like:
Id Date RelDays RelId
49 2010-03-04 00:00:00.000 NULL NULL
746 NULL 6 46
747 NULL 20 746
46 2010-02-18 00:00:00.000 NULL NULL
48 2010-04-04 00:00:00.000 NULL NULL
47 2010-05-04 00:00:00.000 NULL NULL
748 NULL 14 48
What I need to be able to do is to get the calculated date for each row which is either the Date if there is one, or the Date of the "parent" item (using the RelId) plus the RelDays (which can be recursive).
So for example the calculated date for Id 747 is 20 days + 6 days + 2010-02-18 and therefore 2010-03-16.