日期:2014-05-17  浏览次数:20717 次

又是group by 的问题
有这样一张表   t1   (用的是oracle)
--------------------------------

id       content       version
1             11                   0.1
2             11                   1.0
3             22                   1.0
4             22                   2.0
5             22                   0.1
6             33                   3.0  
7             33                   4.0  

---------------------------------
希望得到的结果是这样的

id       content       version
2               11               1.0
4               22               2.0
7               33               4.0
---------------------------------

我想选出在各个content里   version最大的记录,我现在写的是
select   content,max(version)   group   by   version
,这样的话,不能把id   也选出来,有什么好一点的办法可以得到如上的结果,谢谢了!


------解决方案--------------------
不好意思
再仔细看了下,原来
select 1 as id,11 as content, '0.1 ' as version from dual
7 union all
8 select 2 as id,11 as content, '1.0 ' as version from dual
9 union all
10 select 3 as id,22 as content, '1.0 ' as version from dual
11 union all
12 select 4 as id,22 as content, '2.0 ' as version from dual
13 union all
14 select 5 as id,22 as content, '0.1 ' as version from dual
15 union all
16 select 6 as id,33 as content, '3.0 ' as version from dual
17 union all
18 select 7 as id,33 as content, '4.0 ' as version from dual
这段只是用来生成测试数据,前面回复时没看到后面的留言,所以想怎么弄得这么复杂
其实我觉得不需要用到partition by
一般用这个人不多,多数是用MAX来做比较的
------解决方案--------------------
呵呵,XD,因为我在公司,没建表和插数据的环境,只能查询,所以只有这样和XD们探讨问题了 ... 哎~~~