我有以下查询
SELECT DISTINCT XCS_TASK.WORKFLOW_ID,
XCS_TASK.COMPLETED_BY,
XCS_WORKFLOW.OBJECT_KEY,
XCS_WORKFLOW.OBJECT_TYPE_ID,
XCS_WORKFLOW.END_DATE_TIME,
XCS_WORKFLOW.START_DATE_TIME
FROM `XCS_TASK`
inner JOIN XCS_WORKFLOW ON
XCS_TASK.WORKFLOW_ID = XCS_WORKFLOW.WORKFLOW_ID
WHERE TASK_TYPE_ID = 124
GROUP BY XCS_WORKFLOW.OBJECT_KEY
ORDER BY XCS_WORKFLOW.START_DATE_TIME DESC
问题是我想获取该OBJECT_KEY的最新记录。我知道上面的查询是错误的,因为它将分组,然后对结果进行排序。我使用了MAX(DATE)函数,但无法在这种情况下使用它。任何帮助或指示,将不胜感激。
答案 0 :(得分:1)
您可以尝试加入OBJECT_KEY和最大日期(例如:start_date_time)的汇总结果
SELECT
XCS_TASK.WORKFLOW_ID,
XCS_TASK.COMPLETED_BY,
XCS_WORKFLOW.OBJECT_KEY,
XCS_WORKFLOW.OBJECT_TYPE_ID,
XCS_WORKFLOW.END_DATE_TIME,
XCS_WORKFLOW.START_DATE_TIME
FROM `XCS_TASK`
INNER JOIN XCS_WORKFLOW ON XCS_TASK.WORKFLOW_ID = XCS_WORKFLOW.WORKFLOW_ID
INNER JOIN (
SELECT
XCS_WORKFLOW.OBJECT_KEY,
MAX( XCS_WORKFLOW.START_DATE_TIME ) max_date
FROM XCS_WORKFLOW
GROUP BY OBJECT_KEY
) t ON t.OBJECT_KEY = XCS_WORKFLOW.OBJECT_KEY
AND XCS_WORKFLOW.OBJECT_KEY = t.max_date
WHERE TASK_TYPE_ID = 124