表:
ID in out
1 2000-01-01 00:00:00 2000-01-01 12:00:00
1 2000-01-02 00:00:00 NULL
2 2000-01-01 00:00:00 2000-01-01 12:00:00
2 2000-01-02 00:00:00 2000-01-02 11:00:00
3 2000-01-01 00:00:00 NULL
结果:
ID in out
1 2000-01-02 00:00:00 NULL
2 2000-01-02 00:00:00 2000-01-01 11:00:00
3 2000-01-01 00:00:00 NULL
所以我想在所有 ID的中获取最新的 in ,并根据ID对它们进行排序。 我尝试了GROUP BY,但似乎得到了有价值的那个。
我目前所拥有的与
类似SELECT *
FROM TABLE
GROUP BY ID
ORDER BY OUT IS NULL DESC, OUT DESC;
答案 0 :(得分:3)
您需要选择适当的值以及按以下顺序执行订单:
select t.*
from t join
(select t.id, max(t.in) as maxin
from t
group by t.id
) tsum
on t.id = tsum.id and t.in = tsum.maxin
order by id