这是我的样本数据。我想选择最受欢迎的借书月份。这是我迄今为止尝试过的:
SELECT COUNT(borrowdate) AS MostPopularMonthFROM borrower GROUP BY borrowdateORDER BY borrowdate DESC
SELECT COUNT(borrowdate)
AS MostPopularMonth
FROM borrower
GROUP BY borrowdate
ORDER BY borrowdate DESC
kpbpu0081#
在sql server中,可以使用 select top (1) :
select top (1)
SELECT TOP (1) YEAR(BorrowDate), Month(BorrowDate), COUNT(*) AS MostPopularMonthFROM borrower GROUP BY YEAR(BorrowDate), Month(BorrowDate)ORDER BY COUNT(*) DESC;
SELECT TOP (1) YEAR(BorrowDate), Month(BorrowDate), COUNT(*) AS MostPopularMonth
GROUP BY YEAR(BorrowDate), Month(BorrowDate)
ORDER BY COUNT(*) DESC;
如果存在关联,则返回任意匹配行。如果你想要所有的,使用 TOP (1) WITH TIES .
TOP (1) WITH TIES
1条答案
按热度按时间kpbpu0081#
在sql server中,可以使用
select top (1)
:如果存在关联,则返回任意匹配行。如果你想要所有的,使用
TOP (1) WITH TIES
.