小弟新学 接到一个任务 大体描述是这样的
每天的每个小时有几百条数据存入数据库 表项可以理解为这样 id date hour data
现在需要根据date找出每个小时data的最大值的记录返回
我的想法是子查询找到max的记录然后返回id给上层 但是实在想不出怎么实现
请各位多多指点数据库
每天的每个小时有几百条数据存入数据库 表项可以理解为这样 id date hour data
现在需要根据date找出每个小时data的最大值的记录返回
我的想法是子查询找到max的记录然后返回id给上层 但是实在想不出怎么实现
请各位多多指点数据库
where not exists (
select 1 from tab b
where b.[date] = a.[date]
and b.data > a.data
)
where data=(select max(data) from tb where date=t.date and hour=t.hour
group by date,hour)
怎么会呢,相关子查询,外部查询与内部子查询是有关联条件的,
date=t.date and hour=t.hour
也就是当前时间消失内,只返回一个最大值data的数据,不然写这个关联条件干嘛
SELECT a.* FROM ta a
WHERE NOT EXISTS(SELECT 1 FROM ta WHERE cid=a.cid AND id<a.id)
select * from tb t
cross apply(select maxdata=max(data) from tb where date=t.date and hour=t.hour)tt
select *,maxdata=max(data) over(partition by tb.date,tb.hour) from tb