仅显示列中的最高值

时间:2014-06-04 10:07:10

标签: sql

我希望仅显示“已租借”别名中的最高值

SELECT
Registration,
COUNT(1) AS RENTED
FROM
I_Booking
GROUP BY Registration
ORDER BY RENTED DESC

不知道从哪里开始

3 个答案:

答案 0 :(得分:1)

也许您可以尝试以下方法:

      SELECT  TAB.Registration, MAX(TAB.RENTED)
      FROM (
        SELECT
            Registration,
            COUNT(1) AS RENTED
            FROM
            I_Booking
            GROUP BY Registration ) TAB
      GROUP BY TAB.Registration

答案 1 :(得分:0)

添加TOP 1应该足以留下“已租用”中的最高值。列,如果这是你想要的:

SELECT TOP 1
  Registration,
  COUNT(1) AS RENTED
FROM
  I_Booking
GROUP BY Registration
ORDER BY RENTED DESC

答案 2 :(得分:-1)

如果您尝试获取最常发生的注册,请使用COUNT(1)中的ORDER BY代替别名:

SELECT Registration,COUNT(1) AS RENTED
FROM I_Booking
GROUP BY Registration
ORDER BY COUNT(1) DESC