如何在列中选择具有最大值的行?

时间:2016-08-12 19:45:25

标签: sql

我有下表

table_code        table_family       aa
---------------------------------------
F90                    OT            49 
F93                    OT             1
F92                    OT             1

我希望看到如下结果:

F90              OT                49

我尝试过以下SQL:

SELECT 
    table_code, MAX(table_family) table_family,  aa 
FROM 
    (SELECT 
         table_code, table_family, COUNT(*) aa
     FROM
         table1 
     GROUP BY 
         table_code, table_family) 
GROUP BY 
    table_code, AA 

这不起作用。

有人可以帮助我吗?

4 个答案:

答案 0 :(得分:5)

SELECT TOP(1) table_code, table_family, aa 
FROM table1 
ORDER BY aa DESC

答案 1 :(得分:0)

您是要查找要返回的单行还是包含最大值的所有行?大概你的实际表包含三行以上?

SELECT *
FROM table1
WHERE aa = (SELECT MAX(aa) FROM table1)

答案 2 :(得分:0)

请尝试以下代码:

SELECT TOP 1 *
FROM table1
ORDER BY aa DESC

答案 3 :(得分:0)

SELECT * FROM表WHERE aa =(SELECT MAX(aa)FROM table);