简体   繁体   中英

Oracle SQL Max with Joins

I have 2 tables joined by a single column. And I need to use Max to get the latest date on one of the columns.

Table 1 (tbl1)

id     name    join_id

Table 2 (tbl2)

id     start_date    end_date

So far this is my query:

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

I don't know what's wrong. I get errors like

FROM keyword not found where expected

When clearly I have a FROM clause in my query. Please help me guys.

EDIT:

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

put it in the comment but here it is formatted I think you need to change AS prsl.COMP_DATE to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM