How to select random rows from result set in oracle [duplicate] - sql

This question already has answers here:
How to get records randomly from the oracle database?
(9 answers)
Closed 8 years ago.
I tried both these queries but none of them seem to work: I wish to fetch random rows from result set in oracle
select * from TABLE_NAME t where t.ent_application_id = 13101192 order
by Dbms_Random.Random
select * from TABLE_NAME t where t.ent_application_id = 13101192 order
by Dbms_Random.Value

Try
SELECT T.COLUMN1, T.COLUMN2, DBMS_RANDOM.VALUE
FROM TABLE_NAME T
WHERE T.ENT_APPLICATION_ID = 13101192
ORDER BY 3

Related

Select nth row Oracle SQL [duplicate]

This question already has answers here:
How ROWNUM works in pagination query?
(3 answers)
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
how to select even records from a table in oracle?
(12 answers)
Closed 2 years ago.
I'm attempting to access the 3rd row from the following query:
SELECT *
FROM (SELECT
OFFENSEDESC,
COUNT(*) AS num
FROM
CRIMEPROFILE
GROUP BY
OFFENSEDESC
ORDER BY num DESC) o
WHERE rownum = 3
However, it returns no rows, I am uncertain as to why that is, any help is appreciated. NOTE: ORACLE SQL
Try this
SELCT
OFFENSEDESC,
num
from
(
SELECT
OFFENSEDESC,
COUNT(*) AS num,
ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) as rnk
FROM
CRIMEPROFILE
GROUP BY
OFFENSEDESC
) val
WHERE rnk = 3

SQL - GET VALUE USING MAX [duplicate]

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 4 years ago.
I am trying to get the AMOUNT_APP WHERE DATE _APP is the greatest value.
I wrote this query, but I am having troubles
SELECT AMOUNT_APP
FROM Payments_App
WHERE DATE_APP = MAX(DATE_APP)
I expect to get 5234.34
My Table
Use order by and rownum:
select pa.amount_app
from (select pa.*
from payments_app
order by date_app desc
) pa
where rownum = 1;
Or keep can be quite efficient:
select max(pa.amount_app) keep (dense_rank first order by date_app desc)
from payments_app;
If we suppouse that DATE_APP is an attribute that you could find the most recently updated results.
select AMOUNT_APP FROM Payments_App WHERE DATE_APP = (Select
MAX(DATE_APP) FROM Payments_App) ;

Behaviour of rownum in Oracle [duplicate]

This question already has answers here:
How ROWNUM works in pagination query?
(3 answers)
Closed 7 years ago.
I observed a strange behaviour with ROWNUM in Oracle with static tables. I am trying pagination with query like below
select * from (
select the_data,rownum as seqn from the_table
) where seqn <= somenumber and seqn >= othernumber
However when I execute query multiple times I observe that the rownum value returned is not unique for a row ie if a row appears at rownum 25 for the first time it appears at rownum 125 for second execution
This issue does not appear if I do a order by like,
select * from (
select the_data,rownum as seqn from the_table order by column3
) where seqn <= somenumber and seqn >= othernumber
However order by decreases performance. Is this normal or is there any other way to improve performance.
You should check oracle documents for this question. Followed links may be helpful for you http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions156.htm#SQLRF06100
One example:
SELECT * FROM ( SELECT ROW_NUMBER() OVER () AS R, T.* FROM T ) AS TR WHERE R <= 10;
T is a table name. R is a row

PL/SQL ROWNUM keyword and equality (=) [duplicate]

This question already has answers here:
How ROWNUM works in pagination query?
(3 answers)
Closed 7 years ago.
I am new in PL/SQL and I am trying the ROWNUM keyword.
If I query this:
SELECT *
FROM my_table
WHERE ROWNUM <= 1
I am getting a result. Even in this case:
SELECT *
FROM my_table
WHERE ROWNUM = 1
But if I try
SELECT *
FROM my_table
WHERE ROWNUM = 2
I get an empty result..
But the table my_tablehas more than one tuple.
Can you help me?
Thank you!
ROWNUM is a psuedo column that has a value AFTER the result set is returned. Thus you can use where rownum < 2 but you can't select where ROWNUM equals a value because it does not have a value to compare to yet.

Select records after 5 records in a table [duplicate]

This question already has answers here:
Row Offset in SQL Server
(17 answers)
Closed 8 years ago.
I want to select records after first 5 records in a table. The table is getting updated with new records. I have displayed 5 new records select top 5 * from table order by ID DESC.
Now I want to Display another 5 records somewhere else in the page, what will be the query for that?
WITH tmp AS
(SELECT ROW_NUMBER() OVER (ORDER BY a.id) AS 'rn', a.* FROM table a)
SELECT * FROM tmp WHERE rn BETWEEN 5 AND 10
SELECT *
FROM [dbo].[4]
ORDER BY [id] ASC
OFFSET 5 ROWS
FETCH NEXT 10 ROWS ONLY