oracle 查询数据只要排序后的第一条记录(不用rownum=1),sql语句怎么写

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle 查询数据只要排序后的第一条记录(不用rownum=1),sql语句怎么写相关的知识,希望对你有一定的参考价值。

SELECT field1, field2, field3... fieldn
FROM (SELECT field1, field2, field3...fieldn, ROW_NUMBER() OVER(ORDER BY field1 DESC) AS RK ) FROM tab ) t
WHERE rk = 1
把这个语句嵌套到别的语句里面后
select t.a,t.b,
(SELECT field1, field2, field3... fieldn
FROM (SELECT field1, field2, field3...fieldn, ROW_NUMBER() OVER(ORDER BY field1 DESC) AS RK ) FROM tab where field1=t.a) t
WHERE rk = 1)
from lkk t;
会报错 ora-00904 where field1=t.a) "T"."A"请教高手怎么改

1、创建测试表,

create table test_order(id number, value varchar2(50));

2、插入测试数据

insert into test_order values(3,'v3');

insert into test_order values(2,'v2');

insert into test_order values(1,'v1');

insert into test_order values(5,'v5');

insert into test_order values(4,'v4');

commit;

3、查询表中全量数据,可以发现数据并未排序,select t.*, rowid from test_order t;

4、编写sql,数据只要排序后的第一条记录(不用rownum=1) select * from (select t.*, row_number() over(order by id) rn from test_order t) t where rn = 1;

参考技术A 假设排序的字段名字为field1降序, table name: tab
SELECT field1, field2, field3... fieldn
FROM (SELECT field1, field2, field3...fieldn, ROW_NUMBER() OVER(ORDER BY field1 DESC) AS RK ) FROM tab ) t
WHERE rk = 1

00904是说列名不正确,仔细看了下,AS RK后面多了一个括号,下面这样就可以了
SELECT field1, field2, field3... fieldn
FROM (SELECT field1, field2, field3...fieldn,
ROW_NUMBER() OVER(ORDER BY field1 DESC) AS RK
FROM tab
where field1=表名.字段名) t
WHERE rk = 1

我给你改一下吧,你写的那个效率很低的
SELECT t1.a, t1.b, t2.field1, t2.field2, t2.field3...t2.fieldn
FROM lkk t1,
(SELECT field1, field2, field3...fieldn,
ROW_NUMBER() OVER(ORDER BY field1 DESC) AS RK
FROM tab) t
WHERE t.field1 = t1.a
AND t.rk = 1本回答被提问者和网友采纳
参考技术B 首先在 XSREMOBILE 表上建 SENDDATE和ID 的复合索引.

把 WHERE ROWNUM < 50 + 1
改为 WHERE ROWNUM <= 50

应该可以快些.

SELECT /*+ FIRST_ROWS */ *
FROM (SELECT /*+ parallel(M,10) */ *
FROM XSREMOBILE M
WHERE M.SENDDATE >= 20101126
AND M.SENDDATE <= 20101126
oRDER BY M.ID DESC
)
WHERE ROWNUM <= 50
参考技术C SELECT field1, field2, field3... fieldn
FROM (SELECT field1, field2, field3...fieldn, ROW_NUMBER() OVER(ORDER BY field1 DESC) AS RK ) FROM tab ) t
WHERE rk = 1
把这个语句嵌套到别的语句里面后
select t.a,t.b,
(SELECT field1, field2, field3... fieldn
FROM (SELECT field1, field2, field3...fieldn, ROW_NUMBER() OVER(ORDER BY field1 DESC) AS RK ) FROM tab where field1=t.a) t
WHERE rk = 1)
from lkk t;
会报错 ora-00904 where field1=t.a)
参考技术D

如果该表有时间或数值类型字段的话可以采用如下;

SELECT * 

            FROM table a 

            WHERE a.time = (SELECT MAX (b.time) 

                                          FROM table b) ;

取出分组后每组的第一条记录(不用group by)按时间排序

 

--操作日志表
CREATE TABLE [dbo].[JobLog](

    [JobLogId] [int] IDENTITY(1,1) NOT NULL,

    [FunctionId] [nvarchar](20) NULL,

    [OperateTime] [datetime] NULL,

 CONSTRAINT [PK_JobLog] PRIMARY KEY CLUSTERED

(

    [JobLogId] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]

GO

 

-- 操作日志表的所有记录
select * from JobLog

查询结果:
1    001    2007-11-01
2    001    2007-11-02
3    001    2007-11-03
4    002    2007-11-04
5    002    2007-11-05
6    003    2007-11-06
7    004    2007-11-07
8    004    2007-11-08
9    005    2007-11-09
10  005    2007-11-10

 

-- 每个功能最后一次操作记录
SELECT * FROM JobLog A

WHERE JobLogId in

(SELECT TOP 1 JobLogId FROM JobLog WHERE A.FunctionId = FunctionId ORDER BY OperateTime DESC)


查询结果:
3    001    2007-11-03
5    002    2007-11-05
6    003    2007-11-06
8    004    2007-11-08
10  005    2007-11-10

 

以上是关于oracle 查询数据只要排序后的第一条记录(不用rownum=1),sql语句怎么写的主要内容,如果未能解决你的问题,请参考以下文章

oracle 取第一条或者最后一条记录

oracle分组后的整条记录怎么取

Oracle取查询结果数据的第一条记录SQL

oracle记录中选择出第一条记录

Oracle-left join两表关联只取B表匹配到的第一条记录

ORACLE 查询去重保留第一条排序