带连接的Oracle SQL Max

时间:2015-11-12 18:48:32

标签: sql oracle max

我有两个表由一列连接。我需要使用Max来获取其中一列的最新日期。

表1(tbl1)

id     name    join_id

表2(tbl2)

id     start_date    end_date

到目前为止,这是我的问题:

SELECT DISTINCT tbl1.id, tbl1.name, tbl2.start_date, MAX(tbl2.end_date)
FROM tbl1
JOIN tbl2 ON tbl1.join_id = tbl2.id
WHERE tbl1.id = 4
AND tbl1.name LIKE 'Something%'
GROUP BY tbl1.id, tbl1.name, tbl2.start_date

我不知道出了什么问题。我得到像

这样的错误
FROM keyword not found where expected

显然我的查询中有一个FROM子句。 请帮帮我们。

修改

SELECT DISTINCT prsl.PROJ_NO, NVL(prsl.DEADLINE, prsl.SUBMIT_DATE) AS prsl.COMP_DATE,
prsl.SPON_PROG, prsl.PROP_STATUS, prsl.AWD_START, prsl.AWD_END, prsl.AWD_AMT,
dlvr.DUEDATE, MAX(dlvr.ST_STAT_DATE)
FROM ra_v_q_ie_prsl03 prsl
JOIN ra_v_q_ie_pt_deliver dlvr ON prsl.PROJ_NO = dlvr.PROJ_NO
WHERE prsl.EMPL_NO = '{$empid}'
AND prsl.FIRST_PD = 1
AND (prsl.SPON_PROG LIKE 'UM-SSHRC RGP%'
OR prsl.SPON_PROG LIKE 'UM-SSHRC TGP%'
OR prsl.SPON_PROG LIKE 'URGP%'
OR prsl.SPON_PROG LIKE 'UCRP%'
OR prsl.SPON_PROG LIKE 'UIRP%'
OR prsl.SPON_PROG LIKE 'Creative Works%')
GROUP BY prsl.PROJ_NO, prsl.SPON_PROG, prsl.PROP_STATUS, prsl.AWD_START, prsl.AWD_END, prsl.AWD_AMT, dlvr.DUEDATE

1 个答案:

答案 0 :(得分:1)

把它放在评论中,但在这里格式化我认为你需要将AS prsl.COMP_DATE更改为AS COMP_DATE

SELECT   DISTINCT prsl.PROJ_NO,
                    NVL (prsl.DEADLINE, prsl.SUBMIT_DATE) AS COMP_DATE,
                    prsl.SPON_PROG,
                    prsl.PROP_STATUS,
                    prsl.AWD_START,
                    prsl.AWD_END,
                    prsl.AWD_AMT,
                    dlvr.DUEDATE,
                    MAX (dlvr.ST_STAT_DATE)
    FROM      ra_v_q_ie_prsl03 prsl
           JOIN
              ra_v_q_ie_pt_deliver dlvr
           ON prsl.PROJ_NO = dlvr.PROJ_NO
   WHERE   prsl.EMPL_NO = '{$empid}' AND prsl.FIRST_PD = 1
           AND (   prsl.SPON_PROG LIKE 'UM-SSHRC RGP%'
                OR prsl.SPON_PROG LIKE 'UM-SSHRC TGP%'
                OR prsl.SPON_PROG LIKE 'URGP%'
                OR prsl.SPON_PROG LIKE 'UCRP%'
                OR prsl.SPON_PROG LIKE 'UIRP%'
                OR prsl.SPON_PROG LIKE 'Creative Works%')
GROUP BY   prsl.PROJ_NO,
           prsl.SPON_PROG,
           prsl.PROP_STATUS,
           prsl.AWD_START,
           prsl.AWD_END,
           prsl.AWD_AMT,
           dlvr.DUEDATE,
           dlvr.ST_STAT_DATE