日期:2014-05-18  浏览次数:20436 次

查询一月中每天的最大值
表数据类似如下:

datetime num
-------------------------------------------------
2012-02-01 20:15:00 000 53
2012-02-01 21:15:00 000 64
2012-02-01 22:15:00 000 34
2012-02-01 23:15:00 000 65
.....
.....
2012-02-21 18:15:00 000 34
2012-02-21 19:15:00 000 123
2012-02-21 20:15:00 000 43
2012-02-21 21:15:00 000 64
2012-02-21 22:15:00 000 21
2012-02-21 23:15:00 000 31
..


计算出一个月内每天的最大值
谢谢
 

------解决方案--------------------
/*
2012-02-01 20:15:00 000 53
2012-02-01 21:15:00 000 64
2012-03-01 22:15:00 000 34
2012-03-01 23:15:00 000 65
.....
.....
2012-04-22 18:15:00 000 34
2012-04-22 19:15:00 000 123
2012-04-22 20:15:00 000 43
2012-05-21 21:15:00 000 64
2012-05-21 22:15:00 000 21
2012-05-21 23:15:00 000 31
*/
go
if object_id('tbl')is not null
drop table tbl
go
create table tbl(
[date] datetime,
value int
)
go
insert tbl
select '2012-02-01 20:15:00',53 union all
select '2012-02-01 21:15:00',64 union all
select '2012-03-01 22:15:00',34 union all
select '2012-03-01 23:15:00',65 union all
select '2012-04-22 18:15:00',34 union all
select '2012-04-22 19:15:00',123 union all
select '2012-04-22 20:15:00',43 union all
select '2012-05-21 21:15:00',64 union all
select '2012-05-21 22:15:00',21 union all
select '2012-05-21 23:15:00',31

;with T
as
(
select *,
row_number()over(partition by datepart(mm,[date]),datepart(dd,[date]) 
order by value desc) as num from tbl
)

select [date],value from T where num=1

/*
date value
2012-02-01 21:15:00.000 64
2012-03-01 23:15:00.000 65
2012-04-22 19:15:00.000 123
2012-05-21 21:15:00.000 64
*/

我修改了你给的数据
------解决方案--------------------
探讨
SQL code
select convert(varchar(10),[datetime,120), max(num) as num
from tb
group by convert(varchar(10),[datetime,120)

------解决方案--------------------
SQL code

declare @T table ([datetime] datetime,num int)
insert into @T
select '2012-02-01 20:15:00.000',53 union all
select '2012-02-01 21:15:00.000',64 union all
select '2012-02-01 22:15:00.000',34 union all
select '2012-02-01 23:15:00.000',65 union all
select '2012-02-21 18:15:00.000',34 union all
select '2012-02-21 19:15:00.000',123 union all
select '2012-02-21 20:15:00.000',43 union all
select '2012-02-21 21:15:00.000',64 union all
select '2012-02-21 22:15:00.000',21 union all
select '2012-02-21 23:15:00.000',31

select 
    convert(varchar(10),[datetime],120) as [datetime],max(num) as num
from @T group by convert(varchar(10),[datetime],120)
/*
datetime   num
---------- -----------
2012-02-01 65
2012-02-21 123
*/

------解决方案--------------------
上面的写错了!

SQL code

select convert(varchar(10),日期,120), 值
from 表
group by convert(varchar(10),日期,120), 值
having 值 = MAX(值)

------解决方案--------------------
SQL code


create  table  tbtime (dt  varchar(100),num int)
go
insert tbtime
select '2012-02-01 20:15:00',53 union all
select '2012-02-01 21:15:00',64 union all
select '2012-03-01 22:15:00',34 union all
select '2012-03-01 23:15:00',65 union all
select '2012-04-22 18:15:00',34 union all
select '2012-04-22 19:15:00',123 union all
select '2012-04-22 20:15:00',43 union all
select '2012-05-21 21:15:00',64 union all
select '2012-05-21 22:15:00',21 union all
select '2012-05-21 23:15:00',31




 select  max(num)as 最