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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:07:05+00:00 2026-05-12T09:07:05+00:00

I am trying to query for a maximium value from 5am to 5am the

  • 0

I am trying to query for a maximium value from 5am to 5am the next morning. I would also like to have the start of query date in the results.

here is what I have so far

 Select    Max(Value) as RWQ22003DTDDS  from History     
 WHERE Datetime>='2009-08-21 05:00:00'
 AND Datetime<='2009-08-22 05:00:00' and Tagname ='RWQ22003DTDDS'

I would like the date of “2009-08-21” to be in the results.

datetime,   value
------------------
2008-08-21,  2216    
2008-08-20,  4312

etc.
and to do this for 7 days previous

UPDATE :

here’s another approch I came up with

        declare @dec int
declare @SqlQry as varchar(4000)
declare @dd  as nvarchar(50) 
declare @ResolvedQry as varchar(4000)
set @dec = 0

set @SqlQry =''
WHILE (@dec <= 7)
        BEGIN

set @dd = cast(datepart(mm,getdate()-@dec)as nvarchar) +'/'+ 
          cast(datepart(dd,getdate()-@dec)as nvarchar) +'/'+  
          cast(datepart(yyyy,getdate()-@dec)  as nvarchar)+' 06:00:00'


set @ResolvedQry = ' Select  cast(   convert(datetime,'''+@dd+''',102) as datetime)    as [Date],  
                     Max(Value) as RWQ22003DTDDS  from History  
                     WHERE Datetime>='''+   convert(varchar, dateadd(mi,5,convert(datetime,@dd,102)))    +
                     ''' and Datetime<='''+   convert(varchar, dateadd(mi,-5,convert(datetime,@dd,102)+1))    +'''
                     and Tagname =''RWQ22003DTDDS'''

  if(@dec <7)
begin
set @ResolvedQry =@ResolvedQry + ' union'
end

set @SqlQry =  @SqlQry + @ResolvedQry 

set @dec = @dec + 1

END

 set  @SqlQry ='select * from ( ' + @SqlQry + ') as dt order by [Date] desc'
print  @SqlQry
exec(@SqlQry)

results:

  Date                RWQ22003DTDDS
------------------- ----------------------
Aug 21 2009  5:00AM 3586
Aug 20 2009  5:00AM 7233
Aug 19 2009  5:00AM 9099
Aug 18 2009  5:00AM 9099
Aug 17 2009  5:00AM 8909
Aug 16 2009  5:00AM 8516
Aug 15 2009  5:00AM 8064
Aug 14 2009  5:00AM 7437

Comments?

  • 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-12T09:07:05+00:00Added an answer on May 12, 2026 at 9:07 am

    try this (assumes that multiple rows are ok, if the YourValue is not a PK):

    SELECT
        YourTable.*
        FROM YourTable
            INNER JOIN (SELECT
                            MAX(YourValue) AS YourValue
                            FROM YourTable
                            WHERE YourDate>=_StartDateTime 
                                AND YourDate<=_EndDateTime_
                       ) dt ON YourTable.YourValue=dt.YourValue
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query, Im trying to select the maximum value from the summer
I have two tables from which I'm trying to run a query to return
I am trying to query and parse Yahoo fantasy sports data and show results
I am trying to query the windows desktop search API using SQL. I have
I am trying to query an .xls spreadsheet with VBScript, but I have run
I have the following three tables which I am trying to perform a query
I am trying to concatenate results of some string data in a query using
I have a List, and I would like to compute a double[360] containing the
I have an error in my query sub query. I been trying to work
Using classic asp, I am trying to query a SQL Server database like so:

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.