首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql记要筛选,很难写的SQL

2013-02-24 
sql记录筛选,很难写的SQL表格 time, no, price 11:00, a, 10 12:00, a, 11 13:00, a, 9 05:00, b, 20 06: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
          )

热点排行
Bad Request.