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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:07:02+00:00 2026-06-15T08:07:02+00:00

This is a crazy question but basically we need to mark the first row

  • 0

This is a crazy question but basically we need to mark the first row that matches a condition but not the rest, for example we have a history of all users daily Logins but each month we want to show the login that was before a specific date, for example:

UserId     LoginDate
1          2011-12-31
2          2012-12-31
1          2012-01-01
2          2012-01-02
1          2012-01-02
2          2012-01-03

etc….

We want a SQL Query that will return the login that is first for each month in a result set similar to:

UserId     LoginDate     WasFirstLoginOfMonth
1          2011-12-31    0
2          2012-12-31    0
1          2012-01-01    1
2          2012-01-02    1
1          2012-01-02    0
2          2012-01-03    0

etc….

I know this seems like a crazy requirement but we need to know which login satisfied a certain date condition but only one can and the rest can’t.

Is there any way to do this without a slow sub-select for each row to see if it was the first?

Many thanks for any help.

  • 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-15T08:07:02+00:00Added an answer on June 15, 2026 at 8:07 am

    The row_number() function allows you to partition and sort data.

    So in this query, rn represents the ordinal of the login, by user, year and month, so when that value is 1, that is the first login of the month.

    select * 
    from
    (
    select *, 
           row_number() over 
           (
                partition by userID, 
                year(logindate),
                month(logindate)
                order by logindate
           ) rn
    from yourtable
    )  v
    where rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question is crazy - but my employers client is demanding that
I know this question might sound a little bit crazy, but I tough that
This question is driving me crazy. I need to get it right so I
This is NOT a duplicate question, and the problem is driving me crazy. I
This might sound crazy to you, but I need a Nullable<T> (where T is
I'm sorry because it's not really a strictly programming question but I'm going crazy
This might be a trivial question but is driving me crazy. I want to
This might sound like a little bit of a crazy question, but how can
Sorry for the stupid question, but this is driving me crazy... I have test_framework.php
Please apologize for the basicness of my question, but this is driving me crazy

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.