Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6240251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:32:00+00:00 2026-05-24T11:32:00+00:00

I have a SQL table where I need to get the number for the

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:32:01+00:00Added an answer on May 24, 2026 at 11:32 am

    I am going to assume some ordering column called ord and a date column called dt:

    Naively:

    SELECT COUNT(*)
    FROM tbl
    WHERE ord <= (SELECT MAX(ord) FROM tbl WHERE dt IS NOT NULL)
    

    With your updated data, see how this performs:

    https://data.stackexchange.com/stackoverflow/q/109223/

    DECLARE @tbl AS TABLE (ParentID INT, MilestoneID INT, [Date] DATETIME);
    INSERT INTO @tbl VALUES (1234,        123,            '02/01/2011');
    INSERT INTO @tbl VALUES (1234,        124,            '03/01/2011');
    INSERT INTO @tbl VALUES (1234,        125,            '09/01/2011');
    INSERT INTO @tbl VALUES (1234,        126,            NULL);
    INSERT INTO @tbl VALUES (1234,        127,            '10/10/2011');
    INSERT INTO @tbl VALUES (1234,        128,            NULL);
    
    WITH LastCompleted AS (
        SELECT ParentID, MAX(MilestoneID) AS MAXMilestoneID
        FROM @tbl AS Milestone
        WHERE [Date] IS NOT NULL
        GROUP BY ParentID
    )
    SELECT LastCompleted.ParentID, COUNT(*) AS NumMilestones
    FROM LastCompleted
    INNER JOIN @tbl AS Milestone
        ON LastCompleted.ParentID = Milestone.ParentID
        AND LastCompleted.MAXMilestoneID >= Milestone.MilestoneID
    GROUP BY LastCompleted.ParentID;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.