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

  • Home
  • SEARCH
  • 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 8452801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:35:46+00:00 2026-06-10T11:35:46+00:00

select top 1 ROW_NUMBER() OVER (ORDER BY eventtime desc) as id, Eventcode, eventtime as

  • 0
select top 1 ROW_NUMBER() OVER (ORDER BY eventtime desc) as id,
Eventcode, eventtime as et, status from cfw.dbo.DCTBLEVENTINFO 
where MeterID = 4722 and EventTime between convert(date,'2011-10-21') 
and dateadd(day,1,convert(date,'2011-10-26')) 
and EventCode = 13

Original resultset:

id  Eventcode   et                    status
1   13        2011-10-26 15:00:00.000   1

The above query returns the perfect result set, but if I use the same query like the following manner it returns the wrong result

SELECT temp.et 
  FROM (SELECT TOP 1 ROW_NUMBER() OVER (ORDER BY eventtime desc) as id,
               Eventcode, 
               eventtime as et, 
               status 
          FROM cfw.dbo.DCTBLEVENTINFO 
         WHERE MeterID = 4722 
           AND EventTime BETWEEN CONVERT(date,'2011-10-21') 
                             AND DATEADD(day,1,convert(date,'2011-10-26')) 
           AND EventCode = 13) temp 
 WHERE status = 1

Result set for the above query :

et
------------------------
2011-10-21 21:42:00.000

It returns some other date. I can’t find the problem.

  • 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-10T11:35:48+00:00Added an answer on June 10, 2026 at 11:35 am

    Try adding an ORDER BY to your sub select.

    Something like

    select  temp.et 
    from    (
                select  top 1 
                        ROW_NUMBER() OVER (ORDER BY eventtime desc) as id, 
                        Eventcode, 
                        eventtime as et, 
                        status 
                from    cfw.dbo.DCTBLEVENTINFO  
                where   MeterID = 4722 
                and     EventTime between convert(date,'2011-10-21') and dateadd(day,1,convert(date,'2011-10-26'))  
                and     EventCode = 13
                ORDER BY eventtime desc
            )   temp 
    where   status=1
    

    Please always remember Without ORDER BY, there is no default sort order.

    Furthermore, remeber the Logical Query Processing Phases – Order of Statement Execution

    1. FROM
    2. ON
    3. OUTER
    4. WHERE
    5. GROUP BY
    6. CUBE | ROLLUP
    7. HAVING
    8. SELECT
    9. DISTINCT
    10. ORDER BY
    11. TOP
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DECLARE @ActionNumber varchar(20)='EHPL-DES-SQ-1021' set @ActionNumber=(select top 1 * from dbo.ANOSplit(@ActionNumber,'-') order by ROW_NUMBER() OVER
I have the following query: select top 100 eid, cid, id, position, ROW_NUMBER() over(order
I'm working on a query on the SEDE: select top 20 row_number() over(order by
My current method is this: SELECT TOP 1 ID FROM DATAENTRY ORDER BY ID
Consider the below ;WITH GetParentOfChild AS ( SELECT Rn = ROW_NUMBER() Over(Order By (Select
I can't use t from with t as ( select row_number() OVER (partition by
Normally I would do select top (1) * from table where id active =
I have to select top 5 records and display them in ascending order by
I have a sql like this: SELECT TOP 1 field_name * FROM table_name and
I'm running this query: SELECT TOP 1 [DVD Copy].[Stock No] FROM [DVD Copy] WHERE

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.