select * from (select field1,field2,field3,......,row_number() over (order by 排序字段 desc ) rn from tablename) where rn<N;
加一个列F1,F1值为sequence自增变量,然后取值F1值<20的即可。
select * from (select fieldname from tablename group by fieldname) where rownum<N
To Shrewdcat(丧邦&灵猫&潇) 你的主意和我想的一样,我是加了一个循环变量,但是放的地儿好象不对,运行几次就报错了,真晕…………
select * from (select col1,col2,rownum as rn from tablename order by fieldname) where rn<20;
select * from (select rownum num,field1,field2..... from tablename) where num<N order by fieldname
doulikeme(来去无踪)你这样写还是不行的,要把order by 放到最外面
to ORARichard(没钱的日子......) 我在8i里没问题的,可以执行的
To海龙,没钱的日子…………那种SQL都不成了,那个rownum是按数据插入的时候的顺序生成的,这种查询根本没有用啊?晕了………… 你可以试试 select rownum,fieldname1,fieldname2 from tablename order by fieldname2. 这时如果按你们所说的那种SQL查询fieldname2的前N名,查出来是不对的了。
SQL> ed 已写入文件 afiedt.buf 1 select lffh,id from 2 ( 3 select rownum no,lffh,id from 4 ( 5 select lffh,id from jbxx 6 group by lffh,id 7 ) 8 ) 9 where rownum<10 10* order by no SQL> /LFFH ID --------------------- --------------------- 121 111 122 111 12345 12345 123456 123456 2 a 511822111200304300074 20030001 511822111200308110072 20030179 511822111200401060010 20040007 511822111200401310082 20040014已选择9行。
To: doulikeme(来去无踪) 你那个查询只是巧合而已,你试着查一个你不是按顺序录入的数据,之后再按顺序排序就不OK了。
select * from ( select col1,col2,...,coln from tbname group by col1,col2,...,coln ) where rownum<10
为什么不用select * from tabname where rownum<n order by fieldname的方式呢?
To: doulikeme(来去无踪) 你那个查询只是巧合而已,你试着查一个你不是按顺序录入的数据,之后再按顺序排序就不OK了。巧合?? 这个可是实际系统的数据啊!! 我选了1000行也是这个结果!
为什么不用select * from tabname where rownum<n order by fieldname的方式呢?在8i里没问题的,可问题是这位兄弟可能是8i以下
我的是oracle 7忘说了,我刚才执行你们那些SQL都报错。即便查出来结果也不对,因为rownum是按数据录入数据库的顺序生成不变的了。what can I do…………
你可以试试 select rownum,fieldname1,fieldname2 from tablename order by fieldname2.
这时如果按你们所说的那种SQL查询fieldname2的前N名,查出来是不对的了。
-------------------- ----------
000000000000000 1
000002320801502 2
010215791108632 3
110101580427305 4
110102630529005 5
110105771112841 6
110108680913191 7
110226710714284 8
110403811030558 9
111111111111111 10
111111111111117 11
120103640929074 12
120104690410685 13
120104691023581 14
120104700606581 15
120104720523633 16
120104761208585 17
120104770129581 18
130202530124152 19
已写入文件 afiedt.buf 1 select lffh,id from
2 (
3 select rownum no,lffh,id from
4 (
5 select lffh,id from jbxx
6 group by lffh,id
7 )
8 )
9 where rownum<10
10* order by no
SQL> /LFFH ID
--------------------- ---------------------
121 111
122 111
12345 12345
123456 123456
2 a
511822111200304300074 20030001
511822111200308110072 20030179
511822111200401060010 20040007
511822111200401310082 20040014已选择9行。
(
select col1,col2,...,coln from tbname
group by col1,col2,...,coln
)
where rownum<10
SQL> select rownum,vat_no,perc from vat order by perc; ROWNUM VAT_NO PERC
--------- --------- ---------
4 0 0
7 6 1
8 7 3
3 3 4
6 5 5
5 4 6
9 8 8
10 9 9
2 2 13
1 1 17
SQL> select perc,rownum from vat where rownum<4 order by perc; PERC ROWNUM
--------- ---------
4 3
13 2
17 1
(
select col1 from t1
group by col1
)
where rownum<10;
也可以用MSN把东西给我 [email protected]
2 (select * from orderb where qty<121 order by qty desc)
3 where rownum<10;OR_NO INVMAST_NO QTY INV_PRIC INV_DISC PRICE IN_QTY
----------- ------------- ---------- ---------- ---------- ---------- ----------
NOTE INV_GQTY
------------------------------------------------------------ ----------
M2004050011 2010100000181 120 60 100 60 0
0
M2004050012 2010300000745 120 42 100 42 0
0
M2004070007 2010100000181 100 60 100 60 0
0OR_NO INVMAST_NO QTY INV_PRIC INV_DISC PRICE IN_QTY
----------- ------------- ---------- ---------- ---------- ---------- ----------
NOTE INV_GQTY
------------------------------------------------------------ ----------
M2004040025 2010100000181 24 60 100 60 24
0
M2004040025 2010100000198 24 60 100 60 24
0
M2004050025 2010100000181 20 60 100 60 0
AAAAAAAAAA 0
OR_NO INVMAST_NO QTY INV_PRIC INV_DISC PRICE IN_QTY
----------- ------------- ---------- ---------- ---------- ---------- ----------
NOTE INV_GQTY
------------------------------------------------------------ ----------
M2004080003 2010100000204 20 60 100 60 20
0M2004050005 2010100000181 16 120 100 120 15
0M2004070003 2010300002169 14 58 100 58 0
0
選取了 9 列
我的是9i的,可以的啊.