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 time

I 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

Similar Messages

  • How to genrate non-repeating random numbers

    Hi everyone, i'm trying to write a for loop that genrate a
    list of non-repeating random index number for an array list but i
    can't seem to get the random numbers non-repeat. I'm a newbie in
    Flex 2 so it would be great if anyone could show me what seems to
    be the problem in this code:
    Thank you very much.
    Richie
    for (var j:Number = 0; j < tempArray.length; j++)
    var randomNum:Number =
    Math.round(Math.random()*tempArray.length);
    if (j - 1 >= 0)
    while (randomNum == randomIndex[j-1].index)
    randomNum = Math.round(Math.random()*tempArray.length);
    //Push the random number in an array
    randomIndex.push({index: randomNum});
    Alert.show(randomNum.toString());
    }

    Dont' worry about it guys, i already got it thanks!

  • Non repeating random numbers

    Hi,
    Im writing code to generate 10 random numbers and i have code written so that if a number between 1 and 10 is already chosen, it has to look for another number. heres snippets of the code:
    for (int b = 0; b < 2; b++)
    for (int i = 0; i < a.length; i++)
    int test = (int)(Math.random() * 10);
    boolean r = valueExists(test);
    if (r == (true))
    test = (int)(Math.random() * 10);
    else
    a[i] = test;
    boolean valueExists(int value)
    int z = a.length;
    boolean exists = false;
    for (int j = 0; j < z; j++)
    if (a[j] == value)
    exists = true;
    return exists;
    when i run this code, the numbers do repeat. can anyone help?

    import java.util.List;
    import java.util.ArrayList;
    import java.util.Random;
    public class RandomNumbersDemo {
        private List numbers = new ArrayList();
        private Random random = new Random();
        public static void main(String[] args) {
                 RandomNumbersDemo demo = new RandomNumbersDemo();
            demo.init();
            demo.doThatVoodoo();
        public void doThatVoodoo() {
            while (numbers.size() > 0) {
                System.out.println(getRandomInteger());
        public int getRandomInteger() {
            int element = random.nextInt(numbers.size());
            Integer randomInteger = (Integer) numbers.get(element);
            numbers.remove(element);
            return randomInteger.intValue();
        public void init() {
            for (int i = 0; i < 10; i++) {
                numbers.add(new Integer(i));

  • 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,
    Faisal

    thanks 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.
    Daniel

    Try 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
         ;

  • 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]

  • 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>

  • Help with selecting 10 random records from all records meeting report selection criteria in Crystal 11

    <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&#39;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>

  • Lost Calendar appointments (only non-repeated evnets)

    I have trouble in my Palm Z22 , Palm Desktop 4.2@Windows XP.
    I recently found all the NON-REPEATED Data from yr 2003 to mid 2008 has gone whereas the repeated data during the period are intact. Besides, data before yr 2003 and after June 2008 are alright. The situation is similar to another thread http://forums.palm.com/palm/board/message?board.id=windows_hotsync&message.id=34943
    I have backup copy of files "DateBook.bak", "DateBook.mdb" in Feb 2008, which might be created by another version of Palm Desktop. I know that under my current Palm Desktop 4.2, the working file should be "datebook.dat".
    How can I extract data from "DateBook.bak", "DateBook.mdb" so that it is readable to Palm Desktop 4.2?
    Thanks!
    Post relates to: Palm Z22

    You can recover the data from your user folder provided you have only sync'ed once with the PC when you discovered the data missing. When you sync to the PC there are files created for each PIM category in your user folder. For Example; datebook.dat or datebook (Depending on the version of Palm Desktop) and datebook.bak. Using 6.2 the file names are .mdb and mdb.bak. For 6.2, the files are located in the username folder in PalmOSdesktop in my documents.
    The files are named .dat and .bak. When you sync, the current data is stored in .dat. The next time you sync the data in the .dat file is copied to the .bak file. You can rename the .dat to .old. then rename the .bak to .dat. If the data in the .bak file is intact, it will now be in palm desktop when you reopen Palm desktop. I have included a link to show you how to find the userfolder on the PC.
    http://www.palm.com/cgi-bin/cso_kbURL.cgi?ID=26674
    Here is another link regarding data loss;
    http://www.palm.com/cgi-bin/cso_kbURL.cgi?ID=39503
    The best way to backup your data is to use the export feature in Palm Desktop. Make a new folder on the PC desktop named Palm Desktop Backup data. Next, select the category you want to export. You will get a window that asks where you want to save the file, save in the folder created on the desktop. There is a button that allows you to select currently selected record or all, select all. If you don't select all, then only the record highlighted is exported. You will then need to name the file for export, I suggest the name of the category, eg: datebook (With current date). Do this for all critical data in each category. With the data exported to the folder on the PC desktop, you can save this data to a flash drive, external hard drive or a CD-RW. With this method you will have your data in a seperate place than Palm desktop and the device. If you "Lose" data again, you can import the data back into palm desktop.
    Make sure that when you make changes in palm desktop or the device to export the data to the folder created for the backup.
    Here is a link for other options for backing up your data;
    http://www.palm.com/cgi-bin/cso_kbURL.cgi?ID=28751
    Post relates to: Palm i705

  • 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.

  • How to properly create a non-repeat report block?

    Hi everyone,
    I am trying to create a front page for my report. In the report data mode I have a query only return one record, so why that query in the data model showed as a group? <It has the query name - Q_1 on the top, and a group box at the bottom named G_xxxx.>
    Second question is why i use the report block wizard to create the frontpage, the wizard forced me to select a group, otherwise it give warning/error message. I only want to select the client name and his address and no need to repeat <so the frame should use the non-repeatable frame right?>.
    At the moment, I have two queries in the data model, first one is the "select" query which will only return one record for the report's front page, second query is a matrix (cross product) query for the main contents of my report. Both query has no link.
    This is my first Oracle report so any suggestions are the most welcoming.
    Thank you
    Bryan
    Edited by: Bryanevil on Apr 8, 2009 8:19 AM
    Edited by: Bryanevil on Apr 8, 2009 8:26 AM

    Thanks DC
    Thats what I was thinking too. Is this the only way to create a non-repeatable frame; no wizard can be use for create non-repeatable report block? Why I am asking this is because I am lazy, dont want to add all the fields and labels one by one to the frame.
    Secondly, in the data model, is there any way to create a query which the data model will not recognise it as a group query but a single record query?
    Regards
    Bryan

  • Selecting non-contiguous text in Safari

    I cannot find a way to select non-contiguous text in Safari.
    Select>Command>select does not work (as it does in Text Edit.
    I am very surprised that I can't locate any information on this via google, yahoo, etc.
    The only item I did find is something called "Safari Magic" but it looks pretty old and costs $19.
    So here I am hoping to get "truth" from the source (or any kind person).
    I'm in Snow Leopard; Safari 5.1.5.
    Thanks.

    Carolyn
    Thanks. I hadn't explored Safari's Extensions (only Firefox's). Your was a good suggestion. Alas - and to my absolute consternation - nothing for selecting non-contiguous text.
    mcbuffy
    Thanks. And as I alluded to Carolyn...this is really odd and frustrating. Not just that this functionality does not exist in an Apple application while it does in other Apple apps, but also that there does not appear to be any Extensions directed at this function.
    Can I be the only person in the world who selects and copies random or non-contiguous text from web sites?

  • Creating Non-Repeating fields

    Hello all,
    I am creating a query that lists a bunch of student ID's and their schedules. However, the ID's of students should only show on the first row of thier schedule. I've figured out a way to do it with the Lag function, but I was wondering if there is a better way or if this is the only way? Also, does this "method" have a name?
    For example, instead of:
    Student ID    Course
      3823481     Math 101
      3823481     English 201
      3823481     History 230
      5293012     Art 402
      5293012     English 301
      5293012     CGT 212I want this to show:
    Student ID    Course
      3823481     Math 101
                  English 201
                  History 230
      5293012     Art 402
                  English 301
                  CGT 212This is kind of what my code looks like:
      -- Non-repeating PNumber
           Case Lag (ID) over (order by ID)
             When ID Then ' '
             Else ID
            End as Student_ID,
      -- List Classes
           Course_Title
      From (...)
      Where (...)

    Hi ..
    a possible solution:
    with q as(
    select 3823481 Student_ID ,'Math 101' Course from dual union all
    select 3823481    ,'English 201' from dual union all
    select 3823481     ,'History 230' from dual union all
    select 5293012     ,'Art 402' from dual union all
    select 5293012    ,'English 301' from dual union all
    select 5293012    ,'CGT 212' from dual)
    select decode(row_number() over (partition by Student_ID order by course),1,Student_ID) Student_ID,course from q

  • Display Random Record

    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

    Tell 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
    >

  • Non repeating frame - when/how to use

    Hi,
    How do I use a non repeating frame on it's own.
    I have one query in which I have selected 3 fields.
    In the paper layout, I have placed 3 fields in a non repeating frame.
    When I run the report, I get a REP-1213 error - Filed 'xxx' at frequency below its group.
    When I place the 3 fields ina repeating frame, it works, because the repeating frame property inspector allows me to enter the group and the non repeating frame does not.
    So, my question is, when and how does one use a non repeating frame.
    I need to understand the principle behind it.
    Thank you
    i

    You mainly use regular frames to group objects together or prevent them from being overwritten. Check the help on About Frames.

Maybe you are looking for