1

我有一个包含列的表:
s_Id (primary int), sup_type (int), sup_date (datetime), sup_req (int) 到目前为止sup_type

的值范围是 0-5。 如何提取每个sup_type (0-5)的最新条目(按sup_date )。

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*  s_Id   *  sup_type   *        sup_date         *  sup_req  *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*    1    *      0      *   2012-06-15 10:13:21   *     4     *
*    2    *      0      *   2012-06-15 11:22:01   *     4     *
*    3    *      1      *   2012-06-15 13:23:32   *     4     *
*    4    *      2      *   2012-06-16 08:04:29   *     4     *
*    5    *      1      *   2012-06-16 16:23:24   *     4     *
*    6    *      1      *   2012-06-17 13:14:05   *     4     *
*    7    *      3      *   2012-06-18 13:37:55   *     4     *
*    8    *      4      *   2012-06-19 13:21:52   *     4     *
*    9    *      4      *   2012-06-20 16:15:19   *     4     *
*   10    *      5      *   2012-06-20 16:17:37   *     4     *
*   11    *      0      *   2012-06-20 16:21:53   *     4     *
*   12    *      1      *   2012-06-20 16:28:13   *     4     *
*   13    *      3      *   2012-06-21 12:23:29   *     4     *
*   14    *      3      *   2012-06-22 07:26:41   *     4     *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

我要提取

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*  s_Id   *  sup_type   *        sup_date         *  sup_req  *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*    4    *      2      *   2012-06-16 08:04:29   *     4     *
*    9    *      4      *   2012-06-20 16:15:19   *     4     *
*   10    *      5      *   2012-06-20 16:17:37   *     4     *
*   11    *      0      *   2012-06-20 16:21:53   *     4     *
*   12    *      1      *   2012-06-20 16:28:13   *     4     *
*   14    *      3      *   2012-06-22 07:26:41   *     4     *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

谢谢。

4

1 回答 1

4
SELECT
    b.*
FROM
    (
        SELECT sup_type, MAX(sup_date) AS maxsupdate
        FROM tbl
        GROUP BY sup_type
    ) a
INNER JOIN
    tbl b ON 
        a.sup_type = b.sup_type AND 
        a.maxsupdate = b.sup_date
ORDER BY
    b.s_Id
于 2012-06-23T07:14:59.653 回答