I have a SQL table where I need to get the number for the final row with a date and count the number of previous rows even if they don’t have a date in either.
e.g
02/01/2011
03/01/2011
09/01/2011
NULL
10/10/2011
NULL
This table should return the number 5 for the 5th record
NULL
NULL
NULL
09/01/2011
NULL
10/10/2011
NULL
This table should return 6
Thank you in advance
J
—– Update ——
Just a little more information
The table its self represents units of work complete (milestones) and links to a parent table which represents units of work. In the milestone table it contains dates, Parent Work Id and a milsestone ID.
From the first example
ParentID MilestoneID Date
1234 123 02/01/2011
1234 124 03/01/2011
1234 125 09/01/2011
1234 126 NULL
1234 127 10/10/2011
1234 128 NULL
Hope this helps
—– Update 2 —–
The closest I got was this
SELECT TOP 1
Num
FROM
(
SELECT
ROW_NUMBER()OVER(ORDER BY ParentID) AS Num,
Date
FROM
Milestone
WHERE
Milestone.ParentID = 1234
) AS MilestoneStones
WHERE
Date IS NOT NULL
ORDER BY
Num DESC
But with a large data set and other things attaching to it it got very slow
Was hoping that I could get something better
Thank you
J
I am going to assume some ordering column called ord and a date column called dt:
Naively:
With your updated data, see how this performs:
https://data.stackexchange.com/stackoverflow/q/109223/