ROWNUM的有关问题
ROWNUM的问题
SQL如下:
select tab_.*, rownum rownum_
from (SELECT ta.platform_name, NVL(a.sum, 0) AS sum
FROM LCOP_PLATFORM_TAB ta
LEFT JOIN (SELECT t.platform_id, count(1) AS sum
FROM LCOP_USER_TEST t
where SUBSTR(t.timestamp, 1, 6) < =
SUBSTR(to_char(to_date('2012-05-12', 'yyyy-mm-dd'),
'yyyymmdd'),
1,
6)
GROUP BY t.platform_id) a ON a.platform_id =
ta.platform_id
ORDER BY a.sum desc) tab_
where rownum <= 10
执行
SELECT ta.platform_name, NVL(a.sum, 0) AS sum
FROM LCOP_PLATFORM_TAB ta
LEFT JOIN (SELECT t.platform_id, count(1) AS sum
FROM LCOP_USER_TEST t
where SUBSTR(t.timestamp, 1, 6) < =
SUBSTR(to_char(to_date('2012-05-12', 'yyyy-mm-dd'),
'yyyymmdd'),
1,
6)
GROUP BY t.platform_id) a ON a.platform_id =
ta.platform_id
ORDER BY a.sum desc
结果是
1
2
3
4
5
6
7
8
9
10
11
但执行全部SQL 结果却是
1
2
3
5
6
7
8
9
10
11
可以看出这里中间的4丢了 怎么回事啊
------解决方案--------------------
rownum不是按排序后的顺序取的 是按系统顺序 如果你先排序 后取10行就可以了
------解决方案--------------------
你列的顺序是 1 、2、3、4
但是系统中存储的 1 、3、2、4
查询默认按照 系统中的存储位置来排序(rowid/extent_id/partition_position)等。
select * from tableName 可能也是 1、3、2、4
你可以把 rowid也一起查出来看下顺序:
select t.*,rowid from tableName t
------解决方案--------------------
SQL如下:
select tab_.*, rownum rownum_
from (SELECT ta.platform_name, NVL(a.sum, 0) AS sum
FROM LCOP_PLATFORM_TAB ta
LEFT JOIN (SELECT t.platform_id, count(1) AS sum
FROM LCOP_USER_TEST t
where SUBSTR(t.timestamp, 1, 6) < =
SUBSTR(to_char(to_date('2012-05-12', 'yyyy-mm-dd'),
'yyyymmdd'),
1,
6)
GROUP BY t.platform_id) a ON a.platform_id =
ta.platform_id
ORDER BY a.sum desc) tab_
where rownum <= 10
执行
SELECT ta.platform_name, NVL(a.sum, 0) AS sum
FROM LCOP_PLATFORM_TAB ta
LEFT JOIN (SELECT t.platform_id, count(1) AS sum
FROM LCOP_USER_TEST t
where SUBSTR(t.timestamp, 1, 6) < =
SUBSTR(to_char(to_date('2012-05-12', 'yyyy-mm-dd'),
'yyyymmdd'),
1,
6)
GROUP BY t.platform_id) a ON a.platform_id =
ta.platform_id
ORDER BY a.sum desc
结果是
1
2
3
4
5
6
7
8
9
10
11
但执行全部SQL 结果却是
1
2
3
5
6
7
8
9
10
11
可以看出这里中间的4丢了 怎么回事啊
------解决方案--------------------
rownum不是按排序后的顺序取的 是按系统顺序 如果你先排序 后取10行就可以了
------解决方案--------------------
你列的顺序是 1 、2、3、4
但是系统中存储的 1 、3、2、4
查询默认按照 系统中的存储位置来排序(rowid/extent_id/partition_position)等。
select * from tableName 可能也是 1、3、2、4
你可以把 rowid也一起查出来看下顺序:
select t.*,rowid from tableName t
------解决方案--------------------