Help to write a query

Hi,
i want to split a string into words through a query is there any query for that
words in string are space saperated.
Thanks

Check out Re: Problem on string
SQL> SELECT trim(STR1) str1
  2  FROM
  3  (
  4          SELECT DECODE(ROWNUM,1,SUBSTR(STR,1,INSTR(STR,' ',1)),
  5                               LENGTH(STR)-LENGTH(REPLACE(STR,' '))+1,SUBSTR(STR,INSTR(STR,' ',-1,1)),
  6                               SUBSTR(STR,INSTR(STR,' ',1,ROWNUM-1), INSTR(STR,' ',1,ROWNUM)-INSTR(STR,' ',1,ROWNUM-1))
  7                        )  STR1
  8          FROM (SELECT '&string' STR FROM DUAL),ALL_TABLES
  9          WHERE ROWNUM <= LENGTH(STR)-LENGTH(REPLACE(STR,' '))+1
10          ORDER BY ROWNUM
11  )
12  where trim(STR1) is not null
13  /
Enter value for string: Hello Welcome to Oracle Forum
old   8:         FROM (SELECT '&string' STR FROM DUAL),ALL_TABLES
new   8:         FROM (SELECT 'Hello Welcome to Oracle Forum' STR FROM DUAL),ALL_TABLES
STR1
Hello
Welcome
to
Oracle
Forum

