选择最流行的月份

hfwmuf9z  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(297)

这是我的样本数据。我想选择最受欢迎的借书月份。

这是我迄今为止尝试过的:

  1. SELECT COUNT(borrowdate)
  2. AS MostPopularMonth
  3. FROM borrower
  4. GROUP BY borrowdate
  5. ORDER BY borrowdate DESC
kpbpu008

kpbpu0081#

在sql server中,可以使用 select top (1) :

  1. SELECT TOP (1) YEAR(BorrowDate), Month(BorrowDate), COUNT(*) AS MostPopularMonth
  2. FROM borrower
  3. GROUP BY YEAR(BorrowDate), Month(BorrowDate)
  4. ORDER BY COUNT(*) DESC;

如果存在关联,则返回任意匹配行。如果你想要所有的,使用 TOP (1) WITH TIES .

相关问题