开发者

top 10 from sql server

I want to retrieve the ID and DATE fields for the top 10 upcoming from my table.

Without the ID the query below works:

select top 10 MAX(FromDate) as upcomingda开发者_如何转开发tes 
from TM_Schedule 
group by(FromDate)

But if I add the ID, it throws an error:

select top 10 MAX(FromDate) as upcomingdates, ID 
from TM_Schedule 
group by(FromDate)


I don't know your data, but it looks like you can accomplish that without GROUP BY or MAX. Try this:

SELECT TOP 10 ID, FromDate
FROM TM_Schedule
ORDER BY FromDate DESC

It should work, unless you have multiple FromDate values for the same ID.


Select FromDate as upcomingdates, ID from TM_Schedule 
WHERE FromDate IN 
( 
   select top 10 MAX(FromDate)as upcomingdates from TM_Schedule group by(FromDate)
)
ORDER BY FromDate 


Do this:

select top 10 MAX(FromDate)as upcomingdates, ID 
from TM_Schedule group by(FromDate), ID

You need to group by ID also

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