我试图将最新的编译日期从一个表格转换为另一个表格
到目前为止我写的是什么
SELECT
case.case_number,
report.compiled_date
FROM
case
INNER JOIN report ON report.case_number = case.case_number
WHERE
case.case_active = 1
ORDER BY
case.case_number ASC,
report.compiled_date DESC
给了我: -
case_number | compiled_Date
1001 | 23/07/2013
1001 | 23/06/2013
1001 | 01/03/2013
1001 | 02/02/2013
1002 | 24/12/2013
1002 | 23/09/2013
1002 | 01/08/2013
1003 | 23/09/2013
1003 | 12/09/2013
1004 | 23/11/2013
我已尝试过group by和max无效,如果我正确阅读,我认为这将是一个嵌套选择?
正如我所期望的那样: -
case_number | compiled_Date
1001 | 23/07/2013
1002 | 24/12/2013
1003 | 23/09/2013
1004 | 23/11/2013
感谢您的帮助!!
答案 0 :(得分:4)
您需要添加GROUP BY子句以按case_number分组,然后您可以使用聚合函数MAX:
SELECT
case.case_number,
MAX(report.compiled_date)
FROM
case
INNER JOIN report ON report.case_number = case.case_number
WHERE
case.case_active = 1
GROUP BY
case.case_number
ORDER BY
case.case_number ASC
答案 1 :(得分:0)
添加GROUP BY子句
GROUP BY report.compiled_date