来自sql server的前10名

时间:2011-09-22 13:46:28

标签: sql sql-server

我想从我的桌子中检索前10名的IDDATE字段。

如果没有ID,则下面的查询有效:

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

但如果我添加ID,则会抛出错误:

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

3 个答案:

答案 0 :(得分:1)

我不知道您的数据,但看起来您可以在没有GROUP BYMAX的情况下实现这一目标。试试这个:

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

它应该有效,除非您对同一FromDate有多个ID值。

答案 1 :(得分:0)

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 

答案 2 :(得分:0)

这样做:

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

您还需要按ID分组