0
select aaa.BookId from
(SELECT     TOP (100) PERCENT Id, BookId,CreatedOnUtc
                       FROM          dbo.Chapter AS c
                       ORDER BY CreatedOnUtc desc             
                       ) as aaa
group by aaa.BookId

为什么 ORDER BY CreatedOnUtc desc 对结果没有任何影响。

我想得到这样的结果

BookId    (with desc createdonUtc)
3
4
1
...

更新:我同意这个

select BookId, Max(CreatedOnUtc)
from Chapter
group by BookId
order by Max(CreatedOnUtc) desc
4

1 回答 1

2

ORDER BY 仅适用于您的子查询。您的外部查询没有 order by 子句,因此结果可以是任何顺序。

其他事情:

  • 为什么TOP 100 PERCENT
  • 为什么SELECT BookIdGROUP BY BookId没有聚合列?也许你只是想要SELECT DISTINCT

此外,您想要实现的目标可能是不可能的。如果此表具有重复的 BookId:

ID BookId CreatedOnUtc
1  1      1
2  2      2
3  1      3

你的结果应该按什么顺序排列?1 2 还是 2 1?

于 2013-10-30T10:30:49.230 回答