SQL Server语法 - 我有这个表:
C1 - C2 - C3
--------------
2 - AA - 10
2 - AA - 20
3 - AA - 15
3 - AA - 16
3 - AA - 56
1 - BB - 54
2 - BB - 53
2 - BB - 89
我需要一个会返回此结果的查询:
C1 - C2 - C3
--------------
3 - AA - 15
3 - AA - 16
3 - AA - 56
2 - BB - 53
2 - BB - 89
因此它应该只返回最大值为C1的行。
我该怎么做?
答案 0 :(得分:1)
SELECT t1.C1,t1.C2,t1.C3
FROM Table1 t1 INNER JOIN (SELECT MAX(C1) C1,C2 FROM table1 GROUP BY C2)
as t2 on t1.C1=t2.C1 and t1.C2=t2.C2
Order by C2
输出
C1 C2 C3
3 AA 15
3 AA 16
3 AA 56
2 BB 53
2 BB 89
现场演示
答案 1 :(得分:1)
您可以在max c1上使用内部联接来获取c2
select m.*
from my_table m
inner JOIN (
select max(C1) max_c1, C2
from my_table
group C2 ) t on t.max_c1 = m.C1 and t.C2 = m.C2