Selecting a random record
I have a table holding a million records. I want to select records at random. I do not want to use the dbms_random. Any help out there
A thing I stumbled across recently is DBMS_RANDOM.VALUE(low,high) - it must be new in 8.1.7 because I can't see it in the 8.1.6 docs. For example:
SELECT DBMS_RANDOM.VALUE(48,90) FROM DUAL;
DBMS_RANDOM.VALUE(48,90)
52.0487576
On a different tack, if you want a random sample, just use the SAMPLE(percent) clause e.g:
SELECT username FROM all_users SAMPLE(1);
or even
SELECT * FROM
select username from all_users
sample(1)
order by dbms_random.value
WHERE ROWNUM = 1;
Similar Messages
-
How can I select the random records in u201Cstep loopu201D.
Hi, Experts,
I am using step loop to display the data on screen now I want to select the random records from this step loop display. Like 1st 3rd or 5th record, is there any way to select the records,
like I did in Table control there was a filed in internal table named marked char length 1, I gave this field name in u201Cw/ SelColumnu201D it fill that field with u2018Xu2019 from where I can get the info about the selected records, in this way I can easily perform the operation on that internal table with the help of that marked field.
Is there any way to select the records in step loop too ?
Kind Regards,
Faisalthanks for replay shwetali,
but i just gave you example of random records with 1st 3rd and 5th my goal is not select only these records. i want to select the random mean any records from the step loop display.
like we can select from the table control. and when select any record it place 'X' in the given internal table field.
Thanks and kind Regards,
Faisal -
<p>I am trying to select ten random records from all that match the report selection criteria then report on each of these random records for QA/QI. I have tried the RND function however it is giving me a random number rather than a random record selection. I cannot figure this out and am despirately seeking assistance.</p><p>Thank you,</p><p>Amy</p>
<p>I don't know of any Random record selection functions but maybe you could write your own custom function inside of a record selection to randomly filter the records. You would use the Rand function we currently have to decide if a record was included in the report data or not.</p><p>Another possible option is to filter the records before they get to the report. You can only do this if you are pushing the data into the report instead of having the report pull the data. An example of this would be passing an ado recordset to a report at runtime. </p><p>Rob Horne</p><p>http://diamond.businessobjects.com/blog/10 </p>
-
How to select 10 random records?
Requirement is:
Show all records if query returns less than equal to 10 records.
Else
Show random 10 records.
How do I achieve that using SQL alone?
Tried with sample but that didn't work. The number of records in table continuously growing.
ThanksHi,
would tell me how it works. little bit confusing me..
SQL> select count(*) from scott.emp;
COUNT(*)
14
1* SELECT * FROM scott.emp SAMPLE (8)
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO EMAIL
7839 KING PRESIDENT 17-NOV-81 5200 8250 10
SQL>
SQL> ED
Wrote file afiedt.buf
1* SELECT * FROM scott.emp SAMPLE (7.5)
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO EMAIL
7788 SCOTT ANALYST 7566 09-DEC-82 3200 450 20
7934 MILLER CLERK 7782 23-JAN-82 1500 195 10
SQL> SELECT * FROM scott.emp SAMPLE (7.5);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO EMAIL
7902 FORD ANALYST 7566 03-DEC-81 3200 450 20Edited by: Ravi291283 on Jul 27, 2009 2:32 AM -
I have a page that I have a recordset on just calling the
RecordID in the database. How would I have the page or recordset
select a random record and put that recordID in say a session
variable?
ThanksMySQL:
SELECT yourfield FROM yourtable WHERE ... ORDER BY rand()
LIMIT 1
MSSQL:
SELECT TOP 1 yourfield FROM yourtable WHERE ... ORDER BY
newid()
MS Access:
SELECT TOP 1 yourfield FROM yourtable WHERE ... ORDER BY
RND(yourprimarykey) [warning: this may not be random through
CF]
HTH
Tim Carley
www.recfusion.com
[email protected] -
Select Random Records with Group By?
Hi,
I currently select random records by using this type of SQL:
(SELECT *
FROM ( SELECT *
FROM ( SELECT *
FROM TABLEA
ORDER BY dbms_random.value
WHERE ROWNUM <= 100
Based on the following data, I now want to still pick out random records but I only want 1 record from the same ID and START_DATE but where the FLAG could be Y or N.
So with grouping by ID and START_DATE, I need to still be able to choose the record if it has anything from 1 entry or 10 entries per ID and START_DATE.
Could this be performed with a GROUP BY?
ID START_DATE FLAG
1 12/12/2005 Y
1 12/12/2005 N
1 12/12/2005 N
1 30/12/2005 Y
1 30/12/2005 N
1 30/12/2005 N
2 10/10/2005 Y
2 07/07/2005 Y
Thanks for any help.
DanielTry this [not tested]:
SELECT *
FROM
(SELECT *
FROM
( SELECT
ID
,START_DATE
,FLAG
,ROW_NUMBER() OVER
(PARTITION BY ID,START_DATE ORDER BY dbms_random.value) RN
FROM TABLEA
ORDER BY dbms_random.value
WHERE RN = 1
AND ROWNUM <= 100
; -
Selecting random records out of a table
Hi,
I guess this question isn't new, but I didn't found anything about this using the search of this forum.
How can I get e.g. 5 random records out of a table?
I have a small table, which stores the special offers of a shop. Let us say, there are 30 records in it. How can I fetch (e.g.) 5 random records of this table?
I tried to use this statement in a loop:
SELECT t_item_id FROM tbl_special_offers SAMPLE (1) WHERE ROWNUM = 1
But this is a very small table, so most of the query returns no data. Additionally I have to check if this number was already selected and ignore it in this case. This isn't a good solution, is it?
Can somebody help me out with a better solution? Thanks :)create or replace package shop_stuff
is
function random_number return number;
end;
create or replace package body shop_stuff
is
function random_number return number
is
begin
return dbms_random.value;
end;
begin
dbms_random.initialize (to_number(to_char(sysdate,'mmddsssss')));
end;
create table tbl_special_offers (t_item_id number);
insert into tbl_special_offers values (1);
insert into tbl_special_offers values (2);
insert into tbl_special_offers values (3);
insert into tbl_special_offers values (4);
insert into tbl_special_offers values (5);
insert into tbl_special_offers values (6);
insert into tbl_special_offers values (7);
insert into tbl_special_offers values (8);
insert into tbl_special_offers values (9);
insert into tbl_special_offers values (10);
select t_item_id
from (select t_item_id
from tbl_special_offers
order by shop_stuff.random_number)
where rownum <= 5
SQL> /
T_ITEM_ID
6
7
5
4
10
SQL> /
T_ITEM_ID
1
5
3
9
6
SQL> /
T_ITEM_ID
6
5
3
2
10
SQL> /
T_ITEM_ID
10
2
8
4
1
SQL> -
Selecting Non repeatable random records
HI all,
I have 20 records in table. I want to get a random single record from that table.And the main thing is
if i having 20 records 1st time i'm getting a random record.the upcoming execution result that does not show previous random record .
That is out of 20 records ,each time i want to select record that does not occur previously.so at 20 executions i have 20 result with non-repeatable.
If i am confusing you,sorry for this.....pls help me to get a suffled record at each time
note: i'm used NEWID() that gives random records but it occurs previously some timeI cannot reproduce this :-)
with
[10] as (select 0 as c union all select 0),
[11] as (select 0 as c from [10] as a, [10] as b),
[12] as (select 0 as c from [11] as a, [11] as b),
[13] as (select 0 as c from [12] as a, [12] as b),
nums as (select top(20) row_number() over(order by c) as n from [13]),
choice as (select top(1) n from nums order by checksum(newid()))
select * from choice order by n
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Dynamic Table with Random Records
What I am trying to do is select random records from a table
and display them in a dynamic table with max columns set to 3 and
the 4th record to be on a new row. Below is what I have right now
and it works to randomly pick records but has no function to set
columns in a table. If there is an easier way feel free to let me
know. I have tried various ways to do this but none seem to work.
<CFQUERY NAME="getItems" DATASOURCE="absi">
SELECT catfit.*, modcats.*, prodmat.*, prod.* FROM catfit,
modcats,
prodmat, prod WHERE prodmat.prodid=catfit.prodid And
catfit.catid=modcats.catid
ORDER BY modl ASC </cfquery>
<cfif getItems.recordCount>
<cfset showNum = 3>
<cfif showNum gt getItems.recordCount>
<cfset showNum = getItems.recordCount>
</cfif>
<cfset itemList = "">
<cfloop from="1" to="#getItems.recordCount#"
index="i">
<cfset itemList = ListAppend(itemList, i)>
</cfloop>
<cfset randomItems = "">
<cfset itemCount = ListLen(itemList)>
<cfloop from="1" to="#itemCount#" index="i">
<cfset random = ListGetAt(itemList, RandRange(1,
itemCount))>
<cfset randomItems = ListAppend(randomItems, random)>
<cfset itemList = ListDeleteAt(itemList,
ListFind(itemList, random))>
<cfset itemCount = ListLen(itemList)>
</cfloop>
<cfloop from="1" to="#showNum#" index="i">
<cfoutput>
<table width="205" border="0" align="left"
cellpadding="0" cellspacing="0">
<tr>
<td width="235" height="116"> <div
align="center"><img
src="../Products/ProductPictures/#getitems.pic[ListGetAt(randomItems,
i)]#" width="100"></div></td>
</tr>
<tr>
<td
class="ProdTitle">#getitems.brand[ListGetAt(randomItems,
i)]# #getitems.modl[ListGetAt(randomItems, i)]#</td>
</tr>
<tr>
<td
class="paragraph">$#getitems.prc[ListGetAt(randomItems,
i)]#</td>
</tr>
<tr>
<td><A
href="../Products/details.cfm?prodid=#getItems.prodid[ListGetAt(randomItems,
i)]#" class="linkcontact">more
info</a></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</cfoutput>
</cfloop>
</cfif>To start a new row after 3 records, do something like this.
<table>
<tr>
<cfoutput query="something">
<td>#data#<td>
<cfif currentrow mod 3 is 0>
</tr><tr>
</cfoutput>
</tr>
</table>
You should also know that your approach is very inefficient
in that you are bringing in to cold fusion more data than you need.
First of all you are selecting every field from 3 tables when you
don't appear to be using all of them. Second, you are selecting
every record and you only want to use 3. There are better ways out
there, but they are db specific and you did not say what you are
using. -
Morning all
Just a quick one.
How do I display a random record in DW?
I have a MySQL statement which is:
SELECT * FROM
tablename ORDER BY RAND() LIMIT 0,
number of records to display
The error message I get is:
'Syntax error (missing operator) in query expression RAND()
LIMIT 0'
If I alter 'RAND()' to 'RND()', I still get the error. If I
remove the '0' I still get the error.
Any ideas how to make this work?
I'm using Access BTW.
Regards
MartinTell your SQL statement what to RAND - right now it doesn't
know how to
randomize it, by what field.
"Pantyboy" <[email protected]> wrote in
message
news:ef0and$r7g$[email protected]..
> Morning all
>
> Just a quick one.
>
> How do I display a random record in DW?
>
> I have a MySQL statement which is:
>
> SELECT * FROM
tablename ORDER BY RAND() LIMIT 0,
number of
> records to
> display
>
> The error message I get is:
>
> 'Syntax error (missing operator) in query expression
RAND() LIMIT 0'
>
> If I alter 'RAND()' to 'RND()', I still get the error.
If I remove the '0'
> I
> still get the error.
>
> Any ideas how to make this work?
>
> I'm using Access BTW.
>
> Regards
>
> Martin
> -
I have a DB of sites that I need to query and pull in 5
records that are to be displayed in a random order.
I can limit the records but I'm struggling with displaying
them in a random order.
Any ideas?We're using MySQL as the DB and to clarify I need to pull in
5 random records in a random order.
I've just been emailed the answer by a friend so I'llpost it
up here.
"SELECT *
FROM tablename
WHERE somefield='something'
ORDER BY RAND() LIMIT 5"
Seems to work fine to me. -
I have a page on my site that displays certain information in
kind of a "feature" section, where it needs to query the database
based on certain criteria and then display the data for just one of
the records. The record should be different each time the page
loads. So, if my query has 10 or 20 records, I just want to select
one of them randomly and disply its data. What would be the best
way to code such a scenario?
Many thanks.first, a statement that contains "The record
should be different each time the page loads" and "select one
of them
randomly and display its data" is a bit of a contradiction:
you either
display a random record or you control which record to
display to make
sure each page load displays a different one.
if you just want a random record displayed (which means same
record may
be displayed on subsequent page loads) then how to do it
depends on
which database you are using, as cfSearching pointed out.
here's a great place to look to sql statements for this for
various db
systems:
http://www.petefreitag.com/item/466.cfm
if you want to display a different record each page load, you
need to
keep track which records have already been displayed
previously and
display the next one in sequence. you could do this by
storing the list
of record ids in an application-scope variable, displaying
the record
with the first id and removing that id from the list of ids.
when the
list is empty, query the db for record ids again and make a
list anew.
hth
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/ -
Hi Gurus,
I've been asked to write a program that accepts as input a few "filter" selection criteria and a number of records desired. The user wants a "random" selection of matching records (up to the maximum number of records requested).
Selecting the matching records is the easy part. The hard part comes when I have to return a "random" set of matching records. If they call the program twice with the same selection criteria, they don't want the same records to be returned.
Can anyone help me?
Thanks,
Srinivas.Can we get a fixed number of records using SAMPLE clause of SELECT?SAMPLE works on probability so it returns a variable number of records each time. Also, like using DBMS_RANDOM, it doesn't guarantee to return a distinct set of different records each time.
Your actual requirement is for a paginated resultset of records sorted in a random order. But the records must be jumbled up once only and must not be rejumbled each time you get another set.
One way would be to populate a PL/SQL table or temporary table with the sorted records. Alternatively you could do something like this ...
SQL> create or replace package emp_recs as
2 function get_set return sys_refcursor;
3 end;
4 /
Package created.
SQL> create or replace package body emp_recs as
2 ctr_next pls_integer;
3 function get_set return sys_refcursor is
4 return_value sys_refcursor;
5 begin
6 open return_value for
7 select empno, ename, deptno from
8 ( select e.*, rownum as rn
9 from emp e
10 order by dbms_utility.get_hash_value(e.empno||e.ename||e.deptno, 1000, 4194304))
11 where rn between ctr_next+1 and ctr_next + 5;
12 ctr_next := ctr_next + 5;
13 return return_value;
14 end get_set;
15 begin
16 ctr_next := 0;
17 end;
18 /
Package body created.
SQL> var rc refcursor
SQL> exec :rc := emp_recs.get_set
PL/SQL procedure successfully completed.
SQL> print rc
EMPNO ENAME DEPTNO
7654 MARTIN 30
7566 JONES 20
7521 WARD 30
7499 ALLEN 30
7369 SMITH 20
SQL> exec :rc := emp_recs.get_set
PL/SQL procedure successfully completed.
SQL> print rc
EMPNO ENAME DEPTNO
7844 TURNER 30
7839 KING 10
7788 SCOTT 20
7782 CLARK 10
7698 BLAKE 30
SQL> Cheers, APC -
How to select only one record at a time in ALV
Hi all,
I have to use ALV report. Each record on the report output should have a button (or radio button) which will allow the record to be selected. My requirement is to select only one record at a time.
please guide me how to proceed further.
regards
manishhi
https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/sampleCheckBoxProgram
Regards
Pavan -
How to select a random cell in the DataGridView?
Good day
I am new to the DataGridView control. I was wondering if one can select a random cell in the DataGridView and if it is possible can someone help me with the code or a link to do it please.
Thank you.Hi Arno du Toit,
Use the following code, you can select the specilized cell.
DataGridView1.CurrentCell = DataGridView1.Rows[rowindex].Cells[columnindex];
Select a random Cell?
Random rnd = new Random();
int rowMax = DataGridView1.Rows.Count;
int colMax = DataGridView1.Columns.Count;
if(rowMax > 0 && colMax > 0)
int row = rnd.Next(1, rowMax); //get the random row index
int column = rnd.Next(1, colMax); //get the random column index
DataGridView1.CurrentCell = DataGridView1.Rows[row].Cells[column];
If you have any other concern regarding this issue, please feel free to let me know.
Best regards,
Youjun Tang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey.
Maybe you are looking for
-
Screen sharing connect dialog hung
last nite i tried 2 connect 2 a m.b.pro (running 10.6.8) over wifi from my 10.7.4 m.b.pro. it was still spinning when i closed up 4 the nite, and now this morning the dialog is still spinning, and even when selected, the dialog's cancel button is dis
-
Need to know what i have to do
I got a power mac g4 from my job it work one time and now it start up with a icon ? and faces what do i have to do
-
Unwanted white space in Illustrator CS3 EPS
Hai All I am facing a problem with the specific image find the attahced. There is no white space in images (87862fig2_3.eps) if we open it in "Adobe Illustrator CS3". We have imported the same image in "In Design CS3", but there was some white space
-
I hope this doesn't come off as a dumb question... and I'm not really sure how to ask it. The server path for users in iCal are found at /principals/_uids_/really long string of random characters. Is this correct? Shouldn't they be found at /principa
-
hi all, Frequently i am getting this error, when loading mapping. "ORA-01653: unable to extend table %s.%s by %s in tablespace %s" and I need to load 10,00,000 laks of records at a time. How to solve this problem?? IS THERE ANY PERFOMANCE TIPS?? Than