Random Record Selection
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?
Thanks
MySQL:
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]
Similar Messages
-
Random record selection in Access
I need to define a record set in ASP / Access consisting of a
single record from the available records.
In PHP / MySQL I have used the following and it works fine:
SELECT *
FROM photolist
ORDER BY RAND()
LIMIT 1
Can anyone give me a suitable recordset definition to do this
in access?"whatalotofrubbish" <[email protected]>
wrote in message
news:gmk6pe$90k$[email protected]..
>I need to define a record set in ASP / Access consisting
of a single record
> from the available records.
> In PHP / MySQL I have used the following and it works
fine:
>
> SELECT *
> FROM photolist
> ORDER BY RAND()
> LIMIT 1
>
> Can anyone give me a suitable recordset definition to do
this in access?
>
Access won't do a "random" command when queried from a web
page, although it
can if done so within the actual Access application. ASP
pages can certainly
utilize mySQL databases; why not use mySQL as it would be far
more robust
and stable than Access would be. If you can't move to mySQL,
try this
extension from DMXzone:
http://www.dmxzone.com/go?3705 -
<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 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 -
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/ -
Opening updated report fails because record selection filter is invalid
I am trying to update a report stored in BOE XI. The report has been modified and the main part of the report has been moved to a subreport. I first replace the .rpt file with the updated report. Then I attempt to open the report using the code below it, but it throws an error and complains about an unknown field being mentioned in the filter
oReportAppFactory = (IReportAppFactory) es.getService("", "RASReportService");
oReportClientDocument = oReportAppFactory.openDocument((IInfoObject) oReport, 0, Locale.ENGLISH);
ERROR MESSAGE: Cannot open report document. --- This field name is not known.
Details: errorKind
Error in File /opt/bobje/bobje/tmp/{F395AC11-26FC-11DF-A70C-00065B8B38B5}.rpt:
Error in formula <Record Selection>.
'// Filter on desired date range
This field name is not known.
Details: errorKind
If I manually delete the record selection filter in CMC before upgrading the report all works fine. So, do I need to remove the filter before attempting to open the report or is there another step I'm missing to refresh the report using the newly updated .rpt file?
thanks,
BrettThe relevant part of the stacktrace is below.
java.lang.UnsupportedOperationException
at com.crystaldecisions.sdk.properties.internal.AbstractSDKList.remove(Unknown Source)
at java.util.AbstractList$Itr.remove(AbstractList.java:436)
at com.crystaldecisions.sdk.plugin.desktop.common.internal.i.do(Unknown Source)
at com.crystaldecisions.sdk.plugin.desktop.report.internal.e.f(Unknown Source)
at com.crystaldecisions.sdk.plugin.desktop.report.internal.e.f(Unknown Source)
at com.crystaldecisions.sdk.plugin.desktop.report.internal.e.refreshProperties(Unknown Source)
The context is the same as the original post...
An existing report is being updated. The original report uses a single view and has a record selection filter. The updated report uses the same view in a subreport, and the main report has no record selection filter.
.rpt file is updated using IReport.replaceUnique(int, File).
Changes are committed to IInfoStore -> iStore.commit(IReport)
Refresh options are enabled
IReportRefreshOptions refreshOptions = oReport.getReportRefreshOptions();
refreshOptions.removeOption(IReportRefreshOptions.CeRefreshOption.TITLE);
refreshOptions.removeOption(IReportRefreshOptions.CeRefreshOption.DESCRIPTION);
refreshOptions.addOption(IReportRefreshOptions.CeRefreshOption.PROMPT_VALUES);
refreshOptions.addOption(IReportRefreshOptions.CeRefreshOption.RECORD_SELECTION_FORMULA);
refreshOptions.addOption(IReportRefreshOptions.CeRefreshOption.GROUP_SELECTION_FORMULA);
refreshOptions.addOption(IReportRefreshOptions.CeRefreshOption.PRINTER_OPTIONS);
refreshOptions.addOption(IReportRefreshOptions.CeRefreshOption.DEFAULT_LOGON_INFO_VALUES);
Properties are then refreshed using IReport.refreshProperties() which throws the UnsupportedOperationException
changes are again committed.
thanks,
Brett -
Record selected in the table control
Hi All,
How do i know the line no of the record selected in the table control.I have declared a table control CONTROLS : t_control1 type tableview using screen 200.But in t_control1-current_line the line no that is stored is always 1.
Thanks,
Rakesh.in table control property window you have to set that suppose you have set itab-mark as the sel field then it will always be X if you select the field ...
regards
shiba dutta -
Record Selection doesn't work with VS 2005 & CR 2008
Hi all,
Recently I installed CR 2008 with Service Pack 1 and integrate with Visual Studio 2005. I converted all the CR reports to CR 2008 while opening the VS project which was created earlier.
My problem is when I load the Crystal report via created application, record selection formula doesn't work instead it shows all records. But in designing time it works fine. Reports which designed with SQL Server Stored Procedure working fine. But reports which designed by adding Tables don't work. It shows all the records without record selection.
I used simple Record selection formula without using any parameter fields. like this
tTransactionDetails.acid} = 1
. still it didn't work.
When I put break point and see assigned values in Report Document object. It shows
Function evaluation timeout
. and then run, it do the record selection. I did this several times, all the time if I see assigned values in Report Document object then the report showed the selected records only otherwise it shows all the records.
I tried this just running the application exe, still the problem remaining.
I have saved the reports without the data, so that it will always load the data from the SQL database.
Please help me to solve this problem.Sorry i couldn't do search before I posted the thread. After posted, I just did a search and found the solution by reading [this thread|https://forums.sdn.sap.com/click.jspa?searchID=20348355&messageID=6774564|https://forums.sdn.sap.com/click.jspa?searchID=20348355&messageID=6774564].
As it has suggested, I just delete the existing Crystal Report Viewer Control from the form and add it again by clicking on CrystalReportViewer version 12.0.2000.0. It worked. -
Date Format in Record Selection
Hello,
I have a problem where the Crystal Report is not returning all the rows that meet the record selection criteria because I believe there is a problem with the date format.
My record selection criteria is:
( {EMPOS.POS_STARTC} <= {@EB_EDATE} and {EMPOS.POS_ENDD} >= {@EB_SDATE} ) or
IsNull({EMPOS.POS_ENDD} )
where EB_EDATE and EB_SDATE are formula fields with the values Date(2011, 12, 31) and Date(2011, 01, 01) respectively which returns the format of DD/MM/YYYY used by the FinanceOne (F1) ERP application that we run the report from. Both formula fields are overriden by the run parameters in the same date format in F1.
If I were to run the SQL below in a query tool like Toad, it returns a row where pos_startc is 1/01/2011 and pos_endd is null, that is not included in the report.
select * from empos where det_numbera = '01285'
and ( (pos_startc <= '20111231' and pos_endd >= '20110101') or pos_endd is null )
1. Why is Crystal Reports excluding this row when the date format is different?
2. How can I change the criteria (or format) to include the one of many other rows missing?Hello,
Crystal Reports is now returning all rows where pos_endd is null in the database with the record selection criteria of:
{CHRIS_EmpSal.pos_endd} = date(0, 0, 0)
However, when I run it in Finance One, it doesn't like this date format Is there another way to get both Crystal and Finance One (i.e., SQL Server DB) to recognise the same syntax for null dates?
Maybe you are looking for
-
In adobe bridge cs6 i would like to pre build the thumb nails on an entire drive inc sub folders
in adobe bridge i would like to build all the thumbnails for the drive so when i go to a sub folder all the iamges are ready to go. i have tried select all and then build hq thumbnail but it only did the images in the root folder i was in. not the
-
Cannot Print to Shared Windows Printer (Solved)
I'm having lots of difficulty printing. I have Arch installed on both my desktop and laptop, and they're both exhibiting the same problem. I am trying to print to a network printer connected to a desktop running Windows XP. Neither Arch system has di
-
This is far more confusing than I want it to be. I want to set the preferences on my icloud email so the body type is 18 pt. And I want to automatically cc all emails I send to my email addy so I have a copy. I was set up to do this before I was forc
-
Need some help with the samples
I am using CR for Visual Studio 2010. After installing CR and verifying the install per the developer guide, I down loaded the samples crsdk_net_samples_12.zip and loaded VB_Win_Data_DataSets.sln, electing to convert it from VS2008 to VS2010. After
-
Error 403--Forbidden using eclipse and weblogic
hi, i am new here on j2ee I am using eclipse3.4 and weblogic 9.1.2 on xp. I am trying to make simple hello world servlet here is what i did on the eclipse click New and click on Dynamic project click on Servlet and write following code for the servle