Oracle LISTAGG function

Hi all

I'm still reviewing the analytical functions and I was wondering if someone can help me with the LISTAGG function. If this has nothing to do with some school or as homework. It's just me discover Oracle.

So, here is some code to insert data of base. It's just a person table and an array of phone number.
-- to avoid name conflicts ;-)
DROP TABLE PERSONNES CASCADE CONSTRAINTS purge;
DROP TABLE TELEPHONES CASCADE CONSTRAINTS purge;

-- create a person table with an id and a name.
CREATE TABLE PERSONNES (
    ID_PERSONNE NUMBER(10)
  , NOM         VARCHAR2(100)
  , CONSTRAINT PK_PERSONNES PRIMARY KEY(ID_PERSONNE)
);

-- table with phone number for persons. 
CREATE TABLE TELEPHONES (
    ID_TELEPHONE NUMBER(10)
  , ID_PERSONNE  NUMBER(10)
  , NUMERO       VARCHAR2(100)
  , ACTIF        CHAR(1) DEFAULT '0' -- is the number active or not.
  , CONSTRAINT PK_TELEPHONES PRIMARY KEY(ID_TELEPHONE)
  , CONSTRAINT UK_NUMERO     UNIQUE(ID_TELEPHONE, NUMERO)
  , CONSTRAINT C_ACTIF_BOOLEAN CHECK(ACTIF IN ('0', '1'))
  , CONSTRAINT FK_PERSONNE   FOREIGN KEY(ID_PERSONNE) REFERENCES PERSONNES
);

-- insert some data.
BEGIN
  -- create a person with a random name.
  FOR i in 1 .. 10 LOOP
    INSERT INTO PERSONNES(ID_PERSONNE, NOM)
      VALUES (i, InitCap(DBMS_RANDOM.STRING('l', 5)));
    
     -- for each newly inserted person, insert 5 different phone numbers.
    FOR j in 1 .. 5 LOOP
      INSERT INTO TELEPHONES(ID_TELEPHONE, ID_PERSONNE, NUMERO, ACTIF) 
        VALUES(((i-1)*10) + j, i,  
            '+32.' ||
              TRUNC(DBMS_RANDOM.VALUE(1, 9)) || '.' ||
              TO_CHAR(DBMS_RANDOM.VALUE(1, 999), 'fm099') || '.' ||
              TO_CHAR(DBMS_RANDOM.VALUE(1, 99), 'fm09') || '.' ||
              TO_CHAR(DBMS_RANDOM.VALUE(1, 99), 'fm09'), 
            CASE MOD(j, 2) WHEN 0 THEN '0' ELSE '1' END
        );
    END LOOP;
  END LOOP;
  
  -- now insert two different person without any phone number.
  INSERT INTO PERSONNES(ID_PERSONNE, NOM)
    VALUES (100, InitCap(DBMS_RANDOM.STRING('l', 5)));

  INSERT INTO PERSONNES(ID_PERSONNE, NOM)
    VALUES (101, InitCap(DBMS_RANDOM.STRING('l', 5)));

END;
The following query returns all the person with their different phone numbers into a single line.
SELECT NOM, LISTAGG(NUMERO, ', ') WITHIN GROUP (ORDER BY NULL)
  FROM PERSONNES A
 LEFT JOIN TELEPHONES B ON A.ID_PERSONNE = B.ID_PERSONNE
 GROUP BY NOM 
 ORDER BY 2 NULLS LAST;
But what I would like is to have numbers grouped by the flag of the ASSET.
Name1   Actif=0  +32... , +3200000000
Name1   Actif=1  +32... , +...........
Name2   Actif=0  +32... , +3200000000
Name2   Actif=1  +32... , +...........
I thought that maybe using the PARTITION CLAUSE but I couldn't understand it. So maybe one of you has a better idea? I want to do in a single query (without using the inline views or with clause). But all the tips are welcome. I just discover Oracle ;-)

Thank you

Hello

user13117585 wrote:
... But what I would like is to have numbers grouped by the flag of the ASSET.

This is the solution! You want to have results grouped by asset, so add assets to the GROUP BY clause:

SELECT NOM
,      ACTIF          -- New
,      LISTAGG(NUMERO, ', ') WITHIN GROUP (ORDER BY NULL)
  FROM PERSONNES A
 LEFT JOIN TELEPHONES B ON A.ID_PERSONNE = B.ID_PERSONNE
 GROUP BY NOM
 ,        ACTIF     -- New
 ORDER BY 3 NULLS LAST     -- Changed
;

If you want to display ACTIVE, you will need to add it to the SELECT clause.
Don't forget to change the ORDER BY clause, which changes what is in the column #2, as stated above (assuming that it is how you want the output sorted). However, a better way is to give this column alias, which can be used in the ORDER BY clause:

SELECT NOM
,      ACTIF
,      LISTAGG(NUMERO, ', ') WITHIN GROUP (ORDER BY NULL)     AS NUMEROS     -- Alias defined
  FROM PERSONNES A
 LEFT JOIN TELEPHONES B ON A.ID_PERSONNE = B.ID_PERSONNE
 GROUP BY NOM
 ,        ACTIF
 ORDER BY NUMEROS NULLS LAST     -- Alias used
;

Tags: Database

Similar Questions

  • Listagg function excdding 4000 characters

    Hi all
    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    PL/SQL Release 11.2.0.3.0 - Production
    CORE     11.2.0.3.0     Production
    TNS for Linux: Version 11.2.0.3.0 - Production
    NLSRTL Version 11.2.0.3.0 - Production
    
    I'm using listagg function to concatenate the column values. I have nearly 4000 records in the table. I guess listagg function can concatenate till 4000 characters and if it exceeds 4000 then I'm getting an Ora error which says 'results of string concatenation is too long'. 
    
    case 1:
    select listagg(id, ',') within group (order by id) from (select level as id from dual connect by level < 1000);
    No Issue.
    
    Case 2:
    select listagg(id, ',') within group (order by id) from (select level as id from dual connect by level < 1050);
    Ora-01489
    
    How to handle the issue? 
    Thank you
    Rod.
    SQL> set long 10000
    SQL> select listagg(id, ',') within group (order by id)
      2    from (select level as id from dual connect by level < 1050)
      3  /
      from (select level as id from dual connect by level < 1050)
                                    *
    ERROR at line 2:
    ORA-01489: result of string concatenation is too long
    
    SQL> select rtrim(xmlagg(xmlelement(e,id,',').extract('//text()') order by id).GetClobVal(),',')
      2    from (select level as id from dual connect by level < 1050)
      3  /
    
    RTRIM(XMLAGG(XMLELEMENT(E,ID,',').EXTRACT('//TEXT()')ORDERBYID).GETCLOBVAL(),','
    --------------------------------------------------------------------------------
    1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
    ,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,5
    7,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,
    84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,
    108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
    128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,
    148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,
    168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,
    188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
    208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,
    228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,
    
    RTRIM(XMLAGG(XMLELEMENT(E,ID,',').EXTRACT('//TEXT()')ORDERBYID).GETCLOBVAL(),','
    --------------------------------------------------------------------------------
    248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,
    268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,
    288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,
    308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,
    328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,
    348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,
    368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,
    388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,
    408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,
    428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,
    448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,
    
    RTRIM(XMLAGG(XMLELEMENT(E,ID,',').EXTRACT('//TEXT()')ORDERBYID).GETCLOBVAL(),','
    --------------------------------------------------------------------------------
    468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,
    488,489,490,491,492,493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,
    508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,
    528,529,530,531,532,533,534,535,536,537,538,539,540,541,542,543,544,545,546,547,
    548,549,550,551,552,553,554,555,556,557,558,559,560,561,562,563,564,565,566,567,
    568,569,570,571,572,573,574,575,576,577,578,579,580,581,582,583,584,585,586,587,
    588,589,590,591,592,593,594,595,596,597,598,599,600,601,602,603,604,605,606,607,
    608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,
    628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,645,646,647,
    648,649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,
    668,669,670,671,672,673,674,675,676,677,678,679,680,681,682,683,684,685,686,687,
    
    RTRIM(XMLAGG(XMLELEMENT(E,ID,',').EXTRACT('//TEXT()')ORDERBYID).GETCLOBVAL(),','
    --------------------------------------------------------------------------------
    688,689,690,691,692,693,694,695,696,697,698,699,700,701,702,703,704,705,706,707,
    708,709,710,711,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,
    728,729,730,731,732,733,734,735,736,737,738,739,740,741,742,743,744,745,746,747,
    748,749,750,751,752,753,754,755,756,757,758,759,760,761,762,763,764,765,766,767,
    768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,
    788,789,790,791,792,793,794,795,796,797,798,799,800,801,802,803,804,805,806,807,
    808,809,810,811,812,813,814,815,816,817,818,819,820,821,822,823,824,825,826,827,
    828,829,830,831,832,833,834,835,836,837,838,839,840,841,842,843,844,845,846,847,
    848,849,850,851,852,853,854,855,856,857,858,859,860,861,862,863,864,865,866,867,
    868,869,870,871,872,873,874,875,876,877,878,879,880,881,882,883,884,885,886,887,
    888,889,890,891,892,893,894,895,896,897,898,899,900,901,902,903,904,905,906,907,
    
    RTRIM(XMLAGG(XMLELEMENT(E,ID,',').EXTRACT('//TEXT()')ORDERBYID).GETCLOBVAL(),','
    --------------------------------------------------------------------------------
    908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927,
    928,929,930,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,
    948,949,950,951,952,953,954,955,956,957,958,959,960,961,962,963,964,965,966,967,
    968,969,970,971,972,973,974,975,976,977,978,979,980,981,982,983,984,985,986,987,
    988,989,990,991,992,993,994,995,996,997,998,999,1000,1001,1002,1003,1004,1005,10
    06,1007,1008,1009,1010,1011,1012,1013,1014,1015,1016,1017,1018,1019,1020,1021,10
    22,1023,1024,1025,1026,1027,1028,1029,1030,1031,1032,1033,1034,1035,1036,1037,10
    38,1039,1040,1041,1042,1043,1044,1045,1046,1047,1048,1049
    
    SQL>
    

    SY.

  • LISTAGG function: "result of concatenating string is too long."

    Hello

    I use Oracle SQL developer 3.0.04 version. I tried using the LISTAGG function to group the data.
    CREATE TABLE FINAL_LOG AS
    SELECT SESSION_DT, C_IP, CS_USER_AGENT,
    listagg(WEB_LINK, ' ')
         WITHIN GROUP(ORDER BY C_IP, CS_USER_AGENT) "WEB_LINKS"
         FROM webviews
         GROUP BY C_IP, CS_USER_AGENT, SESSION_DT
         ORDER BY SESSION_DT
    However, I get the error message,

    SQL error: ORA-01489: result of concatenating string is too long

    Is it possible to go around it, or are there other alternatives?

    Tim Hall has a page on the various techniques of aggregation of chain that guides you through an example of creating and using a user-defined aggregate.

    On AskTom referenced by Tim page, there is an implementation of an aggregation function of string that returns a CLOB that you can use.

    If you want to understand what makes the user-defined aggregate function, it may look a bit complex, especially if you have never looked at object types in PL/SQL. You don't need to understand all the details of the implementation if you use just the code, however, you can simply create the function and use it as you would with any other aggregate function.

    Justin

  • listagg function problem

    Hello

    I'm not able to get the data using the listagg function.

    DB: 11g

    CREATE TABLE HAS

    (NAME, VARCHAR2 (10))

    VAL VARCHAR2 (10));

    INSERT INTO A VALUES ('LTR', ' 111');

    INSERT INTO A VALUES ('LTR', ' 112');

    INSERT INTO A VALUES ('LTR', ' 113');

    COMMIT;

    SELECT * FROM A;

    SELECT * FROM WHERE A VAL IN (SELECT LISTAGG (A1. VAL, ",") THE GROUP (RANKING BY A1. (VAL) AN A1);


    Break down the problem.

    your subquery is:

    SELECT LISTAGG (A1. VAL, ",") THE GROUP (RANKING BY A1. VAL) AN A1;

    which returns the following STRING:

    LISTAGG (A1. VAL, ",") WITHINGROUP (ORDERBYA1. VAL)

    --------------------------------------------------------------------------------

    111,112,113

    Then use one to find... so basically, you do this:

    SELECT * FROM WHERE A VAL IN ('111,112,113');

    See the problem yet?

    Looking around 'dynamic IN the list... "

    I don't have the links, however, it is fairly easy to find in this search string

  • Listagg function gives ORA-00979: not a GROUP BY expression error

    Hello

    I have an environment that supports the Listagg function. Suite works perfectly:

    Select task_code, LISTAGG(ename||) e ('| role |') (',',') WITHIN GROUP (ORDER BY ename) as employees

    team

    where task_code = '01.07.05'

    Task_code group

    However, this is just a test request, the real is a little wider and I can't get the function works, I get ORA-00979: not a GROUP BY expression error.

    Here's my query (the column names are in Dutch, but that shouldn't really be a problem):

    SELECT distinct op_sod.sod_code, op_sod.sod_omschr_lang, op_ood.ood_code, op_ood.ood_omschr_lang, op_activiteit.act_code, op_activiteit.act_omschr_lang, op_taak.taak_code, op_taak.taak_omschr_lang, op_afdeling___master.afd_code, op_taak.belmo, op_taak_j.planning_la,

    LISTAGG (op_ploeg.wn_naam |) ' ('| op_ploeg.rol |') (',',') WITHIN GROUP (ORDER BY op_ploeg.wn_naam) as employees.

    "Write subtaak/mijlpaal toe."

    op_teamplanning.subtaak_mijlpaal as subtaak_of_mijlpaal,

    op_teamplanning.subtaak_omschr | -case when op_teamplanning.subtaak_mijlpaal = 'BELMO Mijlpaal' then ' (BRUNO mijlpaalnr: ' | nvl (op_teamplanning.nummer, 0) |) ')' other ' ' end

    as subtaak_mijlpaal_omschrijving, op_teamplanning.deadline as date limit, op_teamplanning.status as status,

    trim (both

    case when op_teamplanning.vlag1 = 'Geen' and op_teamplanning.vlag2 = 'Geen' and op_teamplanning.vlag3 = 'Geen' and op_teamplanning.vlag4 = 'Geen'

    and op_teamplanning.vlag5 = 'Geen' or op_teamplanning.vlag1 is null and op_teamplanning.vlag2 is null and op_teamplanning.vlag3 is null

    op_teamplanning.vlag5 is null and op_teamplanning.vlag4 is null then 'Geen '.

    Another pad (both case when op_teamplanning.vlag1 = 'Geen' then "else trim (both from op_teamplanning.vlag1) end) |

    trim (both case when op_teamplanning.vlag2 = 'Geen' then "another ',' | trim (both from op_teamplanning.vlag2) end) |

    trim (both case when op_teamplanning.vlag3 = 'Geen' then "another ',' | trim (both from op_teamplanning.vlag3) end) |

    trim (both case when op_teamplanning.vlag4 = 'Geen' then "another ',' | trim (both from op_teamplanning.vlag4) end) |

    trim (both case when op_teamplanning.vlag5 = 'Geen' then "another ',' | trim (both from op_teamplanning.vlag5) end) end) as Beleidsvlaggen,

    op_teamplanning.naam_verantw as quotation, op_teamplanning.subtaak_id as subtaak_id, nvl (op_teamplanning.nummer, 0) as Nummer, nvl (op_teamplanning.regelgevingsagenda, 'Nee') as

    Regelgeving, op_teamplan_subsubtaak.subsubtaak_submijlpaal as subsubtaak_submijlpaal, op_teamplan_subsubtaak.subsubtaak_id as subsubtaak_id, op_teamplan_subsubtaak.subsubtaak_omschr

    like subsubtaak_omschr, op_teamplan_subsubtaak.deadline, sub_deadline, op_teamplan_subsubtaak.status as sub_status, op_teamplan_subsubtaak.naam_verantw as naam_verantw

    Of...

    WHERE THE...

    Group of op_sod.sod_code, op_sod.sod_omschr_lang, op_ood.ood_code, op_ood.ood_omschr_lang, op_activiteit.act_code, op_activiteit.act_omschr_lang, op_taak.taak_code, op_taak.taak_omschr_lang, op_afdeling___master.afd_code, op_taak.belmo, op_taak_j.planning_la


    Can someone tell me the reason for this error?

    Thanks in advance!

    Kind regards

    NDG

    NDG123 wrote:

    I don't really understand your other question:

    Is below for example, a group of expression?

    op_teamplanning.subtaak_mijlpaal

    After some additional research:

    The group by mistake is not appear anymore when I delete all fields after the listagg field, that might be a clue to the solution.

    It is not difficult to understand. You can have other columns not aggregated in your projection on the side of those listed in the group by expression.

    The reason for ORA-01489 is that the resulting string will become long and exceeds 4000 bytes.

    You will find several solution in this forum.

    For example

    Re: Listagg function excdding 4000 characters

  • limit the number of values in the listagg function and create several games

    Hi all

    I have a requirement where I would like to group one of the fields in a field based on the number of records. Lets say if we have an employee with multiple CODEZONE. Please see the example below. I want concatenation of each 10 EMPNO records into one. If she has more than 10 records for a given EMPNO, it should show in the form of another record.
    For example:
    EMPNO CODEZONE
    A111 AL
    A111 AK
    A111 AZ
    A111 AR
    A111 CA
    A111 CO
    A111 CT
    TO A111
    A111 DC
    A111 FL
    A111 GA
    A111 HI
    B222 AL
    B222 AK
    B222 AZ
    B222 AI
    B222 KS
    B222 KY
    THE B222
    B222 ME
    B222 MD
    MY B222
    B222 MI
    B222 CA
    B222 CO
    B222 CT
    B222 OF

    Result set:
    EMPNO CODEZONE
    A111 AL | AK | AZ | AR | CA | CO | CT | OF | DC | FL
    A111 GA | HI
    B222 AL | AK | AZ | AI | KS |     KY | THE | ME | MD | MY
    B222 MI | CA | CO | CT | OF

    The above example can have any number of area CODE for a given EMPNO, the end result should show only a maximum of 10 codes for an EMPNO in each record. If she has more than 10 CODEZONE, it should show as another record. Please provide me with a generic statement to solve this problem. I was not able to control by setting the limit on records for an EMPNO gave to the LISTAGG function. Any ideas are much appreciated.

    Thank you

    1008754 wrote:
    Hi all

    I have a requirement where I would like to group one of the fields in a field based on the number of records. Lets say if we have an employee with multiple CODEZONE. Please see the example below. I want concatenation of each 10 EMPNO records into one. If she has more than 10 records for a given EMPNO, it should show in the form of another record.
    For example:
    EMPNO CODEZONE
    A111 AL
    A111 AK
    A111 AZ
    A111 AR
    A111 CA
    A111 CO
    A111 CT
    TO A111
    A111 DC
    A111 FL
    A111 GA
    A111 HI
    B222 AL
    B222 AK
    B222 AZ
    B222 AI
    B222 KS
    B222 KY
    THE B222
    B222 ME
    B222 MD
    MY B222
    B222 MI
    B222 CA
    B222 CO
    B222 CT
    B222 OF

    Result set:
    EMPNO CODEZONE
    A111 AL | AK | AZ | AR | CA | CO | CT | OF | DC | FL
    A111 GA | HI
    B222 AL | AK | AZ | AI | KS |     KY | THE | ME | MD | MY
    B222 MI | CA | CO | CT | OF

    The above example can have any number of area CODE for a given EMPNO, the end result should show only a maximum of 10 codes for an EMPNO in each record. If she has more than 10 CODEZONE, it should show as another record. Please provide me with a generic statement to solve this problem. I was not able to control by setting the limit on records for an EMPNO gave to the LISTAGG function. Any ideas are much appreciated.

    Thank you

    ME_XE?with data (empno, areacode) as
      2  (
      3     select 'A111', 'AL' from dual union all
      4     select 'A111', 'AK' from dual union all
      5     select 'A111', 'AZ' from dual union all
      6     select 'A111', 'AR' from dual union all
      7     select 'A111', 'CA' from dual union all
      8     select 'A111', 'CO' from dual union all
      9     select 'A111', 'CT' from dual union all
     10     select 'A111', 'DE' from dual union all
     11     select 'A111', 'DC' from dual union all
     12     select 'A111', 'FL' from dual union all
     13     select 'A111', 'GA' from dual union all
     14     select 'A111', 'HI' from dual union all
     15     select 'B222', 'AL' from dual union all
     16     select 'B222', 'AK' from dual union all
     17     select 'B222', 'AZ' from dual union all
     18     select 'B222', 'IA' from dual union all
     19     select 'B222', 'KS' from dual union all
     20     select 'B222', 'KY' from dual union all
     21     select 'B222', 'LA' from dual union all
     22     select 'B222', 'ME' from dual union all
     23     select 'B222', 'MD' from dual union all
     24     select 'B222', 'MA' from dual union all
     25     select 'B222', 'MI' from dual union all
     26     select 'B222', 'CA' from dual union all
     27     select 'B222', 'CO' from dual union all
     28     select 'B222', 'CT' from dual union all
     29     select 'B222', 'DE' from dual
     30  )
     31  select
     32     empno, listagg(areacode, ',') within group (order by emp_grp)   as emp_list
     33  from
     34  (
     35     select
     36             ceil(row_number() over (partition by empno order by areacode) / 10) as emp_grp,
     37             empno,
     38             areacode
     39     from data
     40  )
     41  group by empno, emp_grp;
    
    EMPNO                          EMP_LIST
    ------------------------------ ------------------------------
    A111                           AK,AL,AR,AZ,CA,CO,CT,DC,DE,FL
    A111                           GA,HI
    B222                           AK,AL,AZ,CA,CO,CT,DE,IA,KS,KY
    B222                           LA,MA,MD,ME,MI
    
    4 rows selected.
    
    Elapsed: 00:00:00.05
    ME_XE?
    

    In the future, it would be nice if you could provide baseline data.

    See you soon,.

  • Oracle 11g LISTAGG function

    Hello

    Hello. I tried to use the new feature in Oracle 11 g: LISTAGG, but I received an error:

    SQL * more: Production release 11.2.0.1.0 Wed Mar 28 15:36:47 2012

    Copyright (c) 1982, 2010, Oracle. All rights reserved.

    Enter the user name: lbihrreader@LTMQA
    Enter the password:

    Connected to:
    Oracle Database 11 g Enterprise Edition Release 11.1.0.7.0 - 64 bit Production
    SQL > SELECT DEVELOPMENTACTIVITY sc.
    SC.' SESSION '.
    LISTAGG (sc. CLASSDATE, ';') THE Group (ORDER BY sc. DEVELOPMENTACTIVITY, sc."SESSION")
    OF LTM. SESSIONCLASS sc
    WHERE sc. HRORGANIZATION = "800"
    AND TS. DELETEFLAG = "00000000000000000000000000000000"
    GROUP BY sc. DEVELOPMENTACTIVITY
    SC.' SESSION ';

    ERROR at line 3:
    ORA-00923: THE KEYWORD not found where expected

    Help, please.

    Thank you
    Helen

    In addition,

    THE Group (ORDER BY sc. DEVELOPMENTACTIVITY, sc."SESSION")

    doesn't make much sense in terms of ORDER BY. "" "You been group by sc. DEVELOPMENTACTIVITY, sc." SESSION "all GROUP lines will have same DEVELOPMENTACTIVITY, sc. ms." SESSION. " In any case, use:

    SELECT sc.DEVELOPMENTACTIVITY
    ,sc."SESSION"
    ,rtrim(xmlagg(xmlelement(s,sc.CLASSDATE,';').extract('//text()') order by sc.CLASSDATE),',')
    FROM LTM.SESSIONCLASS sc
    WHERE sc.HRORGANIZATION = '800'
    AND sc.DELETEFLAG = '00000000000000000000000000000000'
    GROUP BY sc.DEVELOPMENTACTIVITY
    ,sc."SESSION";
    

    For example:

    select  deptno,
            job,
            rtrim(xmlagg(xmlelement(s,sal,';').extract('//text()') order by sal),',') list
    from emp
    group by deptno,job
    /
    
        DEPTNO JOB       LIST
    ---------- --------- -------------------------
            10 CLERK     1300;
            10 MANAGER   2450;
            10 PRESIDENT 5000;
            20 CLERK     800;1100;
            20 ANALYST   3000;3000;
            20 MANAGER   2975;
            30 CLERK     950;
            30 MANAGER   2850;
            30 SALESMAN  1250;1250;1500;1600;
    
    9 rows selected.
    
    SQL> 
    

    SY.

  • without using the listagg function

    Hi Sir,

    Today my Bishop asked me to write a sql query to display comma separated values using listagg, sys_connect_by_path, wm_concat?

    I said, we can write with the cursor... without predefined oracle functions? is this correct? or a Counselor about it?

    Please help me

    Concerning

    AR

    SQL > create table SEPM (deptno number (2), ename varchar2 (2));

    Table created

    SQL > insert all
    2 in samp (deptno, ename) values (10, 'A')
    3 in the SEPM (deptno, ename) values (20, 'd')
    4 in the samp (deptno, ename) values (20, 'E')
    5 SELECT * FROM DUAL;

    3 lines inserted

    SQL > SELECT * FROM SAMP;

    DEPTNO ENAME
    ------ -----
    10A
    20 D
    20 E

    Need to display like this

    10A

    20 D, E

    http://www.sqlsnippets.com/en/topic-11787.html

  • Listagg function displays the keyword not found error message

    is version of Oracle 11 g
    create or replace
    function fn_months_attended
    return number is
    v_array varchar2(1000);
    begin
      select LISTAGG(c.act_id,',') within group (order by c.act_id desc) INTO V_ARRAY from 
              programmes a,campaigns b,activities c
            where  a.pro_id=b.cam_pro_id 
            and    b.cam_id=c.act_act_id 
            order by act_id DESC ;
    return v_array;
    end;
    
    Error i received 
    Error(6,3): PL/SQL: SQL Statement ignored
    Error(6,39): PL/SQL: ORA-00923: FROM keyword not found where expected
    can you please suggest me to find exactly the problem

    Hello

    In the LIST of 11 GR 1 tot material. function does not work. It works in only 11 GR 2.

    Kind regards
    Champion.

  • In Oracle lag function

    Hi all

    I want to understand the inner workings of the Lag function in Oracle, which could help to solve my problem.

    I have the table as the structure below.

    P_ID ENTRY_TIMESTAMP SIZE C_ID
    152871 NOVEMBER 15 05.45.00 PM3092
    1529506.00.00 PM 1ST NOVEMBER 151592
    1530606.00.00 PM 1ST NOVEMBER 15192

    Now, when I write a query as below:

    Select the size, CURR_TIMESTAMP TO_TIMESTAMP (TO_CHAR (ENTRY_TIMESTAMP, 'YYYYMMDDHH24MI'), 'YYYYMMDDHH24MI'),

    TO_TIMESTAMP (TO_CHAR (LAG (ENTRY_TIMESTAMP) OVER (PARTITION BY ORDER BY ENTRY_TIMESTAMP ASC C_ID), 'YYYYMMDDHH24MI'), 'YYYYMMDDHH24MI') LAG_TIMESTAMP

    OF Entry_log

    where ENTRY_TIMESTAMP BETWEEN to_date (1 November 15 18.00.00','DD-MON-YY HH24.MI.) SS') AND to_date (1 November 15 18.00.00','DD-MON-YY HH24.MI.) SS')

    Query will lead to give Lag_timestamp as 06.00.00 PM for size = 1 but will be NULL for size = 15.

    Why is that Oracle will never lag_timestamp = 06.00.00 PM for size = 15.

    Could someone please explain this behavior.

    I'm using Oracle 11 g.

    Thank you

    I had a small mistake in the above query. now, I fixed it

  • Need help with the listagg function

    Hi all

    I try to use Listagg in the query below, but not able to get the answer. It is throwing an error message saying ORA-00979: not a GROUP BY expression.

    If I try to remove the Group By function and run the query, I get the following error ORA-00937: not a function of simple-group.

    Help, please.

    Select Distinct V.User_X, V.Original_Request_Id, (V.Support_Group_Name, ',') listagg Group (order of V.Support_group_name) as Group1,

    T.Individualassignedto, T.Status, T.Groupassignedto, T.Ticketcategory, T.Tickettype, T.orgcompany, T.submitter,

    T.Orgassignedto, T.Urgency

    Display V

    INNER JOIN K_TICKET T ON T.TICKETNUMBER = V.ORIGINAL_REQUEST_ID

    V.Original_Request_ID group

    Because you use a GROUP BY, you don't need SEPARATE.

    All the columns you are not aggregate (list) should be in the GROUP BY.

  • Oracle Spatial function to find the closest channel line according to lat/long

    Hello

    Here's my scenario. I have a table that contains the geometries of type line (road network) channels. The line geomteries are of type Ohio state South (SRID 41104) plan.
    I have a requirement - given a lat/long, find the string line that aligns with this lat/long or the nearest set of strings of line at 0.02 miles.
    It is a typical example of an attempt to identify an accident on our road network situation. Accidents reported us in lat/long through the GPS system.
    How can I achieve this through any space function?

    Thanks for the help in advance.

    Thank you
    L.

    Hello L,

    SELECT
      r.
    FROM
      roadsegments r
    WHERE SDO_NN(r.geometry,
                 sdo_geometry(2001, 41104, sdo_point_type(lat,lon,NULL), NULL, NULL),
                 'sdo_num_res=2 distance=0.02 unit=mile') = 'TRUE';
    

    This will give you the two segments of road nearest you at 0.02 miles. See [url http://docs.oracle.com/cd/E11882_01/appdev.112/e11830/sdo_operat.htm#i78067] documentation on the operator SDO_NN, especially the bit about the use of the index indicator.

    HTH,
    Stefan

  • Oracle form functions and procedures in the APEX, how?

    I'm working to recreate in the APEX, already existing read only forms in Oracle Forms. Report features interactive APEX among other things, in fact worth as well as to target a different audience than utilizies versions of Oracle Form. Versions of Oracle Forms use a lot of pre and post query triggers, PLSQL functions.

    In Oracle Forms that places, these functions can be found in the "Units of program" section of the form. A feature typical of this kind, based on a particular Mission ID route, collecting names of regular passengers, is shaped with a comma and a space after each of them, in a single string that is returned and displayed the list of passengers of a line.

    I have all of this written code so I can move most of the main request of the Oracle form in an interactive report. These functions and triggers called in the form of the "Units of program" section of the Oracle form rather than stored in the database schema in a package, where they would go inside the APEX? Can I create a 'shortcut' in the APEX and call it from the interactive report "Source Région"? I can create the PLSQL function at the level of the region of the interactive report or page level? Or, my best bet creates a package that is stored in the database, all these functions and/or proecedures I might need the original form of Oracle?

    Some advice would be greatly appreciated.

    RLBickham wrote:
    I don't think I've been pretty clear in the description of the specific thing I want to do, simply, it does not reach the level of forms of conversion of the APEX. It is basically a problem of PLSQL function.

    I have an interactive report which is currently 90 percent of what I want however, each line, which represents a Mission may have several feet. Each arm has two places or ICAO codes attached to it. Based on the number of Mission, I want to loop through the array of leg, collect all the codes of ICAO for this Mission, put them together in 1 variable separated by a coma and add this variable to display the columns in this interactive report as the last column.

    In Oracle Forms, I have a function registered in the database, which is called in the main query. Maybe I ask a question that does not need to be asked, but anyway my question is can I put this function currently in the database somewhere within the interactive report and somehow reference it via Http, or should I just stick with set of functions and procedures in the packages stored in the database and called the conventional way?

    You could switch the function to the database and call it from the report query, but it sounds as if it were superfluous. In the report query using any form of Re: 4. How can I convert rows to columns? is appropriate for your version of the (unspecified) database.

    -----

    When you have a problem, you will get an answer faster, more efficient including information as much information as possible from the outset. This should include:

  • Full version of APEX
  • Complete operating system DB, version, edition, host
  • Architecture of Web server (EPG, SST or APEX listener/host operating system)
  • Browser (s) and version (s) used
  • Theme
  • Model (s)

  • Region/section type (s) (particularly as to distinguish if a 'report' is a standard report, an interactive report, or indeed a 'update report' (i.e. a tabular presentation))

    With APEX, we also had the chance to have a great resource in apex.oracle.com where we can reproduce and share problems. Reproduce things is the best way to solve most of the questions, particularly those relating to the layout and Visual formatting. If you expect a detailed response it is appropriate that you take on an important part of the effort by getting as much as possible with an example of the problem on apex.oracle.com before asking for help on specific issues, then we can see firsthand.

  • No support for oracle sql functions

    Hi all

    Please can someone explain why the B-tree indexes are not capable of supporting SQL queries using the built-in functions of Oracle.

    We have indexes of tree b on account_no column.

    Select * from test_table where account_no = 11005208

    the above query use the index on account_no

    Select * from test_table where superior (account_no) = 11005208

    But this query does not use the index on the account_no, going for the full table scan.

    My question here is account_no is the numeric data type.
    Then, there is no difference between the data in the columns account_no and upper (account_no) column data, so why he does not use index when using sql functions in an sql query?

    Hello

    If you apply the upper function, Oracle implicitly converts its argument to a string, so your query is actually upper (to_char (account_no)).
    There is none HAVE fully functional in the optimizer and you do not expect to know everything and do all the conclusions that a human being can do. Why should Oracle re - write this type of query analysis the data type of the columns and features when it seems more natural programmer write a correct query (without capital letters in this example) or, if necessary, create an index based on a function?

  • Oracle to_number function pramaters

    I have problems with TO_NUMBER function second and third parameters. One dependent on the other? How does the nls_params parameter? I don't understand how the result of the query

    SELECT TO_NUMBER ('17,000, 23',
    "999G999D99,"
    ' nls_numeric_characters = ",". ')
    REFORMATTED_NUMBER
    FROM DUAL;

    can be 17000.23. Could someone please explain the above conversion process. I looked towards the top of the documentation, but it is not explanatory eneogh.

    P.S. The query above is taken from a book of preperation certificate Expert Oracle database SQL.

    See
    TO_NUMBER problem
    Re: number format

Maybe you are looking for