Similar Messages

  • Need a help to write the query

    Hi,
    I need a small help to write the query.
    I have a table contains couponid,coupon,createdate,expirationdate,assigndate from couponday table
    i want to write the query to select the coupon whose Expiry date in the table is NOT within 30 days.
    Thanks in advance

    Hi,
    user586 wrote:
    i want to write the query to select the coupon whose Expiry date in the table is NOT within 30 days.If you mean expirationdate (datatype: DATE) is not within 30 days (past or future) of run time, then:
    SELECT  coupon          -- or whatever columns you want
    FROM    table_x
    WHERE   expirationdate  NOT BETWEEN  SYSDATE - 30
                                AND      SYSDATE + 30
    ;

  • I need a help to write the query

    Id
    Child
    Parent
    Is Sub Parent
    Is Parent
    1
    chld1
    y
    2
    chld2
    chld1
    y
    3
    chld3
    chld1
    y
    4
    chld4
    chld1
    5
    chld5
    chld2
    y
    6
    chld6
    chld2
    7
    chld7
    chld5
    8
    chld8
    Q: please check the above table, id no 1-7 are linked with parent and id no 8 is independent child, i need to write a query to display the below result based on the below scenarios
    Scenario 1: if user passes the child 1 the result should be as showed below table all 7 rows need to show.
    Scenario 2: if user passes the child in between child 1 - child 7 then also the result should be as showed below table all 7 rows.
    RESULT
    Id
    Child
    Parent
    1
    chld1
    2
    chld2
    chld1
    3
    chld3
    chld1
    4
    chld4
    chld1
    5
    chld5
    chld2
    6
    chld6
    chld2
    7
    chld7
    chld5
    Thanks in advance

    Hi,
    next time please post sample data as CREATE TABLE and INSERT statements or with clause like this:
    with mychildren as
       select 1 id, 'chld1' chld, null    prnt, null is_sub_parent, 'Y'  is_parent from dual union all 
       select 2 id, 'chld2' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 3 id, 'chld3' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 4 id, 'chld4' chld, 'chld1' prnt, null is_sub_parent, null is_parent from dual union all 
       select 5 id, 'chld5' chld, 'chld2' prnt,'Y'   is_sub_parent, null is_parent from dual union all 
       select 6 id, 'chld6' chld, 'chld2' prnt, null is_sub_parent, null is_parent from dual union all 
       select 7 id, 'chld7' chld, 'chld5' prnt, null is_sub_parent, null is_parent from dual union all 
       select 8 id, 'chld8' chld, null    prnt, null is_sub_parent, null is_parent from dual
    Here a couple of test with chld7 and chld1
    with mychildren as
       select 1 id, 'chld1' chld, null    prnt, null is_sub_parent, 'Y'  is_parent from dual union all 
       select 2 id, 'chld2' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 3 id, 'chld3' chld, 'chld1' prnt, 'Y'  is_sub_parent, null is_parent from dual union all 
       select 4 id, 'chld4' chld, 'chld1' prnt, null is_sub_parent, null is_parent from dual union all 
       select 5 id, 'chld5' chld, 'chld2' prnt,'Y'   is_sub_parent, null is_parent from dual union all 
       select 6 id, 'chld6' chld, 'chld2' prnt, null is_sub_parent, null is_parent from dual union all 
       select 7 id, 'chld7' chld, 'chld5' prnt, null is_sub_parent, null is_parent from dual union all 
       select 8 id, 'chld8' chld, null    prnt, null is_sub_parent, null is_parent from dual
    , find_root as
        select chld
          from mychildren
         where prnt is null
         start with chld='chld1'  --- this is your input
       connect by prior prnt=chld
    select id, chld, prnt
       from mychildren     
      start with chld=(select chld from find_root)
    connect by prior chld=prnt
            ID CHLD  PRNT
             1 chld1     
             2 chld2 chld1
             5 chld5 chld2
             7 chld7 chld5
             6 chld6 chld2
             3 chld3 chld1
             4 chld4 chld1
    Changing it to chld7 does not change the output.
    Output is now sorted according hierarchical query. If you need a different sort order you can specify what you want.
    Regards.
    Al

  • Help to write a query for cursor

    Hi
    I have a table trans_table which has records for all Credit (C) and Debit(D) entries for customers with the trans_date.
    Now there is a column adj_amt which contains 0 for all D entries. But for C entries it contains current trans_amt - Prev trans_amt. If there is no prev record with C type then it is 0. i.e. If this the first C entry for a particular account then adj_amt will contain 0.
    Below is the data.
    create table trans_table (cust_id number, tran_date date, tran_amt number, tran_type char(1), adj_amt number);
    1 01/01/2007 100     D 0
    1 01/01/2007 100     D 0
    1 01/02/2007 80     C 0
    1 05/02/2007 200     C 120
    1 08/02/2007 300     C 100
    2 01/01/2007 100     D 0
    2 05/01/2007 100     D 0
    2 08/01/2007 100     D 0
    2 09/01/2007 100     D 0
    2 01/02/2007 90     C 0
    2 05/02/2007 200     C 110
    3 01/01/2007 100     D 0
    3 05/01/2007 100     D 0
    3 08/01/2007 100     D 0
    4 01/01/2007 100     D 0
    4 05/01/2007 100     D 0
    4 06/01/2007 100     D 0
    4 08/01/2007 100     D 0
    4 10/01/2007 100     D 0
    4 01/02/2007 100     C 0                    
    Now hv to write a query whioch gives the cust_id , max(tran_amt) with D entry, max(trans_amt) wth C entry and if no C entry record then this column will be 0.
    The output wud be klike this..
    1 100 300
    2 100 200
    3 100 0
    4 100 100
    Since cust_id 3 has no C entry so 0 value in 3rd column.
    Now i wrote a query for cust_id and 2nd column, but am not able to get the 3rd column.
    Is it possible to get the above output..
    I wud be using this query in a cursor which wud fetch from 20 million transactions.
    Thanks in advance
    Piks
    (I am pasting the insert script if required)
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (4, to_date('01-02-2007', 'dd-mm-yyyy'), 100, 'C', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (4, to_date('05-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (4, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (4, to_date('10-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (4, to_date('08-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (4, to_date('06-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (3, to_date('08-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (3, to_date('05-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (3, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (2, to_date('05-02-2007', 'dd-mm-yyyy'), 200, 'C', 110);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (2, to_date('01-02-2007', 'dd-mm-yyyy'), 90, 'C', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (2, to_date('05-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (2, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (2, to_date('09-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (2, to_date('08-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (1, to_date('08-02-2007', 'dd-mm-yyyy'), 300, 'C', 100);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (1, to_date('05-02-2007', 'dd-mm-yyyy'), 200, 'C', 120);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (1, to_date('01-02-2007', 'dd-mm-yyyy'), 80, 'C', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (1, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);
    insert into TRANS_TABLE (CUST_ID, TRAN_DATE, TRAN_AMT, TRAN_TYPE, ADJ_AMT)
    values (1, to_date('01-01-2007', 'dd-mm-yyyy'), 100, 'D', 0);

    Hitherto an attempt:
    test@ORA92>
    test@ORA92> select * from trans_table;
       CUST_ID TRAN_DATE             TRAN_AMT T    ADJ_AMT
             4 02/01/2007 00:00:00        100 C          0
             4 01/05/2007 00:00:00        100 D          0
             4 01/01/2007 00:00:00        100 D          0
             4 01/10/2007 00:00:00        100 D          0
             4 01/08/2007 00:00:00        100 D          0
             4 01/06/2007 00:00:00        100 D          0
             3 01/08/2007 00:00:00        100 D          0
             3 01/05/2007 00:00:00        100 D          0
             3 01/01/2007 00:00:00        100 D          0
             2 02/05/2007 00:00:00        200 C        110
             2 02/01/2007 00:00:00         90 C          0
             2 01/05/2007 00:00:00        100 D          0
             2 01/01/2007 00:00:00        100 D          0
             2 01/09/2007 00:00:00        100 D          0
             2 01/08/2007 00:00:00        100 D          0
             1 02/08/2007 00:00:00        300 C        100
             1 02/05/2007 00:00:00        200 C        120
             1 02/01/2007 00:00:00         80 C          0
             1 01/01/2007 00:00:00        100 D          0
             1 01/01/2007 00:00:00        100 D          0
    20 rows selected.
    test@ORA92>
    test@ORA92> select
      2    cust_id,
      3    max(case when tran_type = 'D' then tran_amt else 0 end) as max_c,
      4    max(case when tran_type = 'C' then tran_amt else 0 end) as max_d
      5  from trans_table
      6  group by cust_id;
       CUST_ID      MAX_C      MAX_D
             1        100        300
             2        100        200
             3        100          0
             4        100        100
    4 rows selected.
    test@ORA92>
    test@ORA92>Cheers
    pratz

  • Please help to write a query in this format

    Sir,
    I am having a table like following
    Deptno job
    10 aaa
    11 bbb
    12 ccc
    10 ddd
    11 eee
    12 ffff
    10 ggg
    deptno-number
    job- varchar
    in the above table I want the out put like following in a single query ,is it possible.
    10 aaa,ddd,ggg (concatenation of all jobs under that particular deptno)
    11 bbb,eee
    12 ccc,ffff
    I tried with Connect by clause, but i am not able to successfully execute it.
    select dept,substr(max(substr(sys_connect_by_path (job,', '),2)),1,100)
    as job
    from test
    start with dept = 10
    connect by job = prior job
    and prior dept = dept
    group by dept;
    please help me.
    regards
    Mathew

    Hi Mathew,
    One approach is to write a specific function to solve the problems. The get_job function listed below returns a list of employees for the specified department.
    CREATE OR REPLACE FUNCTION get_job (p_deptno in emp.deptno%TYPE)
    RETURN VARCHAR2
    IS
    l_text VARCHAR2(32767) := NULL;
    BEGIN
    FOR cur_rec IN (SELECT Job FROM test WHERE deptno = p_deptno) LOOP
    l_text := l_text || ',' || cur_rec.ename;
    END LOOP;
    RETURN LTRIM(l_text, ',');
    END;
    SHOW ERRORS
    The function can then be incorporated into a query as follows.
    COLUMN employees FORMAT A50
    SELECT deptno,
    get_job(deptno) AS Job
    FROM test
    GROUP by deptno;
    DEPTNO JOB
    10 aaa,ddd,ggg
    11 bbb,eee
    12 ccc,ffff
    Thanks,
    Nagesh.

  • Can you please help me write below query in efficient way

    Hello-
    I have wrote one query and its very slow to get the data because i am counting the code at every column from the table in from clause.
    SELECT SUM(diag_total) diag_total,
    SUM(diag_complete) diag_complete,
    SUM(diag_remaining) diag_remaining,
    SUM(diag_declined) diag_declined
    FROM (SELECT hpc.dmpc_hicn,(SELECT COUNT(DISTINCT d.msd_icd9_ref)
    FROM d4c_map_suspects_to_diagnosis d
    WHERE trim(d.msd_sd_hicn) = trim(hpc.dmpc_hicn)) diag_total,
    (SELECT COUNT(DISTINCT dd.dspd_icd9)
    FROM d4c_suspect_provider_diagnosis dd
    WHERE dd.dspd_ds_id_ref = 1000
    AND trim(dd.dspd_hicn) = trim(hpc.dmpc_hicn)
    AND trim(dd.dspd_npi) = trim(hpc.dpmc_provider_no)) diag_complete,
    -- AND hpc.dmpc_ss_id_ref = 1020) diag_complete,
    (SELECT COUNT(DISTINCT dd.dspd_icd9)
    FROM d4c_suspect_provider_diagnosis dd
    WHERE dd.dspd_ds_id_ref = 1010
    AND trim(dd.dspd_hicn) = trim(hpc.dmpc_hicn)
    AND trim(dd.dspd_npi) = trim(hpc.dpmc_provider_no)) diag_remaining,
    -- AND hpc.dmpc_ss_id_ref = 1020) diag_remaining,
    (SELECT COUNT(DISTINCT dd.dspd_icd9)
    FROM d4c_suspect_provider_diagnosis dd
    WHERE dd.dspd_ds_id_ref = 1020
    AND trim(dd.dspd_hicn) = trim(hpc.dmpc_hicn)
    AND trim(dd.dspd_npi) = trim(hpc.dpmc_provider_no)) diag_declined
    --AND hpc.dmpc_ss_id_ref = 1020) diag_declined
    FROM d4c_provider_assignment pc, d4c_hicn_provider_claims hpc
    WHERE pc.pa_assignedto = &imrrid
    AND trim(pc.pa_providerid) = trim(hpc.dpmc_provider_no));
    Can you please help me in this?
    Thanks in advance.
    Kind regards,
    Nik

    May be this?
    You dont even need sum as we are doing count for the whole data at a time. Please check the results and let us know if there is any issue.
    SELECT COUNT (DISTINCT d.msd_icd9_ref) diag_total
          ,COUNT (DISTINCT CASE WHEN dd.dspd_ds_id_ref = 1000 THEN dd.dspd_icd9 END) diag_complete
          ,COUNT (DISTINCT CASE WHEN dd.dspd_ds_id_ref = 1010 THEN dd.dspd_icd9 END) diag_remaining
          ,COUNT (DISTINCT CASE WHEN dd.dspd_ds_id_ref = 1020 THEN dd.dspd_icd9 END) diag_declined
      FROM d4c_provider_assignment pc
          ,d4c_hicn_provider_claims hpc
          ,d4c_suspect_provider_diagnosis dd
          ,d4c_map_suspects_to_diagnosis d
    WHERE     pc.pa_assignedto = &imrrid
           AND TRIM (pc.pa_providerid) = TRIM (hpc.dpmc_provider_no)
           AND TRIM (dd.dspd_hicn(+)) = TRIM (hpc.dmpc_hicn)
           AND TRIM (dd.dspd_npi(+)) = TRIM (hpc.dpmc_provider_no)
           AND TRIM (d.msd_sd_hicn(+)) = TRIM (hpc.dmpc_hicn);

  • Help to write the query

    Hello
    I have created 2 user defined fields
    Field 1 must be changed by the user before they save the sales order, it's a numeric field, value is 0 or 1. Field is called 'SPRECPTE'
    The second field is a field to block the user as long as the above field is not changed. This field is alphanumeric and mandatory but the problem is when you create the UDF you must define a default value.
    Here comes the query to remove the default value and make the control possible when you select the cardcode from OCRD.
    Then the field 2 is blank and it must be field up to allow booking when you change UDF1 from 0 to 1. But the query work to empty the field 2 but it does not when SPRECPTE is equal to 1
    Declare @U_SPRECPTE as numeric (1,0)
    Set @U_SPRECPTE = $[$U_SPRECPTE.1.0]
    IF U_SPRECPTE=0
    Select '' ELSE 'A'

    During order entry, the system checks freight cost based on formated search to add sales tax on freight fields.It was calculated for every line.
    Now we are in live condition the formated search slows down order entry as every time an item is keyed in it recalculates every item again....
    Then I decide to stop the fms based on item feild is modified and to valide the calculation before saving the order
    To secure a sales order is saved without calculating the whole order I created 2 UDF to block users....
    So one UDF must be mandatory and without default value, the first field launch the calculation through FMS of the whole order
    I hope I am clear I am not familair with SBO store procedure but if you think it may help please explain
    B Regards

  • Need help to write matrix query

    Hi all,
    I have the query . I'm getting output like this
    chargetype amount start_date
    DA1 170 04/01/2005
    DA2 1170 04/01/2005
    DA3 1730 04/01/2005
    DA4 17 04/01/2005
    DA5 -120 04/01/2005
    DA6 0 04/01/2005
    DA1 170 04/02/2005
    DA2 2005 04/02/2005
    DA3 590 04/02/2005
    DA4 201 04/02/2005
    DA5 340 04/02/2005
    DA6 120 04/02/2005
    I need my output like this
    chargetype 04/01/2005 04/02/2005 04/03/2005 ......
    DA1 170 170
    DA2 1170 2005
    DA3 1730 590
    DA4 17 201
    DA5 -120 340
    DA6 0 120
    pls help me out. your help is greatly appreciated.
    the real query is .
    SELECT st . sttl_item_nme charge_type,
         SUM ( NVL ( sd . sttl_item_amt , 0 )) Adjustment ,
              trunc(s . START_DT_GMT) start_date_gmt
    from      nm_sttl_item_dtl_type st ,
              nm_sttl_item_dtl sd ,
              nm_settlement s
    where st . sttl_item_num = sd . sttl_item_num
         and sd . sttl_id = s . sttl_id
    and trunc(s . START_DT_GMT) between trunc(to_date('04/01/2005', 'mm/dd/yyyy')) and trunc(to_date('04/02/2005', 'mm/dd/yyyy'))
    and s . ptcpt_cd = 'DEMO'
         and s . sttl_pub_cd = ( select sttl_pub_cd
                                  from nm_settlement c
                                  where trunc(c.master_rpt_version_dt_sys) = (select trunc(max(ss.master_rpt_version_dt_sys))
                                                      from nm_settlement ss
                                                      where ss.ptcpt_cd = 'DEMO'
                                                                                         and ss.source_cd = 'ISO'
                                                                                                   and trunc(ss.start_dt_gmt) = trunc(s.start_dt_gmt)
                                       and sttl_id = s.sttl_id)
         group by st . sttl_item_nme, trunc(s . START_DT_GMT)
         order by trunc(s . START_DT_GMT), sttl_item_nme     
    Thanks & Regards,
    Ramana.

    Search this site for "pivot" or "cross tab."

  • Please help to write a query

    Hello all,
    this is my table called 'friend_list'
    | uid | friend_uid | created |
    | 20 | 93 | 1334914944 |
    | 20 | 119 | 1335158873 |
    | 1 | 119 | 1344847327 |
    | 119 | 120 | 1343114126 |
    | 120 | 1 | 1345524729 |
    | 120 | 125 | 1346315712 |
    | 1 | 20 |1386315742 |
    i would like to get the list of recommended friends for eg: the person with id 20 has got 3 friends (93,119,1). 119 has got 3 friends(1,120,20) so 20 should get 120 as recommended friends and 119 should get 93 as recomended friends. ie results must show friend's friends.

    Hi,
    Welcome to the forum!
    957317 wrote:
    Hello all,
    this is my table called 'friend_list'
    | uid | friend_uid | created |
    | 20 | 93 | 1334914944 |
    | 20 | 119 | 1335158873 | ...Whenever you have a problem, please post CREATE TABLE and INSERT statements for your sample data. Since this is your first post, I'll do it for you:
    CREATE TABLE      friend_list
    (   userid           NUMBER          -- UID is not a good column name
    ,   friend_userid      NUMBER
    ,   created            NUMBER          PRIMARY KEY
    INSERT INTO friend_list (userid, friend_userid, created)
                 VALUES          (20,        93,              1334914944);
    INSERT INTO friend_list (userid, friend_userid, created)
                 VALUES          (20,        119,              1335158873);
    INSERT INTO friend_list (userid, friend_userid, created)
                 VALUES          (1,         119,              1344847327);
    INSERT INTO friend_list (userid, friend_userid, created)
                 VALUES          (119,       120,              1343114126);
    INSERT INTO friend_list (userid, friend_userid, created)
                 VALUES          (120,       1,              1345524729);
    INSERT INTO friend_list (userid, friend_userid, created)
                 VALUES          (120,       125,              1346315712);
    INSERT INTO friend_list (userid, friend_userid, created)
                 VALUES          (1,         20,              1386315742);
    COMMIT;Not everyone who wants to help you knows the application as well as you do, so explain what things mean.
    For example, I assume that the relationships are symmetric. That is, if X is a friend of Y, then Y is a friend of X. And so,
    INSERT INTO friend_list (userid, friend_userid, created)
                 VALUES          (20,        93,              1334914944);is equivalent to
    INSERT INTO friend_list (userid, friend_userid, created)
                 VALUES          (93,        20,              1334914944);Or you could say that one of the above implies the other, though only one of them will actually be entered.
    i would like to get the list of recommended friends for eg: the person with id 20 has got 3 friends (93,119,1). 119 has got 3 friends(1,120,20) so 20 should get 120 as recommended friends and 119 should get 93 as recomended friends. ie results must show friend's friends.Also show the complete results you want from the given sample data, and explain how you get those results from that data.
    I assume that the results you mentioned above are only part of what you want, and that the complete result set is:
        USERID RECOMMENDED_FRIEND_USERID
             1                        93
             1                       125
            20                       120
            20                       125
            93                         1
            93                       119
            93                       120
            93                       125
           119                        93
           119                       125
           120                        20
           120                        93
           125                         1
           125                        20
           125                        93
           125                       119That is, even though you only said you want friends of friends, I assume you want more distant realtionships, too, For example, I assume to want to show 93 as being a recommended friend of 125, even though they have no friends in common.
    one way to get those results is a CONNECT BY query, like this:
    WITH    bidirectional_data    AS
         SELECT     userid
         ,     friend_userid
         FROM     friend_list
        UNION ALL
         SELECT     friend_userid     AS userid
         ,     userid          AS friend_userid
         FROM     friend_list
    ,     connect_by_results     AS
         SELECT     userid
         ,     CONNECT_BY_ROOT friend_userid     AS recommended_friend_userid
         ,     LEVEL                    AS lvl
         FROM     bidirectional_data
         CONNECT BY NOCYCLE     friend_userid     = PRIOR userid
    SELECT    userid, recommended_friend_userid
    FROM       connect_by_results
    GROUP BY  userid, recommended_friend_userid
    HAVING       MIN (lvl)     >  1
    AND       userid     != recommended_friend_userid
    ORDER BY  userid, recommended_friend_userid
    ;If you have Oracle 11.2, you might use a recursive WITH clause instead of CONNECT BY. Always say which version of Oracle you're using (e.g., 10.2.0.1.0).

  • Need help to write a query for Update statement with  join

    Hi there,
    The following update statement gives me error as the given table in set statement is invalid. But its the right table .
    Is the statement correct? Please help .
    update (
           select distinct(vpproadside.VEHICLE_CRED_OVERRIDE.vin)            
             from vpproadside.VEHICLE_CRED_OVERRIDE
             join vpproadside.vpp_vehicle
               on vpproadside.vpp_vehicle.vin = vpproadside.VEHICLE_CRED_OVERRIDE.vin
            where VPP_CARRIER_SEQ_NUMBER = 90
              and EXPIRY_DATE = '17-MAR-10'
       set vpproadside.VEHICLE_CRED_OVERRIDE.EXPIRY_DATE = '15-SEP-10';Edited by: Indhu Ram on Mar 12, 2010 1:00 PM
    Edited by: Indhu Ram on Mar 12, 2010 1:22 PM
    Edited by: Indhu Ram on Mar 12, 2010 2:35 PM
    Edited by: Indhu Ram on Mar 15, 2010 8:04 AM
    Edited by: Indhu Ram on Mar 15, 2010 8:06 AM
    Edited by: Indhu Ram on Mar 15, 2010 8:28 AM

    Ask Tom has very good discussion about this, if UPDATE does not work for PK issue, you can use MERGE
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:760068400346785797

  • Need help in write a query

    Hi guys,
    i have two table. here in each table data like below
    tab1(id, app_id)
    id    app_id
    1     111
    2     222
    tab2(id,role_id)
    id      role_id
    1      900,901,902
    and one emp(id,empid) table.
    id      empid
    1       1234
    Now i want to display app_id, role_id which are assigned a perticular empid = 1234 and which saticifies the ID join condition.
    can any one help me on this
    Rgds,
    KLR

    Wowwww... where is my prev post?
    ok i repeat it :
    1) you need this?
    select e.empid, t2.role_id, t1.app_id from emp e
    join tab2 t2
    on t2.id = e.id
    join tab1 t1
    on t1.id = e.id
    SQL>
    19  /
         EMPID ROLE_ID         APP_ID
          1234 900,901,902        111
    2) or this?
    select empid,
           REGEXP_SUBSTR(role_id,'[^,]+',1,row_number() over(partition by empid order by empid)) val,
           app_id
      from (select e.empid, t2.role_id, t1.app_id
              from emp e
              join tab2 t2
                on t2.id = e.id
              join tab1 t1
                on t1.id = e.id
            connect by INSTR(t2.role_id, ',', 1, level - 1) > 0
                   and prior e.empid = e.empid
                   and prior sys_guid() is not null)
    EMPID    VAL    APP_ID
    1234    900    111
    1234    901    111
    1234    902    111
    Ramin Hashimzade

  • Help in writting a query..

    with my_data as
        ( select '1','A',2 from dual
          union
          select '1','B',1 from dual
          union
          select '2','A',4 from dual
          union
          select '3','A',1 from dual
          union
          select '3','B',1 from dual
          union
          select '3','C',1 from dual
         select * from my_data;
         I need output as
    1 A 2 B 1
    2 A 4
    3 A 1 B 1 C 1

    with my_data as
    ( select '1' col1,'A' COL2,2 col3 from dual
    union
    select '1' col1,'B' COL2,1 col3 from dual
    union
    select '2' col1,'A' COL2,4 col3 from dual
    union
    select '3' col1,'A' COL2,1 col3 from dual
    union
    select '3' col1,'B' COL2,1 col3 from dual
    union
    select '3' col1,'C' COL2,1 col3 from dual
    select col1,
    xmltransform
    sys_xmlagg
    sys_xmlgen(COL2||col3)
    xmltype
    '<?xml version="1.0"?><xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <xsl:for-each select="/ROWSET/ROW">
    <xsl:value-of select="text()"/>
              </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>'
    ).getstringval() listagg
    from my_data
    group by col1;
    C LISTAGG
    1 A2B1
    2 A4
    3 A1B1C1
    Edited by: user642237 on Jan 9, 2009 12:31 PM

  • Need Help to write SQL Query

    Hi Experts,
    I have one requirement as below.
    I have two tables say test_1 and test_2 with same structure
    Now what i want to accomplish is loop through every record in table table_1 and look for records that have no corresponding entries in table table_2 and delete these rows from table table_1.
    Thanks.

    Hi,
    SQL> create table test_1(sno number,name varchar2(10))
      2  /
    Table created.
    SQL> select * from test1
      2  /
    no rows selected
    SQL> desc test1
    Name                                      Null?    Type
    SNO                                                NUMBER
    NAME                                               VARCHAR2(10)
    SQL> insert into test1 values(1,'vijay')
      2  /
    1 row created.
    SQL> insert into test1 values(2,'vijay')
      2  /
    1 row created.
    SQL> insert into test_1 values(2,'vijay')
      2  /
    1 row created.
    SQL> insert into test_1 values(3,'vi')
      2  /
    1 row created.
    SQL> commit
      2  /
    Commit complete.
    SQL> select * from test1
      2  /
           SNO NAME
             1 vijay
             2 vijay
    SQL> select * from test_1
      2  /
           SNO NAME
             2 vijay
             3 vi
    SQL> select * from test1
      2  minus
      3  select * from test_1
      4  /
           SNO NAME
             1 vijayRegards,
    Vijayaraghavan K

  • Need help to write a query in automation process

    i want to run the scripts in automation process.Is anyone tel me wat is the solution for it?
    how to run this scripts in automated process?
    Edited by: 927851 on Apr 19, 2012 9:56 AM

    Hi
    If you are unix user...
    you can use script like below:
    # $2 is the output filename, $1 is the .sql script
    runsql()
        sqlplus -s user/passwd/@somewhere <<EOF  > $2
        start $1
        exit
    EOF
    runsql file1.sql logfile1 &
    runsql file2.sql  logfile2 &
    runsql anotherfile.sql logfile3  &
    wait

  • Need to write SQL query for hierarchy

    Hi Guys,
    I need help to write a query for the below scenario.
    I have one organization, under that I have many branches. when i provide input to query on last branch, i need to display all parents of that branch like below
    Organization   ---parent
      branch 1  ---child
         branch11
         branch 12
        branch 2   ---child
          branch 21
         branch 3 ----child
          branch 31
          branch 32
          branch 33
    here, when i provide input as branch 3, then I need to fetch results branch 2, branch 1 till organization.
    can any one help me on this.
    Thanks in advance!
    Regards,
    LKR

    Hi,
    Is this the same question as
    https://community.oracle.com/thread/2616828
    Please don't post the same question over and over.  Mark this thread as "Answered" right away, and continue in the other thread (if necessary; the other thread has answers.)

Maybe you are looking for

  • I am getting error while connecting to sqlplus by using apps account in 11i

    i am getting error while connecting to sqlplus by using apps account in 11i. Does anyone has similar error. [applmgr@appsc05t01 admin]$ sqlplus SQL*Plus: Release 8.0.6.0.0 - Production on Mon Sep 13 14:34:52 2010 (c) Copyright 1999 Oracle Corporation

  • How do I get shared printer to print when job queue thinks job is completed

    I have a HP d135 connected via USB to my mac mini. This printer is configured to be shared as is the mini file system. My XP machine on the network can see the mini and the printer. The XP machine has the d135 driver installed for itself and the mini

  • SQL Azure server instance

    I want to find out is how many SQL bulk operations can reliably be done at once on a SQL Azure server instance? Is there any capacity restrictions such as # of simultaneous bulk inserts, total # of rows per bulk operation, etc.? How should we handle

  • Install Solaris 10 5/08 on T5x40 with boot device hardware Mirrored Volume

    I will have to install Solaris 10 5/08 on a hardware mirrored volume as a boot default device. According to the "T5140 & T5240" Administration Guide" (Page 57), we have to boot the server (T5140 & T5240) on a network installation image in single-user

  • During software update - unable to backup iphone "error -37"

    After downloading the iphone 2.0 update, when I try to update my phone's software I get the following error: "An error occurred while backing up this iphone (-37). Would you like to continue to update this iphone without backup? continuing without ba