表格 
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

解决方案 »

  1.   


    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) 
      

  2.   


    很好啊,能翻译成 not in的方式么,方便我理解一下exists,很少用exists,不怎么理解。
    谢谢。
      

  3.   


    说不太清除,不过你用多了就明白了,就当exists是存在的意思
      

  4.   

    Select
       *
    from tb As a
    Where time=(Select Max(time) from tb As x 
                     Where x.no=a.no
              )
      

  5.   

    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 a where time not in (select time from tb where no=a.no and a.time < time)
    in 和 exists的区别为什么这么大?
      

  6.   

    想知道为什么 not in 和 not exists 为什么不能这样替换。
    他们的差别是什么。
    求指导、谢谢
      

  7.   

    一定要用not in 就是這樣吧,但是效率慢。
    select 

    from tb a 
    where time not in (select time from tb where no=a.no EXCEPT select max(time) from tb where no=a.no )
      

  8.   

    not exists 是表示不存在,下面這句是說不存在比 a表(別名) 更大的時間(同一個no),也就是說 a表 查詢同一個no里最大的時間。
    select * from tb a where not exists (select 1 from tb where no=a.no and a.time < time)not in 是表示不在列表內,這面這句是說 a表 的time列的值不在 子查詢里查詢出來的time列表(子查詢已經剔除同一個no最大的時間),也就是說查詢同一個no里最大的時間。
    select 

    from tb a 
    where time not in (select time from tb where no=a.no EXCEPT select max(time) from tb where no=a.no )
      

  9.   

    create table MaxTime (time varchar(20), [no] varchar(1),price numeric(16,4) )       --新建表格
    insert into MaxTime values ('11:00','a',10)       --在表格中插入数据
    insert into MaxTime values ('12:00','a',11)   
    insert into MaxTime values ('13:00','a',9)  
    insert into MaxTime values ('05:00','b',20)  
    insert into MaxTime values ('06:00','b',21)  
    insert into MaxTime values ('12:00','b',22)   
     
    select time ,[no],price from MaxTime as a
    where not exists(select * from MaxTime as x where x.[no] = a.[no]
    and x.time>a.time)
    drop table MaxTime
      

  10.   


    select *  from tk a where time=(select max(time) from tk where no=a.no )select * from tk a where time>=all(select time from tk where no=a.no)