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 8448427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:25:28+00:00 2026-06-10T10:25:28+00:00

I using Microsoft SQL Server 2008 to try and identify the chronological order of

  • 0

I using Microsoft SQL Server 2008 to try and identify the chronological order of data points in order to create a filter field that will allow me to create a query that only includes the first and last record for each ID number, where multiple rows represent different data points from the same ID

Here is an example of my current data and desired data to give a better idea of what I mean:

Current Data

ID    Indicator        Date
1       1           1988-02-11
1       1           1989-03-9
1       1           1993-04-3
1       1           2001-05-4
2       1           2000-01-01
2       1           2001-02-03
2       1           2002-04-22
3       1           1990-02-01
3       1           1998-02-01  
3       1           1999-03-02
3       1           2000-04-02
4       0               NA

Desired Data

ID    Indicator    Date        Order_Indicator
1       1       1988-02-11        1
1       1       1989-03-9         2
1       1       1993-04-3         3
1       1       2001-05-4         4
2       1       2000-01-01        1
2       1       2001-02-03        2
2       1       2002-04-22        3
3       1       1990-02-01        1
3       1       1998-02-01        2
3       1       1999-03-02        3
3       1       2000-04-02        4
4       0       NULL             NULL

The field I want to create is the “Order_Indicator” field in the “Desired Data” table and with the only relevant records are records with Indicator = 1. With this information I would create a query where I only select the rows where Order_Indicator = 1 and Order_Indicator = MAX(Order_Indicator) for each “row group” that share the same ID. Does anyone have any idea about how I might go about this? I know I could do this very easily in Excel but I need to do it on SQL server in order for it to be reproducible with my colleagues.

Thank you so much in advance!

  • 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-06-10T10:25:29+00:00Added an answer on June 10, 2026 at 10:25 am

    You can do this with the ranking functions:

    select c.*,
           (case when indicator = 1
                 then row_number() over (partition by id, indicator order by [date])
            end) as OrderIndicator
    from current c
    

    This assigns a sequential number based on the date and indicator. The case statement takes care of the indicator = 0 case.

    By the way, this assumes that “date” is being stored as a date.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Microsoft SQL server 2008 I have a query that is in need of
I'm using Microsoft SQL Server 2008 Management Studio to create a relational schema by
I am trying to create an add-in by using the article below http://blogs.microsoft.co.il/blogs/shair/archive/2008/07/28/how-to-create-sql-server-management-studio-addin.aspx I
I try to export data out of my Microsoft SQL Server 2008 Database to
Using Microsoft SQL Server 2008, let's say there is a table1 that keeps the
I am using playframework with a legacy SQL Server 2008 database that has data
I'm using SQL Server 2008 with Report Builder 2.0 to try and query data
I need to reverse engineer a Microsoft SQL Server 2008 in order to create
Using: Microsoft SQL Server 2008 Microsoft Visual Studio 2010 C# .NET 4.0 WinForms Ok
I am using Microsoft SQL Server 2008 Report Builder . I have designed some

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.