简体   繁体   English

如何查询最大值和找到的返回日期

[英]How do I query for a max value and return date found

I am trying to query for a maximium value from 5am to 5am the next morning. 我想查询第二天早上5点到5点的最大值。 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. 我希望日期为“ 2009-08-21”。

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

etc. and to do this for 7 days previous 等等,并在7天内这样做

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? 评论?

try this (assumes that multiple rows are ok, if the YourValue is not a PK): 尝试以下操作(如果YourValue不是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

I solve this kind of query this way: 我通过这种方式解决这种查询:

CREATE TEMPORARY TABLE Timespan (
 Start DATETIME,
 End DATETIME
);

INSERT INTO Timespan VALUES
 ('2009-08-21 05:00:00', '2009-08-22 05:00:00'),
 ('2009-08-20 05:00:00', '2009-08-21 05:00:00'),
 ('2009-08-19 05:00:00', '2009-08-20 05:00:00'),
 ('2009-08-18 05:00:00', '2009-08-19 05:00:00'),
 ('2009-08-17 05:00:00', '2009-08-18 05:00:00'),
 ('2009-08-16 05:00:00', '2009-08-17 05:00:00'),
 ('2009-08-15 05:00:00', '2009-08-16 05:00:00');

Select h1.Value as RWQ22003DTDDS, h1.Datetime
FROM Timespan t JOIN History h1 ON 
  (h1.Datetime BETWEEN t.Start AND t.End AND h1.Tagname = 'RWQ22003DTDDS')
LEFT JOIN History h2 ON
  (h2.Datetime BETWEEN t.Start AND t.End AND h2.Tagname = 'RWQ22003DTDDS')
  AND (h1.Value < h2.Value OR (h1.Value = h2.Value AND h1.Id < h2.Id))
WHERE h2.Value IS NULL;

Here is a SQL Server solution for what I think you want. 这是我想要的SQL Server解决方案。 It shouldn't be hard to adapt to another dialect of SQL. 适应另一种SQL方言应该不难。

Notes: Because of the LEFT join, there will be a result for days with no History; 注意:由于有LEFT联接,将有几天没有历史记录的结果; I've changed <= to < so that rows where "Datetime" falls at exactly 5:00 am will only be counted in one day. 我已将<=更改为<,以便“日期时间”恰好在上午5:00处的行将仅在一天之内进行计数。

create table Seven(
  daysBack int primary key
);
insert into Seven values
  (0),(1),(2),(3),(4),(5),(6);

declare @today date = cast(current_timestamp as date);

select
  dateadd(day,-daysBack,@today) as QueryDateFrom,
  Max(Value) as RWQ22003DTDDS
from Seven left outer join History
on "Datetime" >= dateadd(day,-daysBack,@today)
and "Datetime" < dateadd(day,1-daysBack,@today)
group by dateadd(day,-daysBack,@today)

SELECT CONVERT(CHAR(10), [datetime] ,110) , MAX([value]) SELECT CONVERT(CHAR(10),[datetime],110),MAX([value])

..... .....

GROUP BY CONVERT(CHAR(10), [datetime] ,110) GROUP BY CONVERT(CHAR(10),[datetime],110)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM