2012-07-20 54 views
0

在MySQL有以下MySQL的像甲骨文GROUPBY我如何

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沿fdllowing查询我想同样的结果在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' 

我如何实现Mysql像gro在 我无法得到这,这是我想解决的长查询

+3

MySQL的结果是[indeterminate](http://dev.mysql.com/doc/refman/5.1/en/group-by-hidden-columns.html) 。 – 2012-07-20 15:45:00

+0

你到底在做什么?这两个查询有不同的'group by'子句。你是否想要获得每列,一列或其他内容的最小值? – 2012-07-20 15:49:05

+0

@MarkByers - 在我看来,这是最糟糕的行为之一。我想我宁愿有一些特殊的语法来表示(比如说'GROUP BY col1,HIDDEN(col2)'或其他),以使行为更加明确。事实上,如果你错误地输入了一些东西,你的结果可能会很糟糕,甚至不会提示你。 – 2012-07-20 15:51:10

回答

1

假设你的目的是要获得一个确定的结果(不同于MySQL的结果是不确定的),而col1 - 要保留col4数据是对于给定的col2最小col1值的行数据值,您可以使用分析函数

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 
+0

嗨贾斯汀什么是非确定性的如果运行查询两次我不会得到相同的结果这是你的意思 – 2012-07-20 16:09:06

+0

@ jhon.smith - 有可能得到不同的结果,是的。实际上,如果在没有对表格进行任何更改时运行查询两次,则可能会得到相同的结果。但最终,你可能会得到不同的结果。这可能会在明天发生,可能会在一年后发生,当您升级数据库时可能会发生这种情况。 – 2012-07-20 16:18:24

+0

贾斯汀我必须标记你的答案是正确的希望stackoverflow允许我标记两个正确的答案,因为马克·拜尔斯在技术上是正确的也不会从马克获得信贷 – 2012-07-20 16:22:52

2

的一部分根据mysql文档,组中未指定列的结果可以来自任何行。

所以,甲骨文完全合理查询:

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

如果你的MySQL代码是依赖于一个特定的值被选中,则该代码被打破。你需要弄清楚你想要什么,并找出如何在Oracle中获得。