日期:2014-05-17 浏览次数:20598 次
select * from tb t where datetime=(select max(datetime) from tb where bookid=t.bookid)
------解决方案--------------------
select * from tb t where [datetime]=(select max([datetime]) from tb where bookid=t.bookid)
------解决方案--------------------
select A.* from TB A where not exists(select 1 from TB B where A.booksid = B.booksid and A.datetime <B.datetime)
------解决方案--------------------
select b.booksid,b.datetime from table as b join
(
select booksid,max(datetime) from table as A group by booksid) as c
on b.booksid=c.booksid and b.datetime=c.datetime
ls几位的也可以的,很多种办法
------解决方案--------------------
select b.booksid,b.datetime from table as b join
(
select booksid,max(datetime)as datetime from table as A group by booksid) as c
on b.booksid=c.booksid and b.datetime=c.datetime
------解决方案--------------------
create table ny
(booksid int,dt time)
insert into ny
select 1, '10:00:00' union all
select 1, '12:00:00' union all
select 2, '07:00:00' union all
select 2, '11:00:00'
select booksid,dt
from
(select *,row_number() over(partition by booksid order by dt desc) rn
from ny) y
where rn=1
/*
booksid dt
----------- ----------------
1 12:00:00.0000000
2 11:00:00.0000000
(2 row(s) affected)
*/