我想从我的桌子中检索前10名的ID
和DATE
字段。
如果没有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)
答案 0 :(得分:1)
我不知道您的数据,但看起来您可以在没有GROUP BY
或MAX
的情况下实现这一目标。试试这个:
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分组