1

我需要根据创建时间获得最大优先级值。我的查询在这里,

select
  t.id, ocf1.Content Priority, ocf1.created
from
  Tickets t inner Join ObjectCustomFieldValues ocf1
  on ocf1.ObjectId=t.id
where
  ocf1.CustomField=301
  and t.queue in (27,50,49,52)
  and t.status not in ('rejected','deleted')
  and t.created between subdate(now(),interval 1500 minute) and now();

id     Priority  created
-------------------------------
27344  P4        28/01/13 09:17
27344  Incident  28/01/13 09:18
27345  P4        28/01/13 09:22
27346  P4        28/01/13 09:25
27353  P4        28/01/13 10:08
27353  Incident  28/01/13 10:09
27360  P4        28/01/13 10:38
27360  Incident  28/01/13 10:39
27365  P3        28/01/13 11:52
27376  P4        28/01/13 14:49

示例:这里我需要Incidentid的最大优先级值27344。与其他 ID 相同。

4

1 回答 1

3
SELECT  a.*, b.*      -- select your desired columns here
FROM    Tickets a
        INNER JOIN ObjectCustomFieldValues b
            ON b.ObjectId = a.id
        INNER JOIN
        (
            SELECT  ObjectId, MAX(created) max_val
            FROM    ObjectCustomFieldValues
            WHERE   CustomField = 301
            GROUP   BY ObjectId
        ) c ON  b.ObjectId = c.ObjectId AND
                b.created = c.max_val
WHERE   b.CustomField = 301 AND
        a.queue IN (27,50,49,52) AND
        a.status NOT IN ('rejected','deleted') AND 
        a.created BETWEEN SUBDATE(NOW(), INTERVAL 1500 MINUTE) AND NOW()

你的日期真的是这样格式化的:28/01/13 09:17?数据类型是字符串。对?如果是这种情况,您需要将其转换为日期时间,这样MAX()才能正常工作。

于 2013-01-29T09:46:22.117 回答