0

桌子 :

 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的最新信息,并根据 ID 对它们进行排序。我尝试了 GROUP BY ,但这似乎得到了有价值的。

我现在拥有的类似于

SELECT * 
  FROM TABLE 
GROUP BY ID 
ORDER BY OUT IS NULL DESC, OUT DESC;
4

1 回答 1

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
于 2013-04-24T02:47:02.763 回答