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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:26:13+00:00 2026-05-27T19:26:13+00:00

I have this WITH sequenced_records AS ( SELECT ROW_NUMBER() OVER (ORDER BY [DateTime] DESC)

  • 0

I have this

WITH sequenced_records AS (
    SELECT ROW_NUMBER() OVER (ORDER BY [DateTime] DESC) AS sequence_id, *
    FROM StreamView
    WHERE TypeOf = @TypeOf
    AND [DateTime] >= @DateTime
)
SELECT * FROM sequenced_records WHERE sequence_id = 1;

Which works fine for getting the latest record. But what if it doesn’t find anything? How can I get it to return the latest record going backwards? Meaning if there isnt any newer items than the given DateTime, it would instead go backwards and fetch the first item it find.

thanks

  • 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-27T19:26:13+00:00Added an answer on May 27, 2026 at 7:26 pm

    A couple of options:

    1) If @@RowCount = 0 perform a separate select.

    2) Select into a local table, then if that contains no rows, select into it using your new query, then select out of that table for returning data to caller.

    And the separate select could be (haven’t tested this and not familiar with with statements, but here goes anyway):

    WITH sequenced_records_one AS (
        SELECT TOP 1 ROW_NUMBER() OVER (ORDER BY [DateTime] DESC) AS sequence_id, *
        FROM StreamView
        WHERE TypeOf = @TypeOf
    )
    SELECT * FROM sequenced_records_one WHERE sequence_id = 1;
    

    Upon further review, unless you have simplified this query a lot, you are doing too much work:

    SELECT TOP 1
           * 
      FROM StreamView
     WHERE TypeOf = @TypeOf
       AND [DateTime] >= @DateTime
    ORDER BY [DateTime] DESC
    
    IF @@RowCount = 0
      BEGIN
          /* Get the first record created today */
        SELECT TOP 1
               *
          FROM StreamView
         WHERE TypeOf = @TypeOf
           AND [DateTime] >= CONVERT(DATE, GETDATE())
      ORDER BY [DateTime] ASC
      END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The
This is my query: SELECT B.RECORDID, A.ITEMCODE, A.ITEMNAME, A.STOCKINHAND, B.SALEPRICE FROM ITEMMASTER A, STOCKENTRY
I have a T-Sql Statement as follows; Insert into Table1 Select * From Table2
I have this schema: <xs:complexType name=foo> <xs:sequence> <xs:element name=oneBar type=xs:string minOccurs=0/> <xs:element name=twoBar type=xs:string
I have very long integer sequences that look like this (arbitrary length!): 0000000001110002220033333 Now
I was wondering if this was possible... I have a sequence of variables that
I have this code in jQuery, that I want to reimplement with the prototype
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this line in a javascript block in a page: res = foo('<%=

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.