当前位置: 代码迷 >> Sql Server >> sql记要筛选,很难写的SQL
  详细解决方案

sql记要筛选,很难写的SQL

热度:73   发布时间:2016-04-24 23:37:20.0
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
          )
  相关解决方案