mysql喜欢分组在oracle中如何

时间:2012-07-20 15:42:40

标签: mysql oracle

在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的Mysql 我无法得到这个,这是我试图解决的长查询的一部分

2 个答案:

答案 0 :(得分:2)

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

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

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

如果您的mysql代码取决于所选的特定值,那么该代码就会被破坏。您需要确切地弄清楚您想要什么,并弄清楚如何在Oracle中获得它。

答案 1 :(得分: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