我很难绕过这一个,这应该很简单。
从表中选择时,如果多行具有相同的ID,则选择在Col2中具有更大值的行。
这是我的示例表:
Exception
预期产出:
ID | Col2 |
----------------
123 | 1 |
123 | 2 |
1234 | 2 |
12345 | 3 |
答案 0 :(得分:5)
对于此示例,group by
就足够了;
select id, max(col2) as col2
from t
group by id;
如果您希望行包含最大列,那么我经常会推荐row_number()
:
select t.*
from (select t.*, row_number() over (partition by id order by col2 desc) as seqnum
from t
) t
where seqnum = 1;
但是,“老式”方法可能会有更好的表现:
select t.*
from t
where t.col2 = (select max(t2.col2) from t t2 where t2.id = t.id);
答案 1 :(得分:0)
NOT EXISTS运算符:
onclick="window.location.href = 'https://www.google.com'"
演示:http://sqlfiddle.com/#!18/5e1d6/3
SELECT * FROM Table1 t1
WHERE NOT EXISTS(
SELECT 'Anything' FROM Table1 t2
WHERE t1.id = t2.id
AND t1.Col2 < t2.col2
)