Selecting Random Rows in SQL
Suppose i have 10 rows in a table. I need to select 4th and 10th row.. How can I do that using select statement??
Thank you in advance!!
965373 wrote:
Suppose i have 10 rows in a table. I need to select 4th and 10th row.. How can I do that using select statement??
Thank you in advance!!How do you define which is the 4th row and which is the 10th row? Do you have a column to do that?
Let say you have a column LASTUPDATED of data type TIMESTAMP. And you want to get the 4th and 10th row based on the column LASTUPDATED. Then you can do this
select *
from (select t.*, row_number() over(order by LASTUPDATED) rno from t)
where rno = 4 or rno = 10
Similar Messages
-
Select Random Rows in PL/SQL
I would like to know if anyone has a suggestion fora select statement that would select random rows from a table based on the following:
The table contains 1-to-many categories. From each of these categories I need to select a 'x' amount of rows based upon the category id, with a total of 25 rows being selected overall. Some categories will have only 1 row selected, some will have more than 1. The rows selected contain questions that are either multiple choice (type=1) or true/false (type=2). The total rows selected (25) cannot contain more than 20% of true/false questions. Anyone have a solution for a select statement? Thanks.Not having a database at hand. To be treated as a template
with
parameters as
(select all_rows,
all_categories,
:all_questions all_questions, -- your 25
:category_questions category_questions, -- your 'x'
ceil(all_categories / :category_questions) pick_categories,
:type_2_percentage type_2_percentage -- your 20% expressed as 0.2
from (select count(*) all_rows,
count(distinct category) all_categories,
from your_table
categories_scrambled as
(select category,dense_rank() over (order by categories_mix) category_choice
from (select category,
ceil(dbms_random.value(0,1000)) categories_mix
from your_table
group by category
rows_scrambled as
(select category,question_type,question_text,dense_rank() over (order by rows_mix) row_choice
from (select category,question_type,question_text,
ceil(dbms_random.value(0,10000)) rows_mix
from your_table
combination as
(select r.category,r.question_type,r.question_text,r.row_choice,c.category_choice,
row_number() over (partition by r.row_choice,r.question_type order by r.row_choice) row_mark
from rows_scrambled r,categories_scrambled c
where r.category = c.category
type_2_questions as
(select question_text,question_type,category
from combination
where category_choice <= (select pick_categories from parameters)
and row_mark <= floor(:all_questions * :type_2_percentage / (select pick_categories from parameters))
and question_type = 2
type_1_questions as
(select question_text,question_type,category,row_number() over () the_count
from combination
where category_choice <= (select pick_categories from parameters)
and row_mark <= ceil((:all_questions - (select count(*) from type_2_questions)) / (select pick_categories from parameters))
and question_type = 1
select question_text,question_type,category
from type_2_questions
union all
select question_text,question_type,category
from type_1_questions
where the_count <= :all_questions - (select count(*) from type_2_questions)Regards
Etbin -
How to efficiently select random rows from a large table ?
Hello,
The following code will select 5 rows out of a random set of rows from the emp (employee) table
select *
from (
select ename, job
from emp
order by dbms_random.value()
where rownum <= 5my concern is that the inner select will cause a table scan in order to assign a random value to each row. This code when used against a large table can be a performance problem.
Is there an efficient way of selecting random rows from a table without having to do a table scan ? (I am new to Oracle, therefore it is possible that I am missing a very simple way to perform this task.)
thank you for your help,
John.
Edited by: 440bx on Jul 10, 2010 6:18 PMHave a look at the SAMPLE clause of the select statement. The number in parenthesis is a percentage of the table.
SQL> create table t as select * from dba_objects;
Table created.
SQL> explain plan for select * from t sample (1);
Explained.
SQL> @xp
PLAN_TABLE_OUTPUT
Plan hash value: 2767392432
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 725 | 70325 | 289 (1)| 00:00:04 |
| 1 | TABLE ACCESS SAMPLE| T | 725 | 70325 | 289 (1)| 00:00:04 |
8 rows selected. -
Update random rows in sql table
Do not ask me why but I have to do it and I would greatly appreciate any help.
Let us say that I want to update column foo_column of a table foo. The selection criteria is foo_selection_criteria. So my sql to update would be something like:
update foo set foo_column='some value' where <foo_selection_criteria>
Foo_selection_criteria can be any logical expression based on the value of columns in table foo. For example foo_selection_criteria could be foo.columna != '1';
There are m rows in the table that meet the foo_selection_criteria. I want to update only randomly selected 1/n (eg., 0.5 , 083 etc) of these m rows.
I tried using dbms_random.value procedure to update my sql above so that it reads something like
update foo set foo_column='some value' where <foo_selection_criteria> and dbms_random.value <= 1/n;
However it seems that dbms_random is evaluated only once in the update sql above. Is there any way I can avoid that dbms_value is calculated once and stick in the sql above ? Or is someone aware of another way to randomly update a random fraction of m rows that meed an update selection criteria ?
I would be grateful for any help/Tubby wrote:
Why not just
ROWNUM = 1
that's random....I disagree. Rownum = 1 will in general return the first inserted row into a table.
There is no guarantee that it is the first inserted row, but in 99% of all cases it will be so ( i just made this statistic up, but prove me to be wrong ;)) .
And an 99% chance is not very random for me. -
How to find Random Row using SQL
In Scott.emp table (oracle default) there are fourteen rows
I want to select employee with salary at second (eg if 2000, 3000, 5000, 2500 )
then second no salary is 3000.?Re: how we select 3 rd max salary from given salaries
But.....
How the title is related to the topic ? -
Select a fixed number of random rows
Hi,
I just would like to select a fixed number of random rows from a table. That is, I would like to get 5 rows, without specifying the row_id, and the selected rows should "always" be different. Just as picking 5 balls out of a big box (which is filled with many balls).
Is there a way to easily put it into one sql statement or do I have to write a workaround in the programming language I use?
Thanks a lot in advance,
CQAskTom has a discussion on this:
http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:6075151195522
Note that it requires that you
1) Generate a random number for every row in the table
2) Sort the table by those random numbers
which is a significant performance hit if the table is large.
Justin -
How to select a random row with for update?
Hi,
I have a package that needs to assign a random, reusable number (ID) that is not currently being used - to a procedure.
I'm trying to simulate a pool of numbers (IDs) using a table that has an ID and IS_USED columns.
How do I do:
select a random ID (random row)
from pool_table
where IS_USED is 0 (not taken)
FOR UPDATEThe for update is to lock the row from being taken by another process calling the package.
OR:
Can I simulate a pool of numbers with a different object (not a table)?
I need the numbers to be coherent between sessions (thus package variables will not work) and only one session uses the same ID at any given time. When it finishes the number becomes available for further runs.
Thanks.
Edited by: Pyrocks on Nov 7, 2010 10:45 AMThis works on Oracle 11g (probably on 10g too, but I haven't tested):
CREATE OR REPLACE PACKAGE REUSABLE_RANDOM_NUMBERS
IS
FUNCTION GET_NUMBER RETURN NUMBER;
END REUSABLE_RANDOM_NUMBERS;
create or replace
PACKAGE BODY REUSABLE_RANDOM_NUMBERS
IS
TYPE NUM_TABLE_TYPE IS TABLE OF CHAR INDEX BY PLS_INTEGER;
MIN_VALUE CONSTANT PLS_INTEGER := 0;
max_value CONSTANT PLS_INTEGER := 10;
NUM_TABLE NUM_TABLE_TYPE;
FUNCTION GET_NUMBER RETURN NUMBER
IS
num PLS_INTEGER;
BEGIN
FOR I IN 1 .. 100 LOOP
NUM := DBMS_RANDOM.VALUE( min_value, max_value );
IF NOT NUM_TABLE.EXISTS( NUM ) THEN
NUM_TABLE( NUM ) := 'X';
RETURN NUM;
END IF;
END LOOP;
FOR I IN MIN_VALUE .. MAX_VALUE LOOP
IF NOT NUM_TABLE.EXISTS( i ) THEN
NUM_TABLE( i ) := 'X';
RETURN i;
END IF;
END LOOP;
RAISE_APPLICATION_ERROR( -20991, 'All possible values have ben used, cannot assign a new one' );
END;
END REUSABLE_RANDOM_NUMBERS;
SELECT REUSABLE_RANDOM_NUMBERS.GET_NUMBER
FROM DUAL
connect by level <= 11;
GET_NUMBER
3
4
6
2
1
7
8
0
9
5
10
11 rows selected
SELECT REUSABLE_RANDOM_NUMBERS.GET_NUMBER
FROM DUAL;
Error starting at line 44 in command:
SELECT REUSABLE_RANDOM_NUMBERS.GET_NUMBER
FROM DUAL
Error report:
SQL Error: ORA-20991: All possible values have ben used, cannot assign a new one
ORA-06512: przy "TEST.REUSABLE_RANDOM_NUMBERS", linia 26 -
Newbie question: Select one row from table in PL/SQL
Hi,
I want to select one row from the table Employee where Emplyoyee Number is say 200. This is a simple SQL query, but I don't know the equivalent PL/SQL format. I will have 3 out params here - Id itself, Name, Salary. I will then have to populate a java resultset object from these out params.
Later, I'll have to use cursors to retrieve more than one row.
Thanks for any help.Perhaps something like
CREATE OR REPLACE PROCEDURE get_employee( l_id IN OUT employee.id%TYPE,
l_name OUT employee.name%TYPE,
l_salary OUT employee.salary%TYPE )
AS
BEGIN
SELECT name, salary
INTO l_name, l_salary
FROM employee
WHERE id = l_id;
END;Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
Select extra row without using UNION ALL in pl/sql
Hi,
Can anyone tell me how to select extra row without using UNION or UNION ALL in pl/sql. Actually I want to have my o/p of query as partitioned by designation and ordered by salary and than one extra row which will contain the highest salary in a particular salary. My table has first_name,emp_id,designation and salary column. And I wnt the o/p as.
Mohinish,12212,SI,46000
Ram,11212,SSI,47000
Shyam,12133,SI,48000
Rick,9898,SI,46000
Rocky,12312,SSI,56000
Sariq,23948,SI,43000
Suman,12789,HR,49000
Sampy,12780,SI,46000
Parna,11111,HR,50000
Now the o/p should be.
Mohinish,12212,SI,46000
Rick,9898,SI,46000
Sariq,23948,SI,43000
Shyam,12133,SI,48000
Shyam,12133,SI,48000
Ram,11212,SSI,47000
Rocky,12312,SSI,56000
Rocky,12312,SSI,56000
Suman,12789,HR,49000
Parna,11111,HR,50000
Parna,11111,HR,50000
Thanks in AdvanceYou don't have to do a UNION or UNION ALL in PL/SQL but you would need to in SQL to get the desired output:
with data_recs
as (select 'Mohinish' first_name,12212 emp_id,'SI' designation,46000 salary from dual union
select 'Ram',11212,'SSI',47000 from dual union
select 'Shyam',12133,'SI',48000 from dual union
select 'Rick',9898,'SI',46000 from dual union
select 'Rocky',12312,'SSI',56000 from dual union
select 'Sariq',23948,'SI',43000 from dual union
select 'Suman',12789,'HR',49000 from dual union
select 'Sampy',12780,'SI',46000 from dual union
select 'Parna',11111,'HR',50000 from dual)
select first_name, emp_id, designation, salary from data_recs union all
select s.first_name, s.emp_id, s.designation, s.salary
from (select first_name,
emp_id,
designation,
salary,
row_number() over (partition by designation order by salary desc) high_salary
from data_recs
order by designation, salary) s
where s.high_salary = 1
order by designation, salary;
FIRST_NAME EMP_ID DESIGNATION SALARY
Suman 12789 HR 49000
Parna 11111 HR 50000
Parna 11111 HR 50000
Sariq 23948 SI 43000
Rick 9898 SI 46000
Mohinish 12212 SI 46000
Sampy 12780 SI 46000
Shyam 12133 SI 48000
Shyam 12133 SI 48000
Ram 11212 SSI 47000
Rocky 12312 SSI 56000
Rocky 12312 SSI 56000 -
Randomly selecting some rows from the database table
Hi can some one help me in selecting some rows from a database table which has around 90,000 rows.
Thanks.One thing you might try is the "sample" clause if you have 8i which is supposed to return a random percentage of the table. Say for example, you have a sequence number on your table as the pkey. Then you might try:
select * from <table_name> where pkey in(select pkey from <table_name> sample(10));
This should give you a random 10 percent of the rows in the table but I tried this once and the results seemed unpredictable. For example it returned a different number of rows each time even though the number of rows in the table didn't change.
Hope this works for you. -
For example, I have a 2D array with 46 rows and 400 columns. I would like to randomly select 46 data rows from the 2D array. By doing the random selection it means that not all individual 46 rows will be selected some rows may appear more than once as there may be some duplicates or triplicates in the random selection. The importan thing is that we will have randomly selected 46 rows of data (no matter that some rows appear more than once). Then I would like to subset these randomly selected 46 data rows (some which will be duplicated, or triplicated, etc.) and then also find and subset the rows that were not selected. Does this make sense? Then i would like to do this say 10 times for this data set. So that then I will have 2 by 10 data sets: the first 10 each with 46 rows and the other 10 with n rows depending on how many WERE NOT randomly selected. i hope that my explanation is clear. I am relatively new to Labview. It is really great so I am getting better! If anyone can help me with this problems it will be great. RVR
Start by generating randon #s between 0 and 45. Run a for loop X times and in it use the random function, multiply the result by X and round down (-infinity). You can make this into a subVI, which you can reuse later. In the same loop, or in a different one, use Index Array to extract the rows which were selected (wiring the result out of the loop with auto indexing causes it to be rebuilt into a 2D array).
One possible solution for the second part would be to go over the array of randomly generated numbers in a for loop and use Search 1D Array to find each of the numbers (i). If you get -1, it means the row wasn't selected and you can extract it.
I hope this puts you on the right path. If not, don't be afraid to ask more.
To learn more about LV, I suggest you read the LabVIEW user manual. Also, try searching this site and google for LabVIEW tutorials. Here and here are a couple you can start with. You can also contact your local NI office and join one of their courses.
In addition, I suggest you read the LabVIEW style guide.
Try to take over the world! -
Returning Random Row based on Subset of Data within Table
Hi,
Please see below. Running SQL Server 2008 R2.
Sample DDL:
CREATE TABLE [dbo].[TestPersons]
[TestPersonID] [int] NOT NULL IDENTITY(1,1),
[FirstName] [varchar](50) NULL,
[LastName] [varchar](50) NULL,
[AreaID] [varchar](50) NULL,
CONSTRAINT [PK_TestPersons_TestPersonID] PRIMARY KEY CLUSTERED ([TestPersonID] 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
Sample Data:
INSERT INTO
[dbo].[TestPersons] ([FirstName], [LastName], [AreaID])
VALUES
('Carlos', 'Matlock', 'A0009'),
('William', 'Rivas', 'A0001'),
('Kathryn', 'Rice', 'A0008'),
('John', 'Ball', 'A0009'),
('Robert', 'Barnhill', 'A0009'),
('Timothy', 'Stein', 'A0009'),
('Christopher', 'Smith', 'A0001'),
('Brian', 'Speakman', 'A0001'),
('Harold', 'Clark', 'A0009'),
('Tim', 'Henson', 'A0009'),
('Victor', 'Chilson', 'A0009')
The above insert statement is a small example of the data contained in this table. Normally the table would contain several thousand rows. We use the following query to replace actual data with random rows from our test table:
UPDATE
[P]
SET
[P].[FirstName] = [T].[FirstName],
[P].[LastName] = [T].[LastName],
[P].[AreaID] = [T].[AreaID]
FROM
[dbo].[Persons] [P]
INNER LOOP JOIN
[dbo].[TestPersons] [T] ON ([T].[TestPersonID] = (1 + ABS(CRYPT_GEN_RANDOM(8)%5000)))
This query works as it selects a random row from the entire set of data in the table. However there are cases where we need to specify a restricted subset to randomize from. For example, we may need to randomize data only for Persons with an AreaID
of A0001 or A0008. So in that case, and using the sample data above, we would want the randomization to only select from rows in TestPersons that have an AreaID of A0001 or A0008. How would I go about accomplishing this? I've tried
adding a WHERE clause but it seems it's ignored because of the INNER LOOP JOIN. I've also tried including [P].[AreaID] = [T].[AreaID] in the join hint but to no avail.
I also realize having sample data with only the set that we need would solve this problem but for our needs we need a large test set as our randomization requirements depend on the situation.
Any assistance is greatly appreciated!
Best Regards
BradDECLARE @TestPersons TABLE (TestPersonID int NOT NULL IDENTITY(1,1), FirstName varchar(50), LastName varchar(50), AreaID varchar(50))
INSERT INTO @TestPersons (FirstName, LastName, AreaID)
VALUES ('Carlos', 'Matlock', 'A0009'), ('William', 'Rivas', 'A0001'), ('Kathryn', 'Rice', 'A0008'), ('John', 'Ball', 'A0009'), ('Robert', 'Barnhill', 'A0009'), ('Timothy', 'Stein', 'A0009'),
('Christopher', 'Smith', 'A0001'), ('Brian', 'Speakman', 'A0001'), ('Harold', 'Clark', 'A0009'), ('Tim', 'Henson', 'A0009'), ('Victor', 'Chilson', 'A0009')
;WITH subset AS (
SELECT ROW_NUMBER() OVER (ORDER BY TestPersonID) AS sID, *
FROM @TestPersons
WHERE FirstName LIKE '%e%'
SELECT *
FROM subset
WHERE sID = round((((SELECT COUNT(*) FROM subset) - 1 - 1)*rand())+1,0)
This will grab a random row from a subset (defined in the CTE). -
Delete all rows except 10 random rows
Hi,
how can I delete all rows from table JOBS except 10 random rows?
Someone asked it before (not here..): http://stackoverflow.com/questions/10820105/t-sql-delete-except-top-1
but I didn't understand the answer, and I don't think it will work in PL/SQL.
If the answer in StackOverflow does works in PL/SQL, I will glad if someone explains me with better example,
If the answer in StackOverflow does'nt work in PL/SQL, I will glad if someone gives me an example.
thanks!Actually I found a problem in this solution.
delete from jobs where rowid not in (select rowid from jobs where rownum<=10) ;I used the above code to delete all rows except 10 in JOB_HISTORY table, but for some reason the rows that were chosen were only in the range in which (144<=EMPLOYEE_ID<=146).
I tried this several times and always i stayed with rows in this range.
Here are the queries:
SQL> select employee_id from job_history;
EMPLOYEE_ID
144
144
144
144
144
145
145
146
146
146
146
146
146
146
147
147
147
147
149
149
149
156
156
156
156
156
156
156
158
158
158
158
158
158
158
158
159
159
159
165
165
165
171
171
171
171
46 rows selected
SQL> delete from job_history where rowid not in (select rowid from job_history where rownum <= 15);
31 rows deleted
SQL> select employee_id from job_history;
EMPLOYEE_ID
144
144
144
144
144
145
145
146
146
146
146
146
146
146
147
15 rows selected
SQL>
Glad if someone can explain me why I get this result.
Thanks
Edited by: 998093 on 05:01 05/04/2013 -
Dear all,
I have a table containing 50 rows and I would like to select only rows between 20 and 30. Can you help me to do this with only one select statement or whatever you suggest. Also is it possible to do it without using rownum
Thanks
SLI do want want any specific criteriaSo in fact you should have called this thread Select unspecific rows, hmmm?
If you're really not bothered about the precise number of rows then the SAMPLE cause is a very performant way of getting a random subset of rows...
SQL> SET TIMING ON
SQL> SELECT count(*) FROM big_t
2 /
COUNT(*)
31764
Elapsed: 00:00:01.04
SQL> SELECT count(*) FROM
2 ( SELECT * FROM big_t SAMPLE (0.1) )
3 /
COUNT(*)
33
Elapsed: 00:00:00.03
SQL> SELECT count(*) FROM
2 ( SELECT * FROM big_t SAMPLE (0.1) )
3 /
COUNT(*)
32
Elapsed: 00:00:00.01
SQL> Cheers, APC -
How to get the last transaction in a row in SQL Developer?
What syntax would I use to get the last transaction of a row in SQL developer?
The way I have my query set-up currently it is not returning the correct data, here is my current syntax:
select ssn, max(tran_id), chng_type,tran_id
from pda_tran
where ssn = 'xxx-xxx-0011'
and chng_type = 'C'
group by ssn, chng_type,tran_id;
It returns a 'C' chng_type but it is not the last one. when I query on this ssn this is what I get:
ssn tran_id chng_type
xxx-xxx-0011 001 A
xxx-xxx-0011 002 E
xxx-xxx-0011 003 C
xxx-xxx-0011 004 S
xxx-xxx-0011 005 C
xxx-xxx-0011 006 T
I only want to return the ssn's with a last transaction chng_type of 'C'. How can I get the correct information returned. Please advise.From what I see and read... there is one to many group by
You wrote
select ssn, max(tran_id), chng_type,tran_id
from pda_tran
where ssn = 'xxx-xxx-0011'
and chng_type = 'C'
group by ssn, chng_type,tran_id;
If you want the max(tran_id), remove it from the "group by"
select ssn, chng_type, max(tran_id)
FROM
(SELECT 'xxx-xxx-0011' ssn, '001' tran_id, 'A' chng_type FROM DUAL UNION
SELECT 'xxx-xxx-0011' ssn, '002' tran_id, 'E' chng_type FROM DUAL UNION
SELECT 'xxx-xxx-0011' ssn, '003' tran_id, 'C' chng_type FROM DUAL UNION
SELECT 'xxx-xxx-0011' ssn, '004' tran_id, 'S' chng_type FROM DUAL UNION
SELECT 'xxx-xxx-0011' ssn, '005' tran_id, 'C' chng_type FROM DUAL UNION
SELECT 'xxx-xxx-0011' ssn, '006'tran_id, 'T' chng_type FROM DUAL )
where ssn = 'xxx-xxx-0011'
and chng_type = 'C'
group by ssn, chng_type;
Maybe you are looking for
-
Hi Gurus, Got a question on sales scheduling agreements schedule lines. Our customer sends 10 weeks of forecast delivery schedules and 2 weeks of JIT deliveries. which one of these two are passed to MD04? Assuming only forecast delivery schedules are
-
How to remove tab stops at one shot after importing a word document
HI everyone, This is my first post in Framemaker Community as I am moving from RH to FM. I have an issue with respect to importing word documents to Framemaker. After I have import a word to doc to FM multiple tab stops are created. Now, although I r
-
How do you change the date/currency format for every user in OSX 10.6?
I imaged a bunch of mac labs at work only to realize the base image has the date/currency set for United States rather then United Kingdom. The regional settings are correct. I tried changing it on the local admin account but it does not affect a new
-
Getting an error when i am execution an BI query
Hi Expert, I am getting an error when i am execution an BI query using ABAP. Its Giving me this Error "The Info Provider properties for GHRGPDM12 are not the same as the system default" and in the error analysis it saying as bellow. Property Data I
-
I'm having a problem upgrading the new version of Flash Player.
Having a problem just UPGRADING my computer to the latest Flash Player. Had no problems doing it in the past, but now when I go through the steps, my computer fails to save the download. Then I can't run it. We have Windows XP. What do I need to do?