查询在MySQL中工作而在Oracle中不工作

时间:2018-08-24 10:31:15

标签: mysql sql oracle

下面是在MySQL中运行但在Oracle中不运行的查询。 在这里,我需要从表r获取最新的Date和相应的事件  对于表a中的每个ID。 ID是唯一的。


 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

  

输出应该像这样       ---------------------------

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

2 个答案:

答案 0 :(得分:1)

您的select是:

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

您的group by是:

 group by a.id

这些不兼容-select中除a.id以外的所有其他列的值是什么? MySQL支持此功能是数据库的(错误)功能,几乎所有其他数据库均不支持。

最典型的解决方案是修复这些问题以使其兼容:

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

您的group by是:

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

在您的情况下,可能根本不需要group by

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;

仅当有多个具有相同最大日期的行时才需要。

Oracle中最常见的解决方案可解决此问题:

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;

答案 1 :(得分:0)

在执行n时,您正在聚合结果,因此select应该仅考虑聚合列,即group by并在这种情况下调用聚合函数。由于在此特定示例中a.ida.id,因此如果您问我,在逻辑上,您可以在MySQL的primary key子句中使用a的其他列。但是,就这样的select中的r列而言,也有可能在MySQL中产生错误,这是危险的,除非{{1 }}。您可以通过在group by子句中列出r子句中使用的所有非聚合列来解决此问题。