1

这就是我现在在查询中的内容:

+--------------------+-----------+------------+
| status             | entity_id | seconds    |
+--------------------+-----------+------------+
| Submitted          |       494 | 1352102400 |
| Accepted           |       494 | 1352275200 |
| In press/e-publish |       494 | 1352966400 |
| Rejected           |       520 | 1355817600 |
| Accepted           |       570 | 1352102400 |
+--------------------+-----------+------------+

我希望它看起来像:

+--------------------+-----------+------------+
| status             | entity_id | seconds    |
+--------------------+-----------+------------+
| In press/e-publish |       494 | 1352966400 |
| Rejected           |       520 | 1355817600 |
| Accepted           |       570 | 1352102400 |
+--------------------+-----------+------------+

在准 SQL 中:

SELECT status, entity_id, MAX(seconds) 
FROM foo
GROUP BY entity_id, seconds

上面的准SQL看起来是正确的,但是“status”列的值并没有对应正确的行。我得到如下内容:

+--------------------+-----------+------------+
| status             | entity_id | seconds    |
+--------------------+-----------+------------+
| Submitted          |       494 | 1352966400 |
| Rejected           |       520 | 1355817600 |
| Accepted           |       570 | 1352102400 |
+--------------------+-----------+------------+
4

2 回答 2

6

未经测试,但应如下所示:

SELECT status, entity_id, seconds
FROM entities E
WHERE E.seconds == (
  SELECT MAX(E2.seconds)
  FROM entities E2
  WHERE E2.entity_id = E.entity_id
)

(为您的问题设置一个 SQLfiddle 将为您提供更经过测试的答案:p)

于 2012-12-20T00:48:59.947 回答
3

以下查询将给出您预期的结果。

SELECT max(maxsec), status, entity_id from 
(SELECT status, entity_id, MAX(seconds)  as maxsec
FROM table1
GROUP BY entity_id,status) a GROUP BY entity_id

您在此处创建的架构。

于 2012-12-20T04:21:12.893 回答