日期:2014-05-17  浏览次数:20407 次

sql记录筛选,很难写的SQL
表格 
time, no, price 
11:00, a, 10 
12:00, a, 11 
13:00, a, 9 
05:00, b, 20 
06:00, b, 21 
12:00, b, 22 
有无高效的办法,获得相同NO,最高time的PRICE,
结果
time, no, price
13:00, a, 9
12:00, b, 22

------解决方案--------------------

with tb(time,no,price) as (
select '11:00', 'a', 10 union all 
select '12:00', 'a', 11 union all 
select '13:00', 'a', 9 union all
select '05:00', 'b', 20 union all
select '06:00', 'b', 21 union all 
select '12:00', 'b', 22)
select * from tb a where not exists (select 1 from tb where no=a.no and a.time<time) 


------解决方案--------------------
Select
   *
from tb As a
Where time=(Select Max(time) from tb As x 
                 Where x.no=a.no
          )