Search code examples
oracle-databasepeoplesoft

Rewriting query with table join containing GROUP BY clause


Is it possible to rewrite the following query

SELECT      CT.GROUP, CT.EMP_ID, HT.EFF_DT
FROM        CURR_TABLE CT
JOIN        (SELECT     GROUP, EMP_ID, MAX(EFF_DT) AS EFF_DT
            FROM        HIST_TABLE
            WHERE       STAT = 'A' 
            GROUP BY    GROUP, EMP_ID) HT ON CT.GROUP = HT.GROUP AND 
            CT.EMPID = HT.EMP_ID
WHERE       CT.GROUP = :1
AND         CT.EMP_ID = :2

in a way that is similar to CROSS JOIN style?

SELECT table1.column1, table2.column2...
FROM  table1, table2 [, table3 ]

The reason is that I want to create such query in Peoplesoft, and the above can only be achieved by creating a separate view for the selection with the group by clause. I want to do this just in one query without creating additional views.


Solution

  • You may try writing your query as a single level join with an aggregation:

    SELECT
        CT.GROUP,
        CT.EMP_ID,
        MAX(HT.EFF_DT) AS EFF_DT
    FROM CURR_TABLE CT
    LEFT JOIN HIST_TABLE HT
        ON CT.GROUP = HT.GROUP AND
           CT.EMPID = HT.EMP_ID AND
           HT.STAT = 'A'
    WHERE
        CT.GROUP = :1 AND
        CT.EMP_ID = :2
    GROUP BY
        CT.GROUP,
        CT.EMP_ID;
    

    Note that GROUP is a reserved SQL keyword, and you might have to escape it with double quotes to make this query (or the one in your question) work on Oracle.