Skip to content
Advertisement

Query working in mySql and not working in Oracle

Below is the query which is working in MySQL and not working in Oracle. Here I need to get the latest Date and corresponding event from table r for every ID in Table a . ID is unique.


 SELECT a.name , a.spids , a.group , a.id , r.date,r.event  FROM a
    LEFT OUTER JOIN ( SELECT id, MAX(date) AS latest FROM r  GROUP BY id ) AS rev
        ON rev.id = a.id
    LEFT OUTER JOIN r  ON r.id = rev.id
       AND r.date = rev.latest
     group by a.id order by ID;
    --------------------------------------------- 
    *The error in Oracle is "Not a group by function"
    I read several blogs about group by function and every where they are saying to use an aggregate funciton like sum , min,max. I tried but not able to get the results*
    -----------------------
    Table a ( It has other column - group also)
    -----------------------


   ID  Name   Spids  
    1   SIML    TRDR,THYU
    2   SIML    YUIL
    3   ghhe    yhgu,hjuy,kiuj
    4   th      yuio


   ----------------------- 
    Table r  ( Needs to get the latest updated date and corresponding event details for every ID)
    ----------------------- 


   ID     Event         Date
    1      by chris      02-02-2016
    1      by Steve      02-02-2013
    1      by gen        02-02-2014
    2      by Pete       12-12-2018
    2      by ken        01-02-2014
    3      by Mike       20-08-2018
    3      by chin       20-08-2017
    4      by chn        04-06-2012
    4      by tink       06-06-2017

Output should be like this —————————

NAMe     SPIDS           GROUP      ID      DATE                  EVENT
    SIML    TRDR,THYU        Test        1    02-02-2016             by chris
    SIML    YUIL             Test        2    12-12-2018             by Pete 
    ghhe    yhgu,hjuy,kiuj   Test2       3    20-08-2018             by Mike
    th      yuio             Test1       4    06-06-2017             by tink

Advertisement

Answer

Your select is:

select a.name , a.spids , a.group , a.id , r.date, r.event

Your group by is:

 group by a.id

These are incompatible — what are the values of all the other columns in the select apart from a.id. That MySQL supports this is a (mis)feature of the database, not supported by almost any other database.

The most typical solution is to fix these so they are compatible:

select a.name, a.spids, a.group, a.id, max(r.date), max(r.event)

Your group by is:

 group by a.name , a.spids , a.group , a.id

In your case, the group by is probably not needed at all:

SELECT a.name, a.spids, a.group, a.id, r.date, r.event  
FROM a LEFT OUTER JOIN
     (SELECT id, MAX(date) AS latest
      FROM r
      GROUP BY id
     ) rev
     ON rev.id = a.id LEFT JOIN
     r 
     ON r.id = rev.id  AND r.date = rev.latest
ORDER BY ID;

You only need it if there are multiple rows with the same max date.

The most common solution in Oracle fixes this problem:

SELECT a.name, a.spids, a.group, a.id, r.date, r.event  
FROM a LEFT JOIN
     (SELECT r.*,
             ROW_NUMBER() OVER (PARTITION BY r.id ORDER BY r.date DESC) as seqnum
      FROM r
     ) rev
     ON rev.id = a.id AND seqnum = 1
ORDER BY ID;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement