0

在 Mysql 中,我有 fdllowing 查询以及下面的结果

drop table tab1;
CREATE TEMPORARY TABLE tab1 
(col1 integer,col2 integer(10),col3 varchar(10),col4 integer)engine=memory
insert into tab1
values(100,1,'Hello',9);
insert into tab1
values(200,1,'HelloWrld',8);
insert into tab1
values(300,1,'HelloTher',7);
insert into tab1
values(400,2,'HiThere',6);
insert into tab1
values(500,3,'Howdy',5);
insert into tab1
values(600,3,'Hiya',4);

select col1,col2,col3,col4,min(col4)
from tab1
group by col2

'100', '1', 'Hello', '9', '7'
'400', '2', 'HiThere', '6', '6'
'500', '3', 'Howdy', '5', '4'

在 Oracle 中,我想要与 Mysql 相同的结果

with tab1 as (
 select 100 col1, 1 col2, 'Hello' col3,9 col4  from dual
 union all
 select 200 col1, 1 col2, 'HelloWrld' col3,8 col4  from dual
 union all
 select 300 col1, 1 col2, 'HelloTher' col3,7 col4  from dual
 union all
 select 400 col1, 2 col2, 'HiThere' col3,6 col4  from dual
 union all
 select 500 col1, 3 col2, 'Howdy' col3,5 col4  from dual
 union all
 select 600 col1, 3 col2, 'Hiya' col3,4 col4  from dual
 )
 select min(col1),col2,min(col3),col4,min(col4)
 from tab1
 group by col2,col4

Result I get is this
 MIN(COL1)       COL2 MIN(COL3)       COL4  MIN(COL4)
---------- ---------- --------- ---------- ----------
       100          1 Hello              9          9
       200          1 HelloWrld          8          8
       500          3 Howdy              5          5
       600          3 Hiya               4          4
       300          1 HelloTher          7          7
       400          2 HiThere            6          6

我想要的是这个

'100', '1', 'Hello', '9', '7'
'400', '2', 'HiThere', '6', '6'
'500', '3', 'Howdy', '5', '4'

我如何在 Oracle 中实现 Mysql like group by 我无法得到这个,这是我试图解决的长查询的一部分

4

2 回答 2

2

根据 mysql 文档,未在 group by 中指定的列的结果可以来自任何行。

因此,Oracle 中一个完全合理的查询是:

 select min(col1),col2,min(col3),min(col4),min(col4)
 from tab1
 group by col2

如果您的 mysql 代码取决于选择的特定值,则该代码已损坏。你需要弄清楚你想要什么,并弄清楚如何在 Oracle 中得到它。

于 2012-07-20T15:48:43.287 回答
1

假设您的意图是获得确定性结果(与不确定的 MySQL 结果不同)并且您要保留的col1-col4数据是col1给定col2值具有最小值的行的数据,您可以使用分析函数

SQL> ed
Wrote file afiedt.buf

  1  with tab1 as (
  2   select 100 col1, 1 col2, 'Hello' col3,9 col4  from dual
  3   union all
  4   select 200 col1, 1 col2, 'HelloWrld' col3,8 col4  from dual
  5   union all
  6   select 300 col1, 1 col2, 'HelloTher' col3,7 col4  from dual
  7   union all
  8   select 400 col1, 2 col2, 'HiThere' col3,6 col4  from dual
  9   union all
 10   select 500 col1, 3 col2, 'Howdy' col3,5 col4  from dual
 11   union all
 12   select 600 col1, 3 col2, 'Hiya' col3,4 col4  from dual
 13   )
 14  select col1,
 15         col2,
 16         col3,
 17         col4,
 18         min_col4
 19    from (select col1,
 20                 col2,
 21                 col3,
 22                 col4,
 23                 min(col4) over (partition by col2) min_col4,
 24                 rank() over (partition by col2 order by col1) rnk
 25            from tab1)
 26*  where rnk = 1
SQL> /

      COL1       COL2 COL3            COL4   MIN_COL4
---------- ---------- --------- ---------- ----------
       100          1 Hello              9          7
       400          2 HiThere            6          6
       500          3 Howdy              5          4
于 2012-07-20T15:49:07.853 回答