Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I query for a max value and return date found
    primarykey
    data
    text
    <p>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. </p> <p>here is what I have so far</p> <pre><code> Select Max(Value) as RWQ22003DTDDS from History WHERE Datetime&gt;='2009-08-21 05:00:00' AND Datetime&lt;='2009-08-22 05:00:00' and Tagname ='RWQ22003DTDDS' </code></pre> <p>I would like the date of "2009-08-21" to be in the results.</p> <pre><code>datetime, value ------------------ 2008-08-21, 2216 2008-08-20, 4312 </code></pre> <p>etc. and to do this for 7 days previous</p> <p>UPDATE :</p> <p>here's another approch I came up with</p> <pre><code> 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 &lt;= 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&gt;='''+ convert(varchar, dateadd(mi,5,convert(datetime,@dd,102))) + ''' and Datetime&lt;='''+ convert(varchar, dateadd(mi,-5,convert(datetime,@dd,102)+1)) +''' and Tagname =''RWQ22003DTDDS''' if(@dec &lt;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) </code></pre> <p>results:</p> <pre><code> 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 </code></pre> <p>Comments?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload