query在mysql中工作而在oracle中不工作

sulc1iza  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(331)

下面是在mysql中工作而在oracle中不工作的查询。这里我需要从表r中获得表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

输出应该是这样的

0pizxfdo

0pizxfdo1#


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
31moq8wy

31moq8wy2#

当你在做一个 group by ,您正在聚合结果,因此select应该只考虑聚合列,这是 a.id 在这种情况下调用聚合函数。自 a.id 是一个 primary key 在这个特定的示例中,您的 a 可用于 select 如果你问我的话,mysql中的子句,非常符合逻辑。然而,一列 r 以这样的方式 group by 在mysql中出错的可能性方面也是危险的,除非有一个 r 有史以来最多的 a . 您可以通过列出 select 条款进入 group by 条款。

gajydyqb

gajydyqb3#

你的 select 是:

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

你的 group by 是:

group by a.id

这些是不兼容的--表中所有其他列的值是多少 select 除了 a.id . mysql支持这是数据库的一个(mis)特性,几乎任何其他数据库都不支持。
最典型的解决方案是修复这些问题,使其兼容:

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;

相关问题