Order By clause returning different result when querying each time

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Order By clause returning different result when querying each time



I have a table Exc with following columns in it.


TIME DATE
CODE CHAR(9 BYTE)
VALUE NUMBER(5,2)



There was no sequence number or primary key in the table.
Since there is no unique ID in the table, I have written the following query to generate the unique number for each row which is needed to identify each row as unique.


select time, code, value
from (select time, code, value, ROW_NUMBER() over (order by time) R from EXC) where R > :x;



x - variable to hold the last maximum row number.



But when I run the above query for the second time, the order by index time is returning in a different order for the rows with same time. I need to have a consistent order by from the Oracle query.



Kindly help me on this issue.




2 Answers
2



a different order for the rows with same time



If time isn't unique then you need to decide how to break ties. We can't see your data but this should at least be consistent:


time


select time,code,value, ROW_NUMBER() over (order by time, code, value) R
from EXC;



It seems odd that you are generating the R value in the inner query but then not using it in the outer query. If you only want to use it for ordering the results then you could do:


R


select time, code, value
from (
select time, code, value, ROW_NUMBER() over (order by time, code, value) R
from EXC
)
order by r;



or just:


select time, code, value
from EXC
order by time, code, value;



If you do actually want to see the R value too then you don't need a subquery:


R


select time, code, value, ROW_NUMBER() over (order by time, code, value) R
from EXC
order by r;



or even using then Oracle-only rownum pseudocolumn:


rownum


select time, code, value, rownum R
from EXC
order by order by time, code, value;



I need the R value to fetch the data next time by using ...



You can combine this with a filter, pretty much as you showed:


select time, code, value
from (
select time, code, value, ROW_NUMBER() over (order by time, code, value) R
from EXC
)
where r > :x
order by r;



But if you're mostly using this to look for recent data you might be better off recording the time and filtering directly on that:


select time, code, value
from EXC
where time > :x
order by ...



taking care how you maintain that variable of course.





Table will be updated frequently so i need to query every 10 seconds . So I need the R value to fetch the data next time by using the following query. I will be using query like select time,code,value from ( select time,code,value, ROW_NUMBER() over (order by time,code) R from EXC ) where R > :X; i will use the previous last R value in this query. So in order to query like this i should rely on a consistent order by data.
– KMN
Aug 8 at 8:58






Yes i can use the below query select time, code, value from EXC where time > :x order by ... but the problem is suppose we are fetching the rows and query completed and that time one more row with same time being inserted in the table we might miss that row because we are going to fetch greater than previous time in our query for the next time.
– KMN
Aug 8 at 12:51




Try this:


SELECT time, code, VALUE
FROM (SELECT time, code, VALUE, ROW_NUMBER () OVER (ORDER BY time) R FROM EXC)
ORDER BY r ASC;



It orders output by your rownum.





thanks now more than a time querying with above query i am able to see consistent ordering of data. But I need to put a where condition on R to check the R > previous fetched data.How to do that. Can you please suggest on that. For example Initially when i query there was 10 rows in it.I will have that 10 as variable X and i will query next time for rows having rownum greater than 10 in my query using the variable which i had earlier.
– KMN
Aug 8 at 9:07






I have tried the following query and it seems returning consistent order. Please suggest if any issues in it. select time,code,value from ( select time,code,value, ROW_NUMBER() over (order by time,code,value) R from EXC ) where R > :X order by R;
– KMN
Aug 8 at 9:35





@KMN if you know the exact value of your 'X' variable, then it is fine. You just have to make sure that 'X' always has needed value.
– Goran Kutlaca
Aug 8 at 9:56





yeah I can make sure that. My only concern is that data retrieval order should not change at any point of time.
– KMN
Aug 8 at 10:02





We have already ordered it by time,value and code and why in the outer query we are orderding it by rownum. Little curious to know how it's actually working.Because without order by rownum the rows order is not consistent. Please explain it.
– KMN
Aug 8 at 12:47






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard