字段A 字段B 字段C 字段D
1 1 aaa bbb
2 1 ds ds
3 1 ff ff
4 2 33 33
5 2 44 44
6 3 hh hh
7 3 hh hh
8 3 tt tt要求结果:
字段A 字段B 字段C 字段D
3 1 ff ff
5 2 44 44
8 3 tt tt即:按照B字段分组,取每组中的A字段最大值的记录
1 1 aaa bbb
2 1 ds ds
3 1 ff ff
4 2 33 33
5 2 44 44
6 3 hh hh
7 3 hh hh
8 3 tt tt要求结果:
字段A 字段B 字段C 字段D
3 1 ff ff
5 2 44 44
8 3 tt tt即:按照B字段分组,取每组中的A字段最大值的记录
解决方案 »
- SQL做日历,挑战一下吧,100分
- 自联表怎么更新的问题。
- Oracle Sql Scratchpad中能执行存储过程吗?用execute
- Oracle优化问题
- 菜鸟级提问有关job的,为什么这段代码编译不了[代码很简单请高人们抬眼看看,拜托了]
- 求一条sql语句
- 为什么找不到pfile文件initSID.ora和spfile文件呢?
- 我想连接着三个表使用以上条件在oralce中用聚合函数和汇总函数求20060126当天的总订单金额。请指教。谢谢!!!
- 请问日文系统下的软件改在中文下运行可以吗
- 如何卸载AIX中oracle?
- 请问在EXECUTE IMMEDIATE中如何使用带有引号的函数?
- oracleconnection中的连接字符串Integrated Security=yes的问题
from (select 字段A,字段B,字段C,字段D,row_number() over(partition by 字段B,order by 字段A desc) rn
from table)
where rn=1
where rowid in (select rowid,max(字段A),字段B from table group by 字段B)
CREATE TABLE table1(
A NUMBER,
B NUMBER,
C VARCHAR(10),
D VARCHAR(10)
)INSERT INTO table1
VALUES(1,1,'aaa','bbb');INSERT INTO table1
VALUES(2,1,'ds','ds');INSERT INTO table1
VALUES(3,1,'ff','ff');INSERT INTO table1
VALUES(4,2,'33','33')INSERT INTO table1
VALUES(5,2,'44','44');INSERT INTO table1
VALUES(6,3,'hh','hh');INSERT INTO table1
VALUES(7,3,'hh','hh');INSERT INTO table1
VALUES(8,3,'tt','tt')SELECT *
FROM table1
WHERE A IN (SELECT A FROM (SELECT MAX(A) A, B FROM table1 GROUP BY B))结果为:1 3 1 ff ff
2 5 2 44 44
3 8 3 tt ttDROP TABLE table1
select * from table where 字段A in(select max(字段A) from table group by 字段B)
CREATE TABLE tt(a INT,b INT,c VARCHAR2(10),d VARCHAR2(10));insert into tt values(1,1,'aaa','bbb');
insert into tt values(2,1,'ds','ds');
insert into tt values(3,1,'ff','ff');
insert into tt values(4,2,'33','33');
insert into tt values(5,2,'44','44');
insert into tt values(6,3,'hh','hh');
insert into tt values(7,3,'hh','hh');
insert into tt values(8,3,'tt','tt');SELECT A, B, C, D
FROM (SELECT A,
ROW_NUMBER() OVER(PARTITION BY B ORDER BY A DESC) R,
B,
C,
D
FROM TT)
WHERE R = 1;输出:
A B C D
3 1 ff ff
5 2 44 44
8 3 tt tt
select * from table where 字段A in(select max(字段Afrom table group by 字段B);这样如果字段A中有重复的数据也会显示出来,
比如:A B
1 1
3 1
3 2
4 2
这样A为3的就都会被选则出来
不知lz是否确定A不重复