Dbms_random.value

SELECT value
into xxxx
FROM
( SELECT value FROM a
ORDER BY dbms_random.value )
WHERE rownum = 1;
When it picks value 123, how can code it to not pick value123 at least for a year?

Hi,
When it picks value 123, how can code it to not pick value123 at least for a year?You're asking for a random value, and yet you want it to be (albeit temporarily) unique ?
bells ringin'
That's an oxymoron ;-)
See:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:831827028200

Similar Messages

  • Change format date TO_DATE(TRUNC(DBMS_RANDOM.VALUE...

    Hi all...
    I am trying dbms_random like this :
    **Type data :
    AAAA VARCHAR2(35)
    BBBB VARCHAR2(6)
    DDDD VARCHAR2(20)
    E_DATE VARCHAR2(14)
    **store procedur :
    BEGIN
    FOR X IN 0..3 LOOP
    INSERT INTO TEST VALUES (
    'AAAA--'|| dbms_random.string('X', 28),
    'BBBB' || TO_CHAR (TRUNC(dbms_random.value(0,10))),
    'CCCC--'|| dbms_random.string('X', 12),
    TO_DATE(TRUNC(DBMS_RANDOM.VALUE(2455532,2455563+3)),'J')
    END LOOP;
    END;
    Output :
    AAAA BBBB DDDD E_DATE
    AAAA--RTL2OWN8RVOMY1IUD93RP91RWOQA BBBB1 CCCC--JCWAXRO3TMP0 12-DEC-10
    AAAA--629F3A4AIW5E4OHCDVFZ41SFB2U4 BBBB5 CCCC--DLAUZO511Y97 12-DEC-10
    AAAA--XHKR95JISQRTRH54KNOQKDVUJ6QF BBBB6 CCCC--K7CHOT2KULNV 10-DEC-10
    AAAA--7DRZ38SPRIN26HNA7VPQV9FKWCCQ BBBB7 CCCC--FXV3CCY3BWHY 09-DEC-10i want format E_DATE to DD-MM-YYYY without change type data and still use dbms_random...
    Could you pls solve me out ?
    Thank you
    -newbie-pl/sql-

    Hi,
    Sorry, I'm not sure I uderstand the problem.
    Are you getting the date correctly, but you want to store it in a VARCHAR2 column? That's not a very good idea: dates belong in DATE columns, but if you ever want to convert a DATE into a string, use TO_CHAR:
    TO_CHAR ( TO_DATE ( TRUNC ( DBMS_RANDOM.VALUE (2455532, 2455563+3)
                , 'J'
         , 'DD-MM-YYYY'
         )or equivalently:
    TO_CHAR ( DATE '2010-12-01' + dbms_random.value (0, 34)
         , 'DD-MM-YYYY'
         )Since you're not displaying the hours, minutes or seconds, there's no point in using TRUNC just to set them to 00:00:00.
    Edited by: Frank Kulash on Feb 9, 2011 10:32 PM

  • DBMS_Random.VALUE parameters

    Please can anyone describe de DBMS_RANDOM.VALUE(p1, p2) parameters(p1, p2)?

    can't random just generate single numbers like:Sure
    SQL> r
      1  select trunc(DBMS_RANDOM.VALUE(1,10))
      2* from (select rownum from dual connect by rownum < 20)
    TRUNC(DBMS_RANDOM.VALUE(1,10))
                                 6
                                 3
                                 2
                                 9
                                 1
                                 1
                                 8
                                 2
                                 5
                                 9
                                 7
                                 5
                                 7
                                 8
                                 1
                                 2
                                 1
                                 9
                                 2
    19 rows selected.
    SQL>

  • How to get top 11 values per date range

    I want to get the top 11 values by date range.
    Sample Data
    CREATE TABLE SAMPLE_DATA
        DOMAIN_NAME VARCHAR2(100),
        QTD         NUMBER,
        LOAD_DATE   DATE
    -- Insert
    BEGIN
      FOR lc IN 1..20
      LOOP
        FOR ld IN 1..30
        LOOP
          INSERT
          INTO SAMPLE_DATA VALUES
              'DM_'
              ||lc,
              round(dbms_random.value(0,1000)),
              SYSDATE-ld
        END LOOP;
      END LOOP;
    COMMIT;
    END;
    SELECT *
    FROM
      (SELECT DOMAIN_NAME,
        QTD,
        LOAD_DATE
      FROM
        (SELECT DOMAIN_NAME,
          QTD,
          LOAD_DATE
        FROM SAMPLE_DATA
        WHERE LOAD_DATE = TRUNC(SYSDATE-3)
        ORDER BY QTD DESC
      WHERE ROWNUM <=10
      UNION ALL
      SELECT 'Others' DOMAIN_NAME,
        SUM(QTD) QTD,
        LOAD_DATE
      FROM
        (SELECT DOMAIN_NAME,
          QTD,
          LOAD_DATE
        FROM
          (SELECT rownum rn,
            DOMAIN_NAME,
            QTD,
            LOAD_DATE
          FROM
            (SELECT DOMAIN_NAME,
              QTD,
              LOAD_DATE
            FROM SAMPLE_DATA
            WHERE LOAD_DATE = TRUNC(SYSDATE-3)
            ORDER BY QTD DESC
        WHERE rn > 10
      GROUP BY LOAD_DATE
    ORDER BY QTD DESC
    -- Result
    DOMAIN_NAME                 QTD                         LOAD_DATE                  
    Others                      2888                        24/03/13                   
    DM_1                        1000                        24/03/13                   
    DM_20                       933                         24/03/13                   
    DM_11                       913                         24/03/13                   
    DM_3                        743                         24/03/13                   
    DM_13                       572                         24/03/13                   
    DM_12                       568                         24/03/13                   
    DM_9                        564                         24/03/13                   
    DM_6                        505                         24/03/13                   
    DM_5                        504                         24/03/13                   
    DM_2                        480                         24/03/13    
    Please, Help me get in one query this result using a range of date.
    e.g
    using LOAD_DATE BETWEEN '24/03/13' AND '25/03/13'
    DOMAIN_NAME                 QTD                         LOAD_DATE                  
    Others                      2888                        24/03/13                   
    DM_1                        1000                        24/03/13                   
    DM_20                       933                         24/03/13                   
    DM_11                       913                         24/03/13                   
    DM_3                        743                         24/03/13                   
    DM_13                       572                         24/03/13                   
    DM_12                       568                         24/03/13                   
    DM_9                        564                         24/03/13                   
    DM_6                        505                         24/03/13                   
    DM_5                        504                         24/03/13                   
    DM_2                        480                         24/03/13                     
    Others                      1948                        25/03/13                   
    DM_1                        807                         25/03/13                   
    DM_8                        764                         25/03/13                   
    DM_7                        761                         25/03/13                   
    DM_11                       656                         25/03/13                   
    DM_18                       611                         25/03/13                   
    DM_17                       523                         25/03/13                   
    DM_14                       467                         25/03/13                   
    DM_19                       447                         25/03/13                   
    DM_15                       437                         25/03/13                   
    DM_6                        380                         25/03/13             Thank you in advance.

    I got the solution. Just sharing.
    I used analytic functions that make my job easy.
    Sample Data
    DOMAIN_NAME                 QTD                         LOAD_DATE                  
    DM_1                        807                         25/03/2013                 
    DM_1                        1000                        24/03/2013                 
    DM_2                        226                         25/03/2013                 
    DM_2                        480                         24/03/2013                 
    DM_3                        244                         25/03/2013                 
    DM_3                        743                         24/03/2013                 
    DM_4                        48                          25/03/2013                 
    DM_4                        413                         24/03/2013                 
    DM_5                        164                         25/03/2013                 
    DM_5                        504                         24/03/2013                 
    DM_6                        380                         25/03/2013                 
    DM_6                        505                         24/03/2013                 
    DM_7                        761                         25/03/2013                 
    DM_7                        212                         24/03/2013                 
    DM_8                        764                         25/03/2013                 
    DM_8                        308                         24/03/2013                 
    DM_9                        354                         25/03/2013                 
    DM_9                        564                         24/03/2013                 
    DM_10                       214                         25/03/2013                 
    DM_10                       367                         24/03/2013                 
    DM_11                       656                         25/03/2013                 
    DM_11                       913                         24/03/2013                 
    DM_12                       37                          25/03/2013                 
    DM_12                       568                         24/03/2013                 
    DM_13                       332                         25/03/2013                 
    DM_13                       572                         24/03/2013                 
    DM_14                       467                         25/03/2013                 
    DM_14                       87                          24/03/2013                 
    DM_15                       437                         25/03/2013                 
    DM_15                       450                         24/03/2013                 
    DM_16                       238                         25/03/2013                 
    DM_16                       299                         24/03/2013                 
    DM_17                       523                         25/03/2013                 
    DM_17                       143                         24/03/2013                 
    DM_18                       611                         25/03/2013                 
    DM_18                       145                         24/03/2013                 
    DM_19                       447                         25/03/2013                 
    DM_19                       464                         24/03/2013                 
    DM_20                       91                          25/03/2013                 
    DM_20                       933                         24/03/2013                  Top 11 QTD of DOMAIN_NAME per Data Range.
    SELECT *
    FROM
      (SELECT DOMAIN_NAME,
        QTD,
        LOAD_DATE
      FROM
        (SELECT LOAD_DATE,
          DOMAIN_NAME ,
          QTD,
          (DENSE_RANK() OVER (PARTITION BY LOAD_DATE ORDER BY QTD DESC )) AS RANK_QTD
        FROM SAMPLE_DATA
        WHERE trunc(load_date) BETWEEN '24/03/2013' AND '25/03/2013'
      WHERE RANK_QTD <= 10
      UNION ALL
      SELECT 'Others',
        SUM(QTD) AS QTD,
        LOAD_DATE
      FROM
        (SELECT LOAD_DATE,
          DOMAIN_NAME ,
          QTD,
          (DENSE_RANK() OVER (PARTITION BY LOAD_DATE ORDER BY QTD DESC )) AS RANK_QTD
        FROM SAMPLE_DATA
        WHERE trunc(load_date) BETWEEN '24/03/2013' AND '25/03/2013'
      WHERE RANK_QTD > 10
      GROUP BY LOAD_DATE
    ORDER BY LOAD_DATE ASC,
      QTD DESC
    DOMAIN_NAME                 QTD                         LOAD_DATE                  
    Others                      2888                        24/03/2013                 
    DM_1                        1000                        24/03/2013                 
    DM_20                       933                         24/03/2013                 
    DM_11                       913                         24/03/2013                 
    DM_3                        743                         24/03/2013                 
    DM_13                       572                         24/03/2013                 
    DM_12                       568                         24/03/2013                 
    DM_9                        564                         24/03/2013                 
    DM_6                        505                         24/03/2013                 
    DM_5                        504                         24/03/2013                 
    DM_2                        480                         24/03/2013                 
    Others                      1948                        25/03/2013                 
    DM_1                        807                         25/03/2013                 
    DM_8                        764                         25/03/2013                 
    DM_7                        761                         25/03/2013                 
    DM_11                       656                         25/03/2013                 
    DM_18                       611                         25/03/2013                 
    DM_17                       523                         25/03/2013                 
    DM_14                       467                         25/03/2013                 
    DM_19                       447                         25/03/2013                 
    DM_15                       437                         25/03/2013                 
    DM_6                        380                         25/03/2013 

  • Display a column with a list of values from DUAL;

    Hello, how can I display something like:
    COL1
    A
    B
    C
    D
    E
    F

    On my planet these are 'words' ;) :
    SQL> select dbms_random.string('a', level)
      2  from   dual
      3  connect by level <= 10;
    DBMS_RANDOM.STRING('A',LEVEL)
    r
    BB
    pmE
    bHri
    aZZDC
    FzOZBf
    fXfPyIP
    LOqrQbnK
    AYzNgHcGc
    lvxRvflHXj
    10 rows selected.
    SQL> select dbms_random.string('a', dbms_random.value(10, 50))
      2  from   dual
      3  connect by level <= 10;
    DBMS_RANDOM.STRING('A',DBMS_RANDOM.VALUE(10,50))
    AKExSSzIJOkabCT
    VCHHRTcgtnBSlZyRmsITqzWqVNgDnPdnxmt
    qLnuyGQErxZvHttRBpWrPoLEiGUra
    rcllLkyZXiwvlcGaUoEAeMbEdZSHlZBIgdilgGTQzxlIUW
    ftnGzDCzUBEsxKzhDLfgS
    aqUmpIzjqkWMZGpbAchhbiqFBUKdRaDwFAgdP
    nskqNdzmlAKlsGOJpbKfQHiCfvkDpoFcTkTvKaUcGLFDkgjsX
    fwkxBSoVuDSEOFldgDhilvm
    iAnSvDMQzamGvIFNydfjhr
    lHRPUnKgOxYQwWAltdELJNqqfgMRSmbBpSDnjOsfx
    10 rows selected.http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_random.htm#CDEJAHCB

  • Error while inserting data using DBMS_RANDOM

    Hi ,
    I tried the following insert command in the link http://www.oracle-base.com/articles/misc/RollupCubeGroupingFunctionsAndGroupingSets.php
    BANNER
    Oracle Database 10g Release 10.2.0.1.0 - Production
    PL/SQL Release 10.2.0.1.0 - Production
    CORE    10.2.0.1.0      Production
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    SQL> INSERT INTO dimension_tab
      2  SELECT TRUNC(DBMS_RANDOM.value(low => 1, high => 3)) AS fact_1_id,
      3         TRUNC(DBMS_RANDOM.value(low => 1, high => 6)) AS fact_2_id,
      4         TRUNC(DBMS_RANDOM.value(low => 1, high => 11)) AS fact_3_id,
      5         TRUNC(DBMS_RANDOM.value(low => 1, high => 11)) AS fact_4_id,
      6         ROUND(DBMS_RANDOM.value(low => 1, high => 100), 2) AS sales_value
      7  FROM   dual
      8  CONNECT BY level <= 1000;
    SELECT TRUNC(DBMS_RANDOM.value(low => 1, high => 3)) AS fact_1_id,
    ERROR at line 2:
    ORA-00907: missing right parenthesisCould you please advice me to get the correct records

    smile wrote:
    Hi ,
    I tried the following insert command in the link http://www.oracle-base.com/articles/misc/RollupCubeGroupingFunctionsAndGroupingSets.php
    BANNER
    Oracle Database 10g Release 10.2.0.1.0 - Production
    PL/SQL Release 10.2.0.1.0 - Production
    CORE    10.2.0.1.0      Production
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    SQL> INSERT INTO dimension_tab
    2  SELECT TRUNC(DBMS_RANDOM.value(low => 1, high => 3)) AS fact_1_id,
    3         TRUNC(DBMS_RANDOM.value(low => 1, high => 6)) AS fact_2_id,
    4         TRUNC(DBMS_RANDOM.value(low => 1, high => 11)) AS fact_3_id,
    5         TRUNC(DBMS_RANDOM.value(low => 1, high => 11)) AS fact_4_id,
    6         ROUND(DBMS_RANDOM.value(low => 1, high => 100), 2) AS sales_value
    7  FROM   dual
    8  CONNECT BY level <= 1000;
    SELECT TRUNC(DBMS_RANDOM.value(low => 1, high => 3)) AS fact_1_id,
    ERROR at line 2:
    ORA-00907: missing right parenthesisCould you please advice me to get the correct recordsYou can't used named parameters for function/procedure calls in SQL until version 11.
    You'll have to remove the
    low => And stick with the good old "positional" way of doing things.

  • Dbms_random package

    hallo,
    i would like to know how DBMS_RANDOM package can be implemented.
    can you please explain with example.
    is there any other means of performimg random sampling?
    thank you
    regards,
    rao

    Hi
    Desc DBMS_RANDOM;
    PROCEDURE INITIALIZE
    Názov argumentu Typ In/Out predvolene?
    VAL BINARY_INTEGER IN
    FUNCTION NORMAL RETURNS NUMBER
    FUNCTION RANDOM RETURNS BINARY_INTEGER
    PROCEDURE SEED
    Názov argumentu Typ In/Out predvolene?
    VAL BINARY_INTEGER IN
    PROCEDURE SEED
    Názov argumentu Typ In/Out predvolene?
    VAL VARCHAR2 IN
    FUNCTION STRING RETURNS VARCHAR2
    Názov argumentu Typ In/Out predvolene?
    OPT CHAR IN
    LEN NUMBER IN
    PROCEDURE TERMINATE
    FUNCTION VALUE RETURNS NUMBER
    FUNCTION VALUE RETURNS NUMBER
    Názov argumentu Typ In/Out predvolene?
    LOW NUMBER IN
    HIGH NUMBER IN
    Example in SQL+:
    Exec DBMS_RANDOM.INITIALIZE(100);
    SELECT DBMS_RANDOM.Normal
    , DBMS_RANDOM.RANDOM
    , DBMS_RANDOM.VALUE()
    , DBMS_RANDOM.VALUE(10,100)
    FROM DUAL;
    milos

  • Reg: Fetching default value -

    Hi Experts,
    Need some help in writing a query.
    Table 'Tab_2' has column 'c3' which checks for its value from 'tab_1' column 'c1' values, and accordingly fetches value from column 'c2'.
    But if a value from c3 is not present in tab_1 column 'c1', it should default 'c2' value as 'dx'.
    Please find below the Create & Insert scripts:
    create table tab_1(
        c1 varchar2(200Char),
        c2 varchar2(200Char)
    create table tab_2(
        c3 varchar2(200Char),
        c4 varchar2(200Char)
    insert into tab_1 values('104,113,146,165','1x');
    insert into tab_1 values('204,213,246,265','2x');
    insert into tab_1 values('304,313,346,365','3x');
    insert into tab_1 values('default','dx');
    commit;
    insert into tab_2 values('146',null);
    insert into tab_2 values('265',null);
    insert into tab_2 values('333',null);
    commit;Here's query where I'm stuck:
    SQL> with x1 as
      2  (
      3      select
      4          REGEXP_SUBSTR(c1,'[^,]+',1,level) id1,
      5          c2
      6      from tab_1
      7          connect by level <= length(translate(c1,'~0123456789','~'))+1
      8          and prior c1 = c1
      9          and prior sys_guid() is not null
    10          and c1 != 'default'
    11  )
    12  select
    13      id1, c2, c3
    14  from x1 RIGHT OUTER JOIN tab_2
    15      on (x1.id1 = tab_2.c3);
    ID1        C2         C3
    146        1x         146
    265        2x         265
                          333Please let me know if any further clarification is needed.
    - Ranit

    The data model looks flawed. So not sure the following solution will satisfy all the issues with respect to your problem.
    with t
    as
    select regexp_substr(c1, '[^,]+', 1, level) c1
          , c2
       from tab_1
    connect
         by level <= length(c1) - length(replace(c1, ',')) + 1
        and c1 = prior c1
        and prior dbms_random.value() is not null
    select t1.c3, t2.c1, nvl2(t2.c1, t2.c2, (select c2 from tab_1 where c1 = 'default')) c2
      from tab_2 t1
      left join t t2
        on t1.c3 = t2.c1;Edited by: Karthick_Arp on May 22, 2013 12:39 AM
    Changed NVL to NVL2

  • DBMS_RANDOM and ORA-01436: CONNECT BY loop in user data

    Hi,
    The following query gets the error "ORA_01436: CONNECT BY loop in user data":
    SELECT     currentdate
    ,     ADD_MONTHS ( currentdate
                 , (LEVEL - 1) * months
                 ) + ((LEVEL - 1) * days)     AS final_date
    ,     level
    FROM     table_z
    WHERE     CONNECT_BY_ISLEAF = 1
    CONNECT BY     currentdate               = PRIOR currentdate
    AND          PRIOR dbms_random.value          IS NOT NULL
    AND          ADD_MONTHS ( currentdate
                         , (LEVEL - 2) * months
                         ) + ((LEVEL - 2) * days) <= TO_DATE ( '05/01/2008'
                                                                      , 'MM/DD/YYYY'
                                                  );I've seen "PRIOR dbms_random.value IS NOT NULL" used like this as as a way to get around ORA-01436 in Oracle 9, but I'm using Oracle 11, and it doesn't work
    currentdate is unique.
    If I hard-code any one set of values, like this:
    DEFINE currentdate      = "TO_DATE ('03/02/2006', 'MM/DD/YYYY')"
    DEFINE days           = 300
    DEFINE months           = 0
    SELECT     &currentdate
    ,     ADD_MONTHS ( &currentdate
                 , (LEVEL - 1) * &months
                 ) + ((LEVEL - 1) * &days)     AS final_date
    FROM     dual
    WHERE     CONNECT_BY_ISLEAF = 1
    CONNECT BY     &currentdate               = PRIOR &currentdate
    AND          PRIOR dbms_random.value          IS NOT NULL
    AND          ADD_MONTHS ( &currentdate
                         , (LEVEL - 2) * &months
                         ) + ((LEVEL - 2) * &days) <= TO_DATE ( '05/01/2008'
                                                                       , 'MM/DD/YYYY'
                                                   );I can get the correct results from dual. I was looking for a way to extend this to get multiple rows from table_z in the same query.
    The problem came up in [this thread|http://forums.oracle.com/forums/thread.jspa?threadID=894946&tstart=0], where the problem is:
    Given this table:
    CREATE TABLE     table_z
    (      currentdate     DATE
    ,      months          NUMBER
    ,      days          NUMBER
    INSERT INTO table_z (currentdate, months, days) VALUES (TO_DATE ('03/02/2006', 'MM/DD/YYYY'),     0,     300);
    INSERT INTO table_z (currentdate, months, days) VALUES (TO_DATE ('04/05/2006', 'MM/DD/YYYY'),     10,     0);
    INSERT INTO table_z (currentdate, months, days) VALUES (TO_DATE ('05/04/2006', 'MM/DD/YYYY'),     3,     0);I'm trying to add the given number of months and/or days to currentdate until I reach a given hard-coded cutoff date (05/01/2008 in this case).
    That is, the desired results from the data above are:
    CURRENTDAT     MONTHS       DAYS FINALDATE
    03/02/2006          0        300 08/18/2008
    04/05/2006         10          0 10/05/2008
    05/04/2006          3          0 05/04/2008Edited by: Frank Kulash on May 4, 2009 7:38 PM
    Typo in CONNECT BY conditions fixed.

    Hi Frank,
    Sounds like a bug. Funny enough, but this works:
    SQL> select * from v$version where rownum = 1
      2  /
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
    SQL> SELECT currentdate
      2  , ADD_MONTHS ( currentdate
      3       , (LEVEL - 1) * months
      4       ) + ((LEVEL - 1) * days) AS final_date
      5  , level
      6  FROM table_z
      7  WHERE CONNECT_BY_ISLEAF = 1
      8  CONNECT BY currentdate   = PRIOR currentdate
      9  AND  PRIOR dbms_random.value  IS NOT NULL
    10  AND  ADD_MONTHS ( currentdate
    11           , (LEVEL - 1) * months
    12           ) + ((LEVEL - 1) * days) <= TO_DATE ( '05/01/2008'
    13                                        , 'MM/DD/YYYY'
    14                    );
    FROM table_z
    ERROR at line 6:
    ORA-01436: CONNECT BY loop in user data
    SQL>
    SQL> ed
    Wrote file afiedt.buf
      1  SELECT currentdate
      2  , ADD_MONTHS ( currentdate
      3       , (LEVEL - 1) * months
      4       ) + ((LEVEL - 1) * days) AS final_date
      5  , level
      6  FROM table_z
      7  WHERE CONNECT_BY_ISLEAF = 1
      8  CONNECT BY currentdate   = PRIOR currentdate + 0
      9  AND  PRIOR dbms_random.value  IS NOT NULL
    10  AND  ADD_MONTHS ( currentdate
    11           , (LEVEL - 1) * months
    12           ) + ((LEVEL - 1) * days) <= TO_DATE ( '05/01/2008'
    13                                        , 'MM/DD/YYYY'
    14*                   )
    SQL> /
    CURRENTDA FINAL_DAT      LEVEL
    02-MAR-06 23-OCT-07          3
    05-APR-06 05-DEC-07          3
    04-MAY-06 04-FEB-08          8
    SQL> SY.

  • How to use dbms_random in TimesTen

    Hi, All,
    dbms_random is the supplied procedure in TimesTen 11.2.1,
    but I failed with the following select statement:
    select dbms_random.value from dual; or select sys.dbms_random.value from dual
    the error I got is "1024: Referenced table DBMS_RANDOM not in FROM list"
    But all of the query is OK for OracleDB.
    How can I use dbms_random in TimesTen?
    Thanks
    Edited by: user536764 on 2011-1-5 上午8:24

    In pl/sql work fine
    declare
    v number;
    begin
    v := dbms_random.value(1,100);
    dbms_output.put_line(v);
    end;
    85.95011054036622138729731389231348690891

  • Flatten a Key Value Pair...how many joins are too many?

    Hello,
    So, a product can have many attributes...things that describe the product. In our 3rd party ERP, these are stored in a key-value manner.
    product_code
    attribute_code
    attribute_value
    etc.
    Now, for some products there are 150+ attributes....you can pretty much guess where this is going...
    User wants a report that shows an product_code and it's attributes on a single line (in separate columns) for Excel manipulation(s).
    So, the SQL would require joining the same attribute table as many times as there are distinct attribute_codes for a given product_code.
    If there are 150 named/distinct attributes that need to be lined up, this would mean 150 joins on that one table.
    OR write scalars for each attribute
    OR write a function that fetches the attribute_value when you pass the product_code and attribute_code and call this function 150 times in the SQL select list.
    Yes, I know, I should benchmark each approach and select the one that works best....BUT, I would like to poll the wisdom of outstanding individuals on this group to see which of the 3 approaches would be preferred.
    Oh and the users typically "query" hundreds to thousands of products and want this result set.
    We are still on the terminally supported Oracle 10g database on Linux.
    Thanks,
    Manish

    Marc mentioned it already
    with
    eav as
    (select 1 + mod(level,trunc(dbms_random.value(5,20))) product_code,
            trunc(dbms_random.value(1,500)) attribute_code,
            dbms_random.string('u',dbms_random.value(1,10)) attribute_value
       from dual
    connect by level <= 50
    select csv
      from (select product_code,
                   'name' att_type,
                   product_code||',attribute codes,'||listagg(to_char(attribute_code),',') within group (order by attribute_code) csv
              from eav
             group by product_code
            union all
            select product_code,
                   'value' att_type,
                   product_code||',attribute values,'||listagg(to_char(attribute_value),',') within group (order by attribute_code)
              from eav
             group by product_code
    order by product_code,att_type
    CSV
    1,attribute codes,13,299,476
    1,attribute values,LOCO,FKEKQ,UQHBYITKZ
    2,attribute codes,66,72,121,126,198,307,346
    2,attribute values,DJBBK,FVBYYBPQ,LCHQ,BCFYN,ZP,UYWDSGFEJ,CZ
    3,attribute codes,32,101,213,352,369,449,499
    3,attribute values,XKYBDRKPY,RZBU,RWQN,FVCQKWL,N,HCYTLHN,HCHXQLSU
    4,attribute codes,116,210,244,307
    4,attribute values,FKCMZCIJ,BAWZV,RCTDQLRE,CF
    5,attribute codes,89,144,283,293,389
    5,attribute values,YK,CEEAEFX,JEEZLJ,XESPFSWN,TRNYF
    6,attribute codes,183,435,449
    6,attribute values,CZYGEDPH,QEN,HO
    7,attribute codes,282,333,358,373
    7,attribute values,GRIY,ZCS,FGFQKEPQ,VITJKBNU
    8,attribute codes,180,195,374
    8,attribute values,UJPNIOGYS,GNWXLMB,XSFHO
    9,attribute codes,30,103,216,485
    9,attribute values,FJB,VXQHBYIX,RNZGRDBK,I
    10,attribute codes,234
    10,attribute values,VKCDNJ
    11,attribute codes,27
    11,attribute values,QDQHQHGD
    12,attribute codes,51,101,223,333
    12,attribute values,UMJXWTRLCI,XHSPFNFAX,FNFDEBGAYI,INBNTICY
    13,attribute codes,298
    13,attribute values,RQOS
    14,attribute codes,270,480
    14,attribute values,TMWSSNZNXT,PRLODAMEJ
    16,attribute codes,297
    16,attribute values,CITFASX
    Regards
    Etbin

  • DBMS_RANDOM Function

    I would like to return a number between 0-9999 through the dbms_random function. I am trying this:
    exec dbms_random.initialize(1234);
    select dbms_random.random from dual;
    RANDOM
    -3.10E+08
    this is not what I am looking for. Anyone know how I can get the desired results I am looking for?

    SQL> SELECT DBMS_RANDOM.Value(0,9999) FROM dual;
    DBMS_RANDOM.VALUE(0,9999)
                   2499.13527
    SQL> /
    DBMS_RANDOM.VALUE(0,9999)
                   6274.76354TTFN
    John

  • Inheriting the values in tree structure

    In the below graph if P1 value is 10 then all node value should be 10.
    But at any level of node we can change the value of node for example
    If I override P6 with value 20 then p6, p7, p8 values should be 20 and remainings with 10
    P1 10
    P2 P3 10
    30 P4 P5 10
    P6 20
    P7 P8 20
    Expecting output :
    P1 10
    P2 10
    P3 10
    P4 30
    p5 10
    p6 20
    p7 20
    p8 20
    am having a table which stores this hierarchies
    Table : create table Prod_Hierarchy
    ( child_node varchar2(10) ,parent_node varchar2(10))
    With values
    Child_node, Parent_node
    P1
    P2 P1
    P3 P1
    P4 P2
    p5 p2
    p6 p5
    p7 p6
    p8 p6
    And below table is to override the values from inheritence
    create table Prod_value ( node varchar2(10), node_val number(10))
    values:
    P1 10
    p4 30
    P6 20

    What about this solution.
    Processing ...
    create table members (
         id number,
         value number,
         change_id number
    Processing ...
    create sequence change_id start with 1 increment by 1 nocache
    Processing ...
    create or replace trigger register_modification
    before insert or update on members for each row
    begin
         select change_id.nextval
         into :new.change_id
         from dual;
    end;
    TRIGGER REGISTER_MODIFICATION compiled successfully
    Processing ...
    insert into members (id,value) (
         select rownum id, trunc(dbms_random.value(1,5)) * 10 as value
         from dual
         connect by rownum <= 15
    15 row(s) inserted
    Processing ...
    create table relations as
         select rownum id,decode(rownum,1,null,trunc(dbms_random.value(1,rownum))) parent
         from dual
         connect by rownum <= 15
    Processing ...
    select parent,id,value
    from members join relations using (id)
    Query finished, retrieving results...
                     PARENT                                    ID                                    VALUE                
                                                                                  1                                     40
    1                                                                             2                                     40
    2                                                                             3                                     20
    3                                                                             4                                     10
    1                                                                             5                                     20
    2                                                                             6                                     10
    6                                                                             7                                     30
    5                                                                             8                                     30
    1                                                                             9                                     30
    5                                                                            10                                     30
    5                                                                            11                                     20
    7                                                                            12                                     10
    9                                                                            13                                     20
    9                                                                            14                                     20
    3                                                                            15                                     40
    15 row(s) retrieved
    Processing ...
    select parent,id,value
    from members join relations using (id)
    model
         reference get_parent on (
              select parent,id
              from relations
         dimension by (id)
         measures (parent as p) ignore nav
         main result
         dimension by (id,parent)
         measures (value,change_id,change_id as vtg)
         rules automatic order (
              vtg[any,any] = case
                   when vtg[cv(parent),p[cv(parent)]] > vtg[cv(),cv()] then
                        vtg[cv(parent),p[cv(parent)]]
                   else
                        vtg[cv(),cv()]
              end,
              value[any,any] = case
                   when vtg[cv(),cv()] > change_id[cv(),cv()] then
                        value[cv(parent),p[cv(parent)]]
                   else
                        value[cv(),cv()]
              end
    Query finished, retrieving results...
                     PARENT                                    ID                                    VALUE                
                                                                                  1                                     40
    1                                                                             2                                     40
    2                                                                             3                                     20
    3                                                                             4                                     10
    1                                                                             5                                     20
    2                                                                             6                                     10
    6                                                                             7                                     30
    5                                                                             8                                     30
    1                                                                             9                                     30
    5                                                                            10                                     30
    5                                                                            11                                     20
    7                                                                            12                                     10
    9                                                                            13                                     20
    9                                                                            14                                     20
    3                                                                            15                                     40
    15 row(s) retrieved
    Processing ...
    update members
    set value = 15
    where id = 2
    1 row(s) updated
    Processing ...
    select parent,id,value
    from members join relations using (id)
    model
         reference get_parent on (
              select parent,id
              from relations
         dimension by (id)
         measures (parent as p) ignore nav
         main result
         dimension by (id,parent)
         measures (value,change_id,change_id as vtg)
         rules automatic order (
              vtg[any,any] = case
                   when vtg[cv(parent),p[cv(parent)]] > vtg[cv(),cv()] then
                        vtg[cv(parent),p[cv(parent)]]
                   else
                        vtg[cv(),cv()]
              end,
              value[any,any] = case
                   when vtg[cv(),cv()] > change_id[cv(),cv()] then
                        value[cv(parent),p[cv(parent)]]
                   else
                        value[cv(),cv()]
              end
    Query finished, retrieving results...
                     PARENT                                    ID                                    VALUE                
                                                                                  1                                     40
    1 2 15
    2 3 15
    3 4 15
    1                                                                             5                                     20
    2 6 15
    6 7 15
    5                                                                             8                                     30
    1                                                                             9                                     30
    5                                                                            10                                     30
    5                                                                            11                                     20
    7 12 15
    9                                                                            13                                     20
    9                                                                            14                                     20
    3 15 15
    15 row(s) retrievedOn the table reporting values of nodes there is a number that tells in with order the value of node has been updated.
    Then with the model query latest changes are inherieted by child nodes.
    Bye Alessandro

  • Weighted random value (1-10) e.g.3 will be  50 % of records

    Hello,
    i need to generate random values into my table. For the simplification, lets say that i need to generate values in range from 1 to 10. For this there is a lot of ways.
    e.g. UPDATE test_number SET random_value = round (dbms_random.value (1,10))
    But what I need is something like weighted random values. I need random values to be inserted into column, but I need e.g. 3 to be inserted more times than other values e.g. if i have 100 rows i want to have 3 in 50 random rows
    Do you have any idea how to achieve it?
    Thanks a lot for any help or reply !

    Hi,
    You can do something like this:
    WITH     got_p_num     AS
         SELECT     LEVEL                         AS id
         ,     FLOOR (dbms_random.value (1, 19))     AS p_num
         FROM     dual
         CONNECT BY     LEVEL     <= 10
    SELECT       id
    ,       p_num
    ,       CASE
              WHEN  p_num     < 3     THEN p_num
              WHEN  p_num     < 12     THEN 3
                                     ELSE p_num - 8
           END     AS r_num
    FROM       got_p_num
    ORDER BY  r_num
    ;If you want the number 3 to appear on half of the rows; that means that, out of 18 random numbers, you expect 3 to occur 9 times, and the other numbers to appear once each. The query above generates random integers in the range 1 through 18 inclusive, and calls this number p_num. The CASE statement maps p_num to what you really want, a n integer between 1 and 10, with 3 occurring 9/18 (= 1/2) of the time.
    Instead of using a CASE expression, you might want to create a table, that has one row for every number that can be in the output, and the probability of picking that number. You could then join to that table to convert the raw random number to the id that you want.
    Edited by: Frank Kulash on Oct 6, 2011 3:36 PM

  • Doubt in DBMS_RANDOM

    Hi,
    I got one suituation to calculate the percentage and based on that i have to do an insertion into tableA and tableB.
    4 types of roles are there role1, role2, role3, role4 .
    role1 80%
    role2 70%
    role3 30%
    role4 10%
    I have to generate random number, if the role -> role1 random number is 55 and its entered into tableA . next time role2 user is entered
    and the random number is 20 and its entered into TableB.... But now my question is i have to use 4 random numbers .
    is it possible to use in oracle procedure. i should not use any temp tables ... is there any other solutions are there ?
    Regards,
    KBG

    Haven't you read my last piece of code?
    declare
      rand_num_1 number := trunc(dbms_random.value(0,100));
      rand_num_2 number := trunc(dbms_random.value(0,100));
      rand_num_3 number := trunc(dbms_random.value(0,100));
      rand_num_4 number := trunc(dbms_random.value(0,100));
    begin
       -- do what you want with the 4 random numbers...
    end;
    SQL> declare
      2    rand_num_1 number := trunc(dbms_random.value(0,100));
      3    rand_num_2 number := trunc(dbms_random.value(0,100));
      4    rand_num_3 number := trunc(dbms_random.value(0,100));
      5    rand_num_4 number := trunc(dbms_random.value(0,100));
      6  begin
      7  dbms_output.put_line('Rand1 ='||rand_num_1);
      8  dbms_output.put_line('Rand2 ='||rand_num_2);
      9  dbms_output.put_line('Rand3 ='||rand_num_3);
    10  dbms_output.put_line('Rand4 ='||rand_num_4);
    11  end;
    12  /
    Rand1 =2
    Rand2 =55
    Rand3 =58
    Rand4 =99Max
    [My Italian Oracle blog|http://oracleitalia.wordpress.com/2010/01/10/crittografia-in-plsql-utilizzando-dbms_crypto/]
    Edited by: Massimo Ruocchio on Jan 12, 2010 12:40 PM
    Added example

Maybe you are looking for