LISTAGG help

Hello

We use this query

SELECT A.EMAIL, COUNT (B.VALUE), (B.COMP_LIST_ID, ",") LISTAGG Group (ORDER OF B.COMP_LIST_ID) as long AS value1 DASH_EMP_DATA, DASH_EMP_COMP B WHERE B.COMP_LIST_ID IN (SELECT ID FROM DASH_COMP_LIST WHERE COMP_OWNER = 'XYZ') AND A.ID = B.EMP_DATA_ID GROUP OF B.EMP_DATA_ID A.EMAIL ORDER OF A.EMAIL

output just like that

Email count (b.value) value1

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

[email protected]                2                                              386,387

[email protected]                3                                              456,789,123

[email protected]                 1                                               654

We put out like this [Instead of ID value we have to the actual name of the component]

Email count (b.value) value1

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

[email protected]                2                                               comp1,comp2

[email protected] 3 comp3 comp4, comp5

[email protected]                 1                                               comp5

table DASH_EMP_COMP does not have the name of the component

name of the component is stored in a SEPARATE dash_comp_list like this table

ID                                                  NAME

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

386                                             COMP1

387                                              COMP2

---

---

----

We need out put as indicated above, kindly help

Thank you

Hello

Looks like you just need to join the additional table, so the name will be available for use as an argument to the LISTAGG, like this:

SELECT a.email

COUNTY (b.value)

LISTAGG (l.name, ',')

THE Group (ORDER BY b.comp_list_id) AS value1

OF dash_emp_data one

dash_emp_comp b

dash_comp_list l

WHERE b.comp_list_id IN ((in English only)

SELECT id

OF dash_comp_list

WHERE comp_owner = 'XYZ '.

)

AND a.id = b.emp_data_id

AND b.comp_list_id = l.id

GROUP BY b.emp_data_id

a.email

ORDER BY a.email

;

Depending on the relationship between dash_emp_comp and dash_comp_list, you may be able to do what the IN subquery with the join:

SELECT a.email

COUNTY (b.value)

LISTAGG (l.name, ',')

THE Group (ORDER BY b.comp_list_id) AS value1

OF dash_emp_data one

dash_emp_comp b

dash_comp_list l

WHERE a.id = b.emp_data_id

AND b.comp_list_id = l.id

AND l.comp_onwer = 'XYZ '.

GROUP BY b.emp_data_id

a.email

ORDER BY a.email

;

I hope that answers your question.

If this isn't the case, please post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.

Point where queries above are to produce erroneous results, and explain, using specific examples, how you get the right results from data provided in these places.

Always say what version of Oracle you are using (for example, 11.2.0.2.0).

See the FAQ forum: Re: 2. How can I ask a question on the forums?

Tags: Database

Similar Questions

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

  • Need help for the Partition of PL/SQL query for a question

    Hi all


    I'm running on a question that I'm not able to get the desired result. I would appreciate if one of you experts can help resolve this.

    SQL * more: Production of release 11.2.0.1.0 game 4 Feb 09:31:26 2016
    Copyright (c) 1982, 2010, Oracle.  All rights reserved.

    Connected to:
    Oracle Database 11 g Enterprise Edition Release 11.2.0.4.0 - 64 bit Production
    With partitioning, OLAP, Data Mining and Real Application Testing options


    with t as)
    Select 1 numero_enregistrement, Student_ID 123, nvl (trim (01), 'NULL') like Item_id, 'ITEM_ID_01' FIELD_NAME Union double all the
    Select 1 numero_enregistrement, Student_ID 123, nvl (trim(' '), 'NULL') like Item_id, 'ITEM_ID_02' FIELD_NAME Union double all the
    Select 1 numero_enregistrement, Student_ID 123, nvl (trim(' '), 'NULL') like Item_id, 'ITEM_ID_03' FIELD_NAME Union double all the
    Select 1 numero_enregistrement, Student_ID 123, nvl (trim(' '), 'NULL') like Item_id, 'ITEM_ID_04' FIELD_NAME Union double all the
    Select 1 numero_enregistrement, Student_ID 123, nvl (trim(' '), 'NULL') like Item_id, 'ITEM_ID_05' FIELD_NAME Union double all the
    Select 1 numero_enregistrement, Student_ID 123, nvl (trim(' '), 'NULL') like Item_id, 'ITEM_ID_06' FIELD_NAME Union double all the
    Select 2 numero_enregistrement, Student_ID 123, nvl (trim('01'), 'NULL') like Item_id, 'ITEM_ID_01' FIELD_NAME Union double all the
    Select 2 numero_enregistrement, Student_ID 123, nvl (trim('02'), 'NULL') like Item_id, 'ITEM_ID_02' FIELD_NAME Union double all the
    Select 2 numero_enregistrement, Student_ID 123, nvl (trim('03'), 'NULL') like Item_id, 'ITEM_ID_03' FIELD_NAME Union double all the
    Select 2 numero_enregistrement, Student_ID 123, nvl (trim('01'), 'NULL') like Item_id, 'ITEM_ID_04' FIELD_NAME Union double all the
    Select 2 numero_enregistrement, Student_ID 123, nvl (trim('01'), 'NULL') like Item_id, 'ITEM_ID_05' FIELD_NAME Union double all the
    Select 2 numero_enregistrement, Student_ID 123, nvl (trim('02'), 'NULL') like Item_id, 'ITEM_ID_06' double FIELD_NAME)
    SELECT distinct COUNT (*) OVER (PARTITION BY numero_enregistrement ORDER BY numero_enregistrement) REC_COUNT, numero_enregistrement, STUDENT_ID, ITEM_ID, Field_Name FROM t
    where ITEM_ID = 'NULL '.

    I get the following result.


    NUMERO_ENREGISTREMENT ITEM_ID FIELD_NAME STUDENT_ID REC_COUNT
    5                                   1                                             123                              NULL               ITEM_ID_02
    5                                   1                                             123                              NULL               ITEM_ID_03
    5                                   1                                             123                              NULL               ITEM_ID_04
    5                                   1                                             123                              NULL               ITEM_ID_05
    5                                   1                                             123                              NULL               ITEM_ID_06


    My desired output is

    Rec_COUNT Student_ID ITEM_ID FIELD_NAME numero_enregistrement
    5 1 123 NULL ITEM_ID_02, ITEM_ID_03, ITEM_ID_04, ITEM_ID_04, ITEM_ID_05

    Thanks in advance

    Rajesh

    Hi, Renon,

    Thanks for posting the sample data and results; It is very useful.

    Be sure to explain exactly how you get these results from these data.  There are many different reasons you may have, that all happen to produce the same results with the sample data you posted, but will get different results with other data sets.

    This looks like a job for aggregate (including the LISTAGG) functions and GROUP BY, not for functions, analytical and PARTITION BY

  • How can I get the nested case statements to work with listagg?

    I have a statement below that does not work as I want it, here is my code:

    Select

    cases where o.personal_label is not null

    then

    listagg (cases where s.subcodevalue like '% MON %')

    then s.subcodevalue |' '|| o.personal_label

    of other s.subcodevalue

    end,': ')

    the Group (order of s.subcodevalue) as subcodevalue

    on the other

    listagg (s.subcodevalue,': ')

    the Group (order of s.subcodevalue) as subcodevalue

    end

    of mtm_styles_new s, order_mtm o

    where INSTR (o.extras, s.code) > 0

    and o.bodyfitting = s.bodyfitting

    and o.division = s.division

    and s.subcodevalue is not null

    and o.ORDER_MTMID = 'somevalue '.

    It should bring the following

    * MY TEST: RPP

    or the following if the first case is not completed:

    * MONDAY: RPP

    I get an error message saying 'missing keyword', but cannot make out where, can someone please help me here?

    The following works fine as long as it is not a nested case statement, but I need to nest the case statement:

    Select listagg (cases where s.subcodevalue like '% MON %')

    then s.subcodevalue |' '|| o.personal_label

    of other s.subcodevalue

    end,': ')

    the Group (order of s.subcodevalue) as subcodevalue

    of mtm_styles_new s, order_mtm o

    where INSTR (o.extras, s.code) > 0

    and o.bodyfitting = s.bodyfitting

    and o.division = s.division

    and s.subcodevalue is not null

    and o.ORDER_MTMID = 'somevalue ';

    Out alias 'subcodevalue as' the case at the end of the case body.

  • Query complex REGEXP LISTAGG

    Hello world

    I have a problem with a complex regular expression, maybe I'm over complicating it.

    Essentially, what I have is some interactions, interaction can go to several queues over time, what I need to achieve is when the queue of the item is listed as WORKBIN or OFFICE I want to find the queue that was before that. Things get complicated, because sometimes I have an example where he goes queue > QUEUE > WORKBIN > TAIL > DESKTOP >TAIL> WORKBIN I need to find the line in "BOLD".

    What I tried was the LISTAGG a partition of interactions for a list as a QUEUE. QUEUE | WORKBIN | QUEUE | DESKTOP COMPUTER | QUEUE: WORKBIN can use REGEXP_INSTR to cut the chain out, but I can't seem to find the latest iteration of the expression.

    Here is an example of the data:

    queue of the date interaction_id

    1037150 2015-01-20 18:35:57 HMB_SITEL_BASIC_IXNQ

    1037150 2015-01-20 20:59:47 desktop_in_progress

    1037150 2015-01-20 21:44:18 TFR_HMB_SITELBASIC_IXNQ

    1037150 2015-01-21 07:54:30 desktop_in_progress

    1037150 2015-01-21 07:56:40 NO

    1037150 2015-01-21 07:56:40 desktop_in_progress

    And the statement that I have tried:

    UBSTR (SUBSTR ('|)) ' || LISTAGG (LAST_QUEUE_NAME, ' |') Group (order of AGENT_END_TS) on (INTERACTION_ID score) |' | ', 1, REGEXP_INSTR ('|) ' || LISTAGG(LAST_QUEUE_NAME, '|') Group (order of AGENT_END_TS) on (INTERACTION_ID score) |' | ((',' desktop_in_progress', 1-2), INSTR (SUBSTR ('|)) ' || LISTAGG (QUEUE_NAME, ' |') Group (order of AGENT_END_TS) on (INTERACTION_ID score) |' | ', 1, REGEXP_INSTR ('|) ' || LISTAGG(QUEUE_NAME, '|') Group (order of AGENT_END_TS) on (INTERACTION_ID score) |' | ((',' desktop_in_progress ', 1-2),' | (',-1) + 1, REGEXP_INSTR ('|) ' || LISTAGG(QUEUE_NAME, '|') Group (order of AGENT_END_TS) on (INTERACTION_ID score) |' | ((',' desktop_in_progress', 1-2)

    Any help or suggestions appreciated

    Thomas

    Select min (rn) of


    with

    data in the form of

    (select 1037150 interaction_id, to_date ('2015-01-20 18:35:57 ',' yyyy-mm-dd hh24:mi:ss') date_, 'HMB_SITEL_BASIC_IXNQ' queue_ of all the double union)

    Select 1037150, to_date ('2015-01-20 20:59:47 ',' yyyy-mm-dd hh24:mi:ss'), 'desktop_in_progress' from dual union all

    Select 1037150, to_date ('2015-01-20 21:44:18 ',' yyyy-mm-dd hh24:mi:ss'), 'TFR_HMB_SITELBASIC_IXNQ' from dual union all

    Select 1037150, to_date ('2015-01-21 07:54:30 ',' yyyy-mm-dd hh24:mi:ss'), 'desktop_in_progress' from dual union all

    Select 1037150, to_date ('2015-01-21 07:56:40 ',' yyyy-mm-dd hh24:mi:ss'), 'NONE' from dual union all

    Select 1037150, to_date ('2015-01-21 07:56:40 ',' yyyy-mm-dd hh24:mi:ss'), 'desktop_in_progress' from dual

    )

    Select interaction_id, to_char (date_, 'yyyy-mm-dd hh24:mi:ss') date_, queue_,.

    cases where instr (upper (queue_), 'WORKBENCH') = 0

    and instr (upper (queue_), 'DESKTOP') = 0

    and instr (upper (queue_), 'NONE') = 0

    then row_number() over (partition by interaction_id by date_ desc order)

    end RN

    from the data

    order of interaction_id, date_

    INTERACTION_ID DATE_ QUEUE_ RN
    1037150 2015-01-20 18:35:57 HMB_SITEL_BASIC_IXNQ 6
    1037150 2015-01-20 20:59:47 desktop_in_progress -
    1037150 2015-01-20 21:44:18 TFR_HMB_SITELBASIC_IXNQ 4
    1037150 2015-01-21 07:54:30 desktop_in_progress -
    1037150 2015-01-21 07:56:40 NONE -
    1037150 2015-01-21 07:56:40 desktop_in_progress -

    Concerning

    Etbin

  • Help Update column

    Oracle 11.2.0.1

    Windows

    create table patient_visited (patient_id, year_list varchar2 (40));

    insert into patient_visited values (478, ' 2003, 2004, 2005, 2008, 2009, 2010, 2014');

    insert into patient_visited values (572, ' 2010, 2011, 2012, 2013, 2014');

    insert into patient_visited values (386, ' 2010, 2011, 2012, 2013');

    insert into patient_visited values (232, ' 2004, 2006, 2013, 2014');

    insert into patient_visited values (511,'2004-2009, 2010, 2013, 2014');

    insert into patient_visited values (140, null);

    insert into patient_visited values (315,'2007-2013");

    PATIENT_ID YEAR_LIST

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

    478 2003,2004,2005,2008,2009,2010,2014

    572 2010,2011,2012,2013,2014

    386 2010,2011,2012,2013

    232 2004,2006,2013,2014

    511 2004-2009, 2010, 2013, 2014

    140

    315 2007-2013

    7 selected lines.

    Power required: (we want to remove the repetition of years if they continue to "-") and add the current year (assume it is 2015!)

    PATIENT_ID YEAR_LIST

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

    478 2003-2005, 2008-2010, 2014, 2015

    572 2010-2014, 2015

    386 2010-2013, 2015

    232 2004,2006,2013-2014,2015

    511 2004-2009, 2010, 2013-2014, 2015

    140 2015

    315 2007-2013, 2015

    Reasons:

    ID 478: Years ago continue from 2003 to 2005, and from 2008 to 2010 and 2014 and now added 2015

    ID 572: Continue the years 2010 to 2015 and now added 2015

    ID 386: Continue the years 2010 to 2013 and now added 2015

    ID 232: It not there no 2005, therefore 2004,2006 and continue the years 2013-2014 and 2015 has now added

    ID 511: Already continue year from 2004-2009, 2010, continue the year 2013,2014 so it should be 2013-2014

    ID 140: No year 2015 so simply

    ID 315: Already continue between 2007 and 2013, now added year 2015.

    In this table, we store just patient ID that comes to our hospital for treatment.  There's that break everything in the year while year should be listed under the separate name, otherwise he shall appear in continue with its predecessor.  In fact, year_list has fixed length and we want to store years as much as possible.  Next year above the table data should looks like this: (assume that all went to the hospital)

    PATIENT_ID YEAR_LIST

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

    478 2003-2005, 2008-2010, 2014-2015, 2016

    572 2010-2016

    386 2010-2013, 2015, 2016

    232 2004,2006,2013-2016

    511 2004-2009, 2010, 2013-2016

    140 2015,2016

    315 2007-2013, 2015, 2016

    Kindly help me.

    Thank you.

    And finally, to illustrate the results of the separate data, the previous method to view...

    SQL > ed
    A written file afiedt.buf

    1 with patient_visit as (select 140 as patient_id, null as visit_year of all the double union)
    2 Select 232,2004 of all the double union
    3 select 232,2006 of all the double union
    4 Select 232,2013 of all the double union
    5 Select 232,2014 of all the double union
    6 select 315,2007 of all the double union
    7 select 315,2008 of all the double union
    8 select 315,2009 of all the double union
    9 select 315,2010 of all the double union
    10. Select 315,2011 of all the double union
    11. Select 315,2012 of all the double union
    12. Select 315,2013 of all the double union
    13. Select 386,2010 of all the double union
    14 select 386,2011 of all the double union
    15 select 386,2012 of all the double union
    16. Select 386,2013 of all the double union
    17 select 478,2003 of all the double union
    18 select 478,2004 of all the double union
    19 select 478,2005 of all the double union
    20. Select 478,2008 of all the double union
    21 select 478,2009 of all the double union
    22 select 478,2010 of all the double union
    23 select 478,2014 of all the double union
    24 select 511,2004 of all the double union
    25 select 511,2005 of all the double union
    26 select 511,2006 of all the double union
    27 select 511,2007 of all the double union
    28 select 511,2008 of all the double union
    29 select 511,2009 of all the double union
    30 select 511,2010 of all the double union
    31 select 511,2013 of all the double union
    32 pick 511,2014 all the double union
    33 select 572,2010 of all the double union
    34 select 572,2011 of all the double union
    35 select 572,2012 of all the double union
    36 select 572,2013 of all the double union
    37 select double 572,2014
    38                        )
    39-
    40. end of test data
    41-
    42. now add current year to patients where they have not already curernt year
    43-
    44, as add_current_year)
    45 select patient_id, visit_year


    46 by patient_visit
    47 where visit_year is not null - must null visits
    48 union
    49 select distinct patient_id, extract (year from sysdate)
    patient_visit 50
    51            )
    52-
    53. now use tabibitosan method to create groups of the year
    54-
    55, yr_group as (select patient_id, visit_year
    56, row_number() over (partition by order of visit_year patient_id) - visit_year as yr_group
    add_current_year 57
    58                   )
    59-
    60. now get min and max years within the Group
    61-
    62, min_max_yr in (select distinct)
    63 patient_id
    64 min (visit_year) on (partition patient_id, yr_group) as min_yr
    65, max (visit_year) on (partition patient_id, yr_group) as max_yr
    66, dense_rank() over (partition by patient_id of yr_group desc order): the nurse
    yr_group 67
    68                     )
    69-
    70 now aggregate years to display purposes
    71-
    Select of 72 patient_id
    73, listagg(min_yr|| cas où min_yr = max_yr then else null '-'|| fin de max_yr, «,») Group (order by rn) visits
    74, of min_max_yr
    Group of 75 by patient_id
    76 * order by patient_id
    SQL > /.

    PATIENT_ID VISITS
    ---------- ----------------------------------------
    140 2014
    232 2004,2006,2013-2014
    315 2007-2014
    386 2010-2014
    478 2003-2005, 2008-2010, 2014
    511 2004-2010, 2013-2014
    572 2010-2014

    7 selected lines.

    Thus, by having your data stored correctly, when you need to add or delete to view information from your database, it is just a case insertion or deletion of rows in the table.  The display of this information can be manipulated, but you need; So where is it necessary to show in the style of 'Beach' you can use a method like this, but another 'report' or interface may require the data differently.

    Always try and separate the presentation of data from data storage.  Store the data in the most optimal way for a relational database and let the presentation of data to individual needs.

  • Pivot query help

    need help on creating pivot query

    SELECT * FROM TEST1

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

    VALUE OF PERSON COMPUTERNAME

    COMP1                    ABC                     3

    COMP2                    ABC                     5

    COMP1                    CAD                     3

    COMP3                    CAD                     5

    COMP2                    TES                      1

    COMP1                    TES                      5

    COMP3                    ABC                      2

    myQuery

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

    Select the link null, label, value1 COUNT (VALUE)

    from 'test1 '.

    CONTROL group PER PERSON

    Results

    ---------

    Link label value1

    -                     ABC                     3

    -                     CAD                     2

    -                     TES                      2

    My requirement

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

    can we have something like that out using the concept of pivot? If so can you share an example query pls.


    Link label value1

    -ABC ORDI1, COMP2, COMP3

    -CAD COMP1, COMP2

    -YOUR ORDI1, COMP3

    Hello

    Subhash C-Oracle wrote:

    need help on creating pivot query

    SELECT * FROM TEST1

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

    VALUE OF PERSON COMPUTERNAME

    COMP1                    ABC                    3

    COMP2                    ABC                    5

    COMP1                    CAD                    3

    COMP3                    CAD                    5

    COMP2                    TES                      1

    COMP1                    TES                      5

    COMP3                    ABC                      2

    myQuery

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

    Select the link null, label, value1 COUNT (VALUE)

    from 'test1 '.

    CONTROL group PER PERSON

    Results

    ---------

    Link label value1

    -                    ABC                    3

    -                    CAD                    2

    -                    TES                      2

    My requirement

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

    can we have something like that out using the concept of pivot? If so can you share an example query pls.

    Link label value1

    -ABC ORDI1, COMP2, COMP3

    -CAD COMP1, COMP2

    -YOUR ORDI1, COMP3

    This sounds like a job for LISTAGG:

    SELECT NULL AS link

    label

    LISTAGG (comp_name, ',')

    THE Group (ORDER BY ComputerName) AS value1

    OF test1

    GROUP BY label

    ;

    If you would care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.

    Are you sure that the results you posted are what you want from data provided?

    Is of the order of the elements in a significant list?  In other words, when you say you want to get the results:

    COMP1, COMP2

    you'd be just as happy with

    ORDI1, COMP2

    ?  If the order is important, explains what this order.

  • Kindly help for mentioning out with an SQL query, thanks

    Hello

    Please find below the input grid and help me get the result. Thanks in advance.  [Seeks assistance of query tunned]

    with the temp as

    (select alert_id 100, 111 double branch_code)

    Union of all the

    Select double 100 111

    Union of all the

    Select double 100 222

    Union of all the

    Select double 101 333

    Union of all the

    Select double 101 444

    Union of all the

    Select double 101 555

    Union of all the

    Select double 102 666

    Union of all the

    Select double 102 666

    Union of all the

    Select 102 666 double)

    Select * Temp;

    Entry
    Alert_IDBranch_code
    100111
    100111
    100222
    101333
    101444
    101555
    102666
    102666
    102666

    Output
    Alert_idBranch_code
    100111 222
    101333,444,555
    102666

    Thanks a ton

    Arpit

    Oracle 11 g 2, with analysis of the table only once. .. Here's how:

    WITH temp1

    AS (SELECT alert_id,

    branch_code,

    ROW_NUMBER)

    COURSES (PARTITION BY alert_id ORDER BY branch_code)

    DSB

    TEMP

    GROUP BY alert_id, branch_code)

    SELECT alert_id,

    LISTAGG (branch_code, ",") WITHIN GROUP (ORDER BY DSB) branch_code

    OF temp1

    GROUP BY alert_id;

    ALERT_ID BRANCH_CODE

    100 111 222

    101 333,444,555

    102 666

    See you soon,.

    Manik.

  • 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

  • Reg: WMSYS. WM_CONCAT or LISTAGG

    All,

    I'm trying to select column names that begin with "E" in my sql query projection area. For this I used USER_TAB_COLS with WMSYS. WM_CONCAT / LISTAGG too.

    I got the names of columns, with separator ", but could not inject it to the sql query. Please help me.

    SQL > SELECT WMSYS. WM_CONCAT (column_name)
    2 FROM user_tab_cols
    3 WHERE table_name = 'EMP '.
    4 AND column_name LIKE '% E ';

    WMSYS. WM_CONCAT (COLUMN_NAME)
    --------------------------------------------------------------------------------
    EMPNO, ENAME

    SQL > SELECT
    2. (SELECT WMSYS. WM_CONCAT (column_name)
    3 FROM user_tab_cols
    4 WHERE table_name = 'EMP '.
    5 AND COLUMN_NAME LIKE '% E '.
    (6) a
    7 FROM EMP where rownum < = 4;

    A
    --------------------------------------------------------------------------------
    EMPNO, ENAME
    ERROR:
    ORA-22922: non-existent LOB value

    -LISTAGG

    SQL > select (selection ListAgg (column_name, ',')
    2 IN THE GROUP (ORDER BY COLUMN_NAME DESC) AS CONCATV
    3 to user_tab_cols
    4. WHERE TABLE_NAME = 'EMP '.
    5 AND COLUMN_NAME LIKE '% E') a FROM EMP WHERE ROWNUM < = 3;

    A
    ----------------------------------------------------------------------------------------------------
    ENAME, EMPNO
    ENAME, EMPNO
    ENAME, EMPNO

    Thank you

    Here is an example of what you want to do:

    : c is of type cursor

    declare

    v varchar2 (4000);

    Start

    Select GROUP(ORDER BY COLUMN_NAME DESC) AS CONCATV ListAgg (column_name, ',')

    v

    of user_tab_cols

    WHERE TABLE_NAME = 'MYTABLE ';

    Open: c for 'select'. v. "from MYTABLE";

    end;

    ----

    Ramin Hashimzade

  • Listagg

    Hi all

    I see this example LISTAGG:
    SELECT deptno, LISTAGG(ename, ',') INTRA GROUP (ORDER BY ename) as employees FROM emp GROUP BY deptno;
    Can you help me convert my sql to this?

    Select group_concat (emp_name) from emp where dept_no = '10';

    Is this correct?
    Select LISTAGG(emp_name,',') INTRA GROUP (ORDER BY emp_name) emp where dept_no = '10';
    I can omit group by?

    Thank you...

    Select LISTAGG (emp_name, ',') IN GROUP (ORDER BY emp_name) from emp where dept_no = '10';

    I can omit group by?

    There is no GROUP of there is that ORDER BY. You need to order by oracle because they need to know in what order he will have to concat values. You can use different columns in ORDER BY. for example, if you want to concat the name in order of EMPNO, that you can use EMPNO in order.

  • need help for XMLAGG

    For this request below I need single quotes are required for each kintana_number

    and when I increase value < = 100 I get error as ORA-19011 how change above one get between apostrophes and avoid 0ra-19011
    Help, please

    SELECT DISTINCT RTRIM
    (XMLAGG
    (XMLELEMENT (e, p.kintana_number |)) ','). EXTRACT
    ('/ / text()')
    ),
    ','
    )
    PROJECT p where p.projectid < = 100


    Select
    listagg (kintana_number, ",") | " '
    WITHIN THE GROUP
    (ORDER BY kintana_number)
    Of
    project where the projectid < = 100
    I want each kintana_number singlequote after that each comma separated

    Published by: user13301356 on February 8, 2013 12:39 AM
    SELECT dbms_xmlgen.convert(
             rtrim(
               extract(
                 xmlagg(
                   xmlelement(e, ''''|| p.kintana_number || ''',')
                 )
               , '//text()'
               ).getclobval()
             , ','
             )
           , 1
           )
    FROM project p ;
    

    Or, on Oracle 11:

    SELECT rtrim(
             xmlcast(
               xmlagg(
                 xmlelement(e, ''''|| p.kintana_number || ''',')
               )
               as clob
             )
           , ','
           )
    FROM project p ;
    
  • Help returns the array of query results

    Hi all... Pretty new to this so please forgive my ignorance. Running Oracle 11 g, trying to return results as in the example I give below. I don't know how the Sport field will have unique values, and I don't know how many unique values the year field is seen in the data. Does anyone have recommendations/suggestions?

    The structure of the table is:
    Year of sport
    Baseball 10-11
    Football 10-11
    Football 11-12
    Football 11-12
    Football 11-12
    Baseball 11-12

    The result should be sports in each year and should be grouped by year but shown as columns so:
    (GROUP OF the year)
    1, baseball 1, Football
    2, 1, Football Soccer 1, Baseball


    I hope that makes sense. Thanks for any help!
    ... Kaelon

    Does anyone have recommendations/suggestions?

    Maybe something like:

    SQL> with t as (
     select 'Baseball' sport, '10-11' year from dual union all
     select 'Football', '10-11' from dual union all
     select 'Football', '11-12' from dual union all
     select 'Football', '11-12' from dual union all
     select 'Soccer',   '11-12' from dual union all
     select 'Baseball', '11-12' from dual
    )
    --
    --
    select year, listagg(cnt || ', ' || sport, ' ') within group (order by cnt desc) sports from (
    select count(*) cnt, sport, year from t
    group by sport, year)
    group by year
    /
    YEAR    SPORTS
    ------- ----------------------------------------
    10-11   1, Baseball 1, Football
    11-12   2, Football 1, Baseball 1, Soccer       
    
    2 rows selected.
    

    ?

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

  • concatenation of listagg with the boy?

    Hi people;

    I need to CONCATENATE interval of integers like this:
    1 - 1 = 1
    1-10, 2-3, 1-1 = 1-10
    1-10, 12-14 is 1-10, 12-14

    I have a global temporary table with a range (of type varchar2) by folder:
    RANGES
    1 - 10
    3 - 4
    1 - 1
    45 - 100
    I know that listagg (I'm on 11g):
    select LISTAGG(ranges, ', ') WITHIN GROUP (ORDER BY ranges) as a_validity_range from TMP_VALIDITY_RANGES;
    
    A_VALIDITY_RANGE                                                                                                                                                                                      
    -------------------------------------------------------------------------------------
    1 - 1, 1 - 10, 3 - 4, 45 - 100 
    I need to have the result: a_validity_range = 10-1, 45-100

    Please help me :)

    Published by: Moostiq on May 10, 2011 14:10

    Maybe something in that direction...

    SQL> ed
    Wrote file afiedt.buf
    
      1  with t as (select '1 - 10' as rng from dual union all
      2             select '3 - 4' from dual union all
      3             select '1 - 1' from dual union all
      4             select '45 - 100' from dual)
      5  --
      6  -- end of test data
      7  --
      8      --
      9      -- determine lo and hi values from each range
     10      --
     11      ,x as (select rng, to_number(regexp_substr(rng,'^[0-9]+')) lo, to_number(regexp_substr(rng,'[0-9]+$')) hi from t)
     12      --
     13      -- generate all lists of numbers from each range
     14      --
     15      ,y as (select rng, lo+rn-1 as nm
     16             from   x cross join (select rownum rn from dual connect by rownum <= (select max(hi-lo)+1 from x))
     17             where lo+rn-1 <= hi)
     18      --
     19      -- roll up the new ranges
     20      --
     21      ,z as (select min(nm)||' - '||max(nm) rng
     22             from (select distinct nm from y order by nm)
     23             group by nm-rownum
     24             order by 1)
     25  --
     26  -- finally concatenate the ranges together
     27  --
     28  select ltrim(sys_connect_by_path(rng,', '),', ') as ranges
     29  from   (select rng, rownum as rn from z)
     30  where connect_by_isleaf = 1
     31  connect by rn = prior rn + 1
     32* start with rn = 1
    SQL> /
    
    RANGES
    --------------------------------------------------------------------------------------------------------------------------
    1 - 10, 45 - 100
    
    SQL>
    

Maybe you are looking for

  • Firefox crashes on startup - occurs every time you start

    It happens on every attempt to start. I tried too private browsing mode, it crashes as well. SAFE MODE CRASH-ID: bp-478a6514-09f7-474f-a560-1fd222120221

  • HP Deskjet F4224 scan to PDF does not work

    Windows XP operating system. I want to scan a document and have a power of pdf. Menu of the printer does not offer this. Limits of output TIF file. I want much less space resulting output pdf. I have used other printers and they offered the alternati

  • Can I connect to wifi using an older version of XP pro?

    Hi, I tried to connect to a wifi system library with my Windows XP pro system. The OS is the drive immediately and has not been updated. It does not seem an option in "Network connections" for the wireless, but it has broadband services. I tried to c

  • printer offline error

    Remember - this is a public forum so never post private information such as numbers of mail or telephone! Ideas: reset printer? You have problems with programs Error messages Recent changes to your computer What you have already tried to solve the pr

  • Connecting the projector cuts out after 10 minutes

    Hello I scoured internet help forums looking for answers, and nobody seems to have the same problem I encounter. Here goes: Details Laptop - Lenovo IdeaPad N585 OS - Windows 7 Home Premium Projector - Samsung LCD TV series 5 Connection - cable PC mon