Split multiple columns into rows using XML

Hi Forum,
I am trying to split 2 columns that each contain values separated by semicolon into single rows. The relation between the values of the two columns is that the order in the cells corresponds to each other.
The data looks like this:
pk    Manufacturer                partnumber
1     Man1; Man2;Man3      PN1;PN2;PN3
2     Man4; Man2;Man5      PN4;PN5;PN6
The result should be:
pk    Manufacturer     partnumber
1       Man1                   PN1
1       Man2                   PN2
1       Man3                   PN3
2       Man4                   PN4
2       Man2                   PN5
2       Man5                   PN6
I am not sure how to format the XML to get a useful Basis for XML.value or XML.query
Any ideas?
TIA
Alex

Hi,
Try like this ,
DECLARE @tmp TABLE (pk INT,Manufacturer NVARCHAR(50),partnumber NVARCHAR(50))
INSERT @tmp SELECT 1,'Man1; Man2;Man3','PN1;PN2;PN3'
INSERT @tmp SELECT 2,'Man4; Man2;Man5','PN4;PN5;PN6'
SELECT * FROM @tmp
SELECT tmp2.pk pk,Manufacturer,partnumber FROM (
SELECT ROW_NUMBER()OVER(ORDER BY tmp1.pk) RN,* FROM (
SELECT pk,
LTRIM(i.value('.','varchar(100)')) Manufacturer
FROM ( SELECT pk, Manufacturer,
CONVERT(XML,'<r><n>'
+ REPLACE(Manufacturer,';', '</n><n>') + '</n></r>') AS X
FROM @Tmp) Spt
CROSS APPLY Spt.X.nodes('//*[text()]') x(i)
) tmp1 ) tmp2
JOIN
(SELECT ROW_NUMBER()OVER(ORDER BY pk) RN,* FROM (
SELECT pk,
j.value('.','varchar(100)') partnumber
FROM ( SELECT pk, partnumber,
CONVERT(XML,'<r><n>'
+ REPLACE(partnumber,';', '</n><n>') + '</n></r>') AS Y
FROM @Tmp) Spt
CROSS APPLY Spt.Y.nodes('//*[text()]') y(j)) tmp2 ) tmp3 ON tmp3.RN = tmp2.RN
sathya - www.allaboutmssql.com ** Mark as answered if my post solved your problem and Vote as helpful if my post was useful **.

Similar Messages

  • How to convert columns into rows using  transpose function

    Hi
    anybody tell me how to convert columns values into rows using transpose function.

    Since BluShadow went to all the trouble to put it together, someone should use it.
    See the post titled How do I convert rows to columns? here SQL and PL/SQL FAQ
    John

  • Inserting multiple columns into database using dbtool set

    I have  a labVIEW program that is currenly simulating voltage and a current signal using the DAQ Assistant. I'm using the database connectivity toolset. I am currently able to insert one signal into the database converted to a float(I can insert both into the same column as bianary but it needs to be stored as a usable format and in seperate columns.)
    The problem I am having is at the DB Tools Insert Data block. I have the connection, dbTable, error code, and create table boolean being passed into it. Where I am a little lost is how to set the columns I want to store the multiple signals into.
    If someone could break that part down for me that would be great. In short, I want to store voltage(channel0) and current(channel1) from the DAQ Assistant into their own respective columns in the db.
    Message Edited by DonPoulson on 07-10-2007 11:04 AM

    Don,
    This particular forum is specific to TestStand, not LabVIEW or Multifunction DAQ. You may have a better chance at responses by posting to either of those forums rather than here.
    Brandon Vasquez | Software Engineer | Integration Services | National Instruments

  • Mild challenge -pivoting *multiple* columns per row using only SQL

    Hello All,
    I'm in the process of learning the various pivoting techniques available
    in SQL, and I am becoming more familiar with the decode,function,group-by
    technique seen in many examples on these forums. However, I've got a case
    where I need to pivot out 3 different columns for 3 rows of data where the
    value of a different column is driving whether or not those columns are pivoted.
    I know that last sentence was as clear as mud so I'll show you/provide the simple
    scripts and data, and then I'll elaborate a little more beneath.
    create table temp_timeline (
    mkt_id varchar2(10),
    event_id number(8),
    event_type varchar2(3),
    mod_due_date date,
    cur_due_date date,
    act_due_date date
    insert into temp_timeline values('DSIM6',51,'S1','NOV-13-06','NOV-13-06',NULL);
    insert into temp_timeline values('DSIM6',51,'S2','DEC-20-06','DEC-20-06',NULL);
    insert into temp_timeline values('DSIM6',51,'S3','JAN-17-07','JAN-17-07',NULL);
    insert into temp_timeline values('DSIM6',51,'S4','FEB-14-07','FEB-14-07',NULL);
    commit;
    select * from temp_timeline;
    The "normal" output (formatted with period-separated fields) is:
    DSIM6.51.S1.NOV-13-06.NOV-13-06.NULL
    DSIM6.51.S2.DEC-20-06.DEC-20-06.NULL
    DSIM6.51.S3.JAN-17-07.JAN-17-07.NULL
    DSIM6.51.S4.FEB-14-07.FEB-14-07.NULL
    The DESIRED 1-row output (formatted with period-separated fields) is:
    DSIM6.51.NOV-13-06.NOV-13-06.NULL.DEC-20-06.DEC-20-06.NULL.JAN-17-07.JAN-17-07.NULL.FEB-14-07.FEB-14-07.NULL
    So, the first 2 columns in the table have the same data, and the third column
    makes the row unique (they could all have the same/similar dates).
    If this table only consisted of the first 3 columns then many of the examples seen
    on this forum would work well (grouping by the first 2 rows and pivoting out
    the "event_type" columns containing (S1,S2,S3,S4) etc.
    But, in my case, I need to discard the event_type column and pivot out the
    3 columns of date data onto the first row (for each different event_type).
    So the 3 Dates associated with the "S2" column would go to the first row, and the
    3 dates associated with the "S3" column would also go to the first row (and so on).
    The 3 dates need to be 3 distinct columns when they are
    pivoted out (not concatenated to each other and pivoted as one column).
    Given this, I will need to pivot out a total of 12 different columns for each distinct
    (mkt_id, event_id) pair.
    For the time being I have accomplished this with a union, but am trying to expand
    my abilities with other sql methods. I've seen some very elegant solutions on this
    forum so will be interested to see what others can come up with for this solution.
    Thanks in advance for any comments you may provide.

    Just DECODE based on the event type, which will generate your 12 columns.
    SELECT mkt_id, event_id,
           MAX(DECODE(event_type, 'S1', mod_due_date, NULL)) s1_mod_due,
           MAX(DECODE(event_type, 'S1', cur_due_date, NULL)) s1_cur_due,
           MAX(DECODE(event_type, 'S1', act_due_date, NULL)) s1_act_due,
           MAX(DECODE(event_type, 'S2', mod_due_date, NULL)) s2_mod_due,
           MAX(DECODE(event_type, 'S2', cur_due_date, NULL)) s2_cur_due,
           MAX(DECODE(event_type, 'S2', act_due_date, NULL)) s2_act_due,
           MAX(DECODE(event_type, 'S3', mod_due_date, NULL)) s3_mod_due,
           MAX(DECODE(event_type, 'S3', cur_due_date, NULL)) s3_cur_due,
           MAX(DECODE(event_type, 'S3', act_due_date, NULL)) s3_act_due,
           MAX(DECODE(event_type, 'S4', mod_due_date, NULL)) s4_mod_due,
           MAX(DECODE(event_type, 'S4', cur_due_date, NULL)) s4_cur_due,
           MAX(DECODE(event_type, 'S4', act_due_date, NULL)) s4_act_due
    FROM temp_timeline
    GROUP BY mkt_id, event_idTested, because you supplied create table and insert statements, thank you.
    John

  • Transporting columns into rows using SSIS

    Hi everyone,
    Just need some help.
    I wanna transpose a data table in SSIS.
    The table is originally 
    CUSTREFNO
    Citizen
    FORENAME
    Citizen
    SURNAME
    Citizen
    DATE-OF-BIRTH
    Citizen
    TEL_NO
    Citizen
    GUARDIANNAME
    Citizen
    ADDRESS1
    Citizen
    ADDRESS2
    Citizen
    ADDRESS3
    Citizen
    ADDRESS4
    Citizen
    POSTTOWN
    Citizen
    POSTCODE
    Citizen
    OPERATOR
    Junk
    BOARDINGPOINT
    Junk
    DESTINATION
    Junk
    SERVICE_NO
    Junk
    EMAIL
    Citizen
    And i need the table in the following format
    CUSTREFNO
    FORENAME
    SURNAME
    DATE-OF-BIRTH
    TEL_NO
    GUARDIANNAME
    ADDRESS1
    ADDRESS2
    ADDRESS3
    ADDRESS4
    POSTTOWN
    POSTCODE
    OPERATOR
    BOARDINGPOINT
    DESTINATION
    SERVICE_NO
    EMAIL
    Citizen
    Citizen
    Citizen
    Citizen
    Citizen
    Citizen
    Citizen
    Citizen
    Citizen
    Citizen
    Citizen
    Citizen
    Junk
    Junk
    Junk
    Junk
    Citizen
    Any help, would be very much appreciated, thanks

    Umar, please see these resources.
    http://blogs.msdn.com/b/philoj/archive/2007/11/10/transposing-rows-and-columns-in-sql-server-integration-services.aspx
    http://www.rad.pasfu.com/index.php?/archives/14-PIVOT-Transformation-SSIS-Complete-Tutorial.html
    http://dinesql.blogspot.in/2011/08/pivot-and-unpivot-integration-services.html
    http://sqlage.blogspot.in/2013/12/ssis-how-to-use-unpivot-in-ssis.html
    http://www.bimonkey.com/2009/06/the-pivot-transformation/
    http://www.sql-server-performance.com/2007/ssis-pivot-unpivot/
    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

  • Change multiple column to rows using unpivot or pivot

    is there any way I could use pivot or unpivot to get the result show in the graph? From the top table structure to the lower table structure. I could do activity hours and desc separately using unpivot but do not know how to join them together. 

    Looks like a pivot isn't the best bet, try this:
    DECLARE @ActivityReport TABLE ([Employee_ID] [int] NULL, [Week] nvarchar(50) NULL, [Hours_Admin] [float] NULL, [Desc_Admin] [nvarchar](200) NULL, [Hours_HelpDesk] [float] NULL, [Desc_HelpDesk] [nvarchar](200) NULL, [Hours_Support] [float] NULL, [Desc_Support] [nvarchar](200) NULL)
    insert into @ActivityReport VALUES (1, 'week1', 5, 'desc', 6, 'desc', 9, 'desc'), (1, 'week2', 5, 'desc1', 6, 'desc1', 9, 'desc1'), (2, 'week1', 5, 'desc', 6, 'desc', 9, 'desc'), (2, 'week2', 5, 'desc2', 6, 'desc2', 9, 'desc2')
    SELECT employee_ID, week, hours_admin AS activityHours, 'admin_hours' AS activity_type
    FROM @ActivityReport
    UNION ALL
    SELECT employee_ID, week, hours_helpdesk AS activityHours, 'helpdesk_hours' AS activity_type
    FROM @ActivityReport
    UNION ALL
    SELECT employee_ID, week, hours_support AS activityHours, 'support_hours' AS activity_type
    FROM @ActivityReport
    ORDER BY Employee_ID, week
    Don't forget to mark helpful posts, and answers. It helps others to find relevant posts to the same question.

  • Splitting one column into 4 columns

    Hi,
      I want to split one column into 4 columns as shown below.
    parameters
    PanelPanel=MP01110201&YearYear=2013&Source=1&UserID=aad2779
    PanelPanel=MP12100173&YearYear=2013&Source=1&UserID=aac6440
    it should be display as 
    panel                      yearyear                source              userid
    MP01110201           2013                            1                    aad2779
    MP12100173           2013                            1                    aac6440
    there will be thousands of rows in the column. Can anybody help how to split it as shown.The length may very from row to row
    Thanks for your help.........
    BALUSUSRIHARSHA

    It is working...thank u very much... I found one more issue here
    PanelPanel=MP01110201&YearYear=2013&Source=1&UserID=aad2779
    PanelPanel=MP11100070&Source=1&PNR=2&YearYear=2014&UserID=ddc1535
    PanelPanel=MP11101276&Source=1&YearYear=2014&PNR=2&UserID=ddc1565
    I found 3 kinds of formats in the same column... I didn't observe the data carefully while posting the
    question..sorry about that. In this case if we need to show as
    panel                      yearyear                source
                 userid
    MP01110201           2013                            1                    aad2779
    MP11100070           2014                           1                    ddc1535
    MP11101276           2014                
              1                    ddc1565
    is it possible to filter like this? Should we use any case statement in query while we have diff formats
    like this?
    BALUSUSRIHARSHA

  • Converting multiple columns into one.

    All,
    I have a requirement to convert multiple columns into one. Following is the pseudo code of current implementation. It unions the various columns ( col1, col2........) into a new column new_col. But perforamnce is extrmely slow owing to multiple unions. It may be noted that tables and where conditions of all these queries are same.
    Can you help me create a more efficient query?
    select col1 , col2, col3, col4 from my_tables where some_cols = my_condition;Below is the query used to convert these columns into one.
    select col1 new_col from my_tables where some_cols = my_condition
    union all
    select col2 from my_tables where some_cols = my_condition
    union all
    select col3 from my_tables where some_cols = my_condition
    union all
    select col4 from my_tables where some_cols = my_condition

    Without looking at those things you could be barking up the wrong tree by assuming the issue relates to the unioning of queries or other such things.Well, might be.........
    Execution time of this query (just the execution time not the retrival) is around 34 seconds with connect-by clause for returning 22,000 records.
    Here's the plan
    Execution Plan
    | Id  | Operation                          | Name                        | Rows
    | Bytes | Cost (%CPU)|
    |   0 | SELECT STATEMENT                   |                             |   326
    | 33904 |   135   (2)|
    |   1 |  SORT ORDER BY                     |                             |   326
    | 33904 |   135   (2)|
    |   2 |   MAT_VIEW ACCESS BY INDEX ROWID   | MV_COMQ_RM_DEAL             |     1
    |    62 |     1   (0)|
    |   3 |    NESTED LOOPS                    |                             |   326
    | 33904 |   134   (1)|
    |   4 |     MERGE JOIN CARTESIAN           |                             |   326
    | 13692 |     3   (0)|
    |   5 |      VIEW                          |                             |     1
    |    13 |     2   (0)|
    |   6 |       COUNT                        |                             |
    |       |            |
    |   7 |        CONNECT BY WITHOUT FILTERING|                             |
    |       |            |
    |   8 |         FAST DUAL                  |                             |     1
    |       |     2   (0)|
    |   9 |      BUFFER SORT                   |                             |   326
    |  9454 |     3   (0)|
    |  10 |       TABLE ACCESS BY INDEX ROWID  | SF_SEARCH_IDS_TMP_COMQ      |   326
    |  9454 |     1   (0)|
    |  11 |        INDEX RANGE SCAN            | IDX1_SF_SEARCH_IDS_TMP_COMQ |   349
    |       |     1   (0)|
    |  12 |     INDEX RANGE SCAN               | IDX_MV_COMQ_RM_DEAL         |     9
    |       |     1   (0)|
    -----------------------     Hope, It's readable.......
    I would have posted sample data and query but........I cant do that...:(

  • Displaying data from multiple columns into a single line graph

    Post Author: hollowmatrix
    CA Forum: WebIntelligence Reporting
    Hey,I have an issue with the WEBI reporting.I have a data source that has multiple columns say ( month1, month2, month3, month4,.....month 12, month 13, ....month24) with the sales data for each month.Now say I call the month 1 to month 12 as "current year", and call month 13 - month 24 as "previous year".I want to put a prompt in the report which allows  me to select between "current year" and "previous year".Based on the prompt value we get a graph of the sales vs month ....as in if we select  "current year", then we get a graph of the sales Vs time( remember that the sales data for each month is in a different column.)and if we select  "previous year" then we get a graph of the sales Vs time for previous year..( sales vs time for Month 13, month 14, month 15....month 24).I am not able to pull data from multiple columns into a single object that I can use to populate the graphs.Any help on the same will be appreciated .   

    Hi,
    <p>
    please click
    here (asktom) and look for the words "how about the other way round"
    </p>

  • Problem in displaying the data of columns into rows in sap script

    hi,
    i am working on a sap script and i have to display the dat which is displayed in column into rows but it is not displaying it properly.
    eg, C
        12.1
        Si
        5.5
    it is displaying the data right now like this but i want to display the  data like this:-
    eg, C      Si
        12.1   5.5
    plzzprovide me guidelines how to solve this problem.

    hi,
    i am using this code to display the data:-
    plzz provide me guidelines where i am getting wrong?
    TOPparCOMPONENT DESP,,,,,, INS. LOT #, , , , , , MIC,,,,,,,,,, MIC VALUEparENDTOPparFINAL
    PROTECT
    IF &I_FINAL-PRUEFLOS& NE '000000000000'
    &I_FINAL-MAKTX(23)&&i_final-prueflos(12Z)&
    &I_FINAL-kurztext(25)&
    &I_FINAL-original_input(8)&
    ELSE
    &I_FINAL-MAKTX(23)&     
    &I_FINAL-kurztext(25)&
    &I_FINAL-original_input(8)&
    ENDIF
    ENDPROTECT
    ITEMHEAD
    POSITION WINDOW
    SIZE WIDTH +0 . 4 CH HEIGHT +1 LN
    BOX FRAME 10 TW
    BOX HEIGHT '1.35' LN INTENSITY 20
    IF &PAGE& = '1'
    BOX XPOS '0' CH YPOS '0' CM WIDTH '0' CM HEIGHT '43' LN FRAME '10' TW
    For horizontal line at top
    BOX XPOS '0' CH YPOS '0' CM WIDTH '75' CH HEIGHT '0' LN FRAME '10' TW
    COLUMN LINES...
    END OF COLUMN LINES...
    BOX XPOS '0' CH YPOS '43' LN WIDTH '75' CH HEIGHT '0' LN FRAME '10'TW
    BOX XPOS '75' CH YPOS '0' LN WIDTH '0' CH HEIGHT '43' LN FRAME '10'TW
    ELSE
    COLUMN LINES...
    END OF COLUMN LINES...
    BOX XPOS '0' CH YPOS '0' CM WIDTH '0' CM HEIGHT '47' LN FRAME '10' TW
    BOX XPOS '0' CH YPOS '0' CM WIDTH '75' CH HEIGHT '0' LN FRAME '10' TW
    BOX XPOS '0' CH YPOS '0' CM WIDTH '45' CM HEIGHT '0' LN FRAME '10' TW
    BOX XPOS '20' CH YPOS '0' CM WIDTH '0' CM HEIGHT '47' LN FRAME '10' TW
    BOX XPOS '0' CH YPOS '47' LN WIDTH '75' CH HEIGHT '0' LN FRAME '10'TW
    BOX XPOS '75' CH YPOS '0' LN WIDTH '0' CH HEIGHT '47' LN FRAME '10'TW
    ENDIF
    LINEFEED
    NEWPAGE
    NEW-PAGE
    provide me guidelines to solve this problem.
    Edited by: ricx .s on Mar 13, 2009 5:58 AM

  • How to display the rows in to columns and columns into rows?

    DES:- I know by using pivot and unpivot you can convert rows into columns and columns into rows but i don't know how to write the syntax?

    Hi,
    Besides the places Martin mentioned above, there are many examples in the forum FAQ:
    https://community.oracle.com/message/9362005#9362005
    For an example of both PIVOT and UNPIVOT in the same query, see
    http://forums.oracle.com/forums/thread.jspa?threadID=920854&tstart=0

  • Transpose columns into rows

    hi ,
    i need to transpose columns into rows ,
    i know i can use the UNION ALL but my num of columns will most likely not be fixed so how can i do that ?
    pls advise

    This is from one of the forms link,, i reallyy dont know the link, but i guess this is "adrains" code
    SQL> WITH ilv AS (
      2      SELECT str || ','                                   AS str
      3      ,     (LENGTH(str) - LENGTH(REPLACE(str, ','))) + 1 AS no_of_elements
      4      FROM   t
      5      )
      6  SELECT RTRIM(str, ',')                              AS original_string
      7  ,      SUBSTR(str, start_pos, (next_pos-start_pos)) AS single_element
      8  ,      element_no
      9  FROM  (
    10         SELECT ilv.str
    11         ,      nt.column_value AS element_no
    12         ,      INSTR(
    13                   ilv.str,
    14                   ',',
    15                   DECODE(nt.column_value, 1, 0, 1),
    16                   DECODE(nt.column_value, 1, 1, nt.column_value-1)) + 1 AS start_pos
    17         ,      INSTR(
    18                   ilv.str,
    19                   ',',
    20                   1,
    21                   DECODE(nt.column_value, 1, 1, nt.column_value)) AS next_pos
    22         FROM   ilv
    23         ,      TABLE(
    24                   CAST(
    25                      MULTISET(
    26                         SELECT ROWNUM FROM dual CONNECT BY ROWNUM < ilv.no_of_elements
    27                         ) AS number_ntt )) nt
    28        );
    ORIGINAL_STRING                 SINGLE_ELEMENT  ELEMENT_NO
    X,Y,Z                           X                        1
    X,Y,Z                           Y                        2
    X,Y,Z                           Z                        3
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  XXX                      1
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  Y                        2
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  ZZ                       3
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  AAAAA                    4
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  B                        5
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  CCC                      6
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  D                        7
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  E                        8
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  F                        9
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  GGG                     10
    13 rows selected.
    Note that the above SQL performs the following steps:
        * determines how many elements are in each string (WITH clause);
        * for each string, generates a collection of n elements (TABLE expression), where n is the derived number of elements in the string. Note in particular the use of "less than" in the "CONNECT BY ROWNUM < ilv.no_of_elements" on line 26. In all versions other than 10.1.x, this will need to be "CONNECT BY ROWNUM <= ilv.no_of_elements" (i.e. "less than or equal to"). There is an unusual bug with this row-generation technique in 10.1 that generates an additional row from the CONNECT BY;
        * uses the generated rows in a Cartesian Product with the original data to generate n rows per string, based on the above definition of n;
        * calculates the start and end position of each element in each string (INSTR); and
        * cuts each element from each string (SUBSTR).

  • Splitting one column into different columns.

    Hello Experts,
    How do i split datetime column into different columns while doing a Select statement.
    Ex:
    The column "REC_CRT_TS" has data like "2014-05-08 08:23:09.0000000".The datatype of this column is "DateTime". And i want it in SELECT statement like;
    SELECT
    YEAR(DATETIME) YEAR,
    MONTH(DATETIME) MONTH,
    DATENAME(DATETIME) MONTHNAME,
    DATEPART(DATETIME) WEEKNUM,
    DAY(DATETIME) DATE,
    DATEPART(DATETIME) HOUR
    FROM TABLE_NAME;
    The output should look like this;
    --YEAR| MONTH | MONTHNAME| WEEKNUM | DATE | HOUR
    --2014| 5 | May | 25 | 08 |08
    Any suggestions please.
    Thanks!
    Rahman

    I made a very quick research and I see in this blog post
    http://www.jamesserra.com/archive/2011/08/microsoft-sql-server-parallel-data-warehouse-pdw-explained/
    that  It also uses its own query engine and not all features of SQL
    Server are supported.  So, you might not be able to use all your DBA tricks.  And you wouldn’t want to build a solution against SQL Server and then just hope to upsize it to Parallel Data Warehouse Edition.
    So, it is quite possible that this function doesn't exist in PDW version of SQL
    Server. In this case you may want to implement case based month name or do it in the client application.
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • Concatenate multiple columns into one string

    Hello,
    I am using Oracle 11.2, how can I concatenate the value of multiple columns into one string with one SQL:
    create table testTb(classId number(5), classRoom varchar2(32));
    insert into testTb value(101, 'room101');
    insert into testTb value(101, 'room201');
    insert into testTb value(101, 'room301');
    insert into testTb value(202, 'room444');
    insert into testTb value(202, 'room555');
    I would like to generate the result as followings:
    Class 101 is in room101, room201, room301
    Class 202 is in room444, room555
    Thanks,

    Hi,
    Since you're using Oracle 11.2, you can use the aggregate LISTAGG function:
    SELECT       'Class ' || classid
                   || ' is in '
                 || LISTAGG ( classroom
                         ) WITHIN GROUP (ORDER BY classroom)
                   AS txt
    FROM       testtb
    GROUP BY  classid
    ;The generic name for concatenating all the strings in a group is String Aggregation . This page shows several ways to do it, suitable for different versions of Oracle.

  • One Column into Rows

    I have data in a table that looks like below:
    ColumnA               ColumnB
    123                    abc|cde|fgr
    345                    def|ght|sew
    890                    deq|nmk|lop|lip|fre|dwsThere is no limit on how many values you can have in ColumnB and they are pipe delimited.
    I need to split this one column into rows as:
    ColumnA               ColumnB
    123                    abc
    123                    cde
    123                    fgr
    345                    def
    345                    ght
    890                    fre
    890                    dwsThanks in advance!

    with sample_data as (
                         select 123 columna,'abc|cde|fgr' columnb from dual union all
                         select 345,'def|ght|sew' from dual union all
                         select 890,'deq|nmk|lop|lip|fre|dws' from dual
    select  columna,
            regexp_substr(columnb,'[^|]+',1,column_value) columnb
      from  sample_data,
            table(
                  cast(
                       multiset(
                                select  level
                                  from  dual
                                  connect by level <= length(regexp_replace(columnb || '|','[^|]'))
                       as sys.OdciNumberList
      order by columna,
               column_value
       COLUMNA COLUMNB
           123 abc
           123 cde
           123 fgr
           345 def
           345 ght
           345 sew
           890 deq
           890 nmk
           890 lop
           890 lip
           890 fre
       COLUMNA COLUMNB
           890 dws
    12 rows selected.
    SQL> SY.

Maybe you are looking for

  • Oracle Version fro ECC 6.0

    Hi Gurus ,.              We are planning to install ECC 6.0 on Windows 2003 server x64 Edition with Oracle 10.2 . In our media kid , Oracle DVD contains AMD64 , I386 and IA64 , But i have one doubt which one i have to select for the installation . Ki

  • Lock(Enque/Deque) is a logical lock & database lock

    Hi All, I have gone through various portals and SDN too, and found that locks are the logical locks and sometimes database locks too but still have some confusion. 1) If these are the logical locks then why they are created by SE11 and once created t

  • An invalid character was found in text content. (msxml6.dll)

    while executing or saving the SSIS package we get this error... =================================== Failure saving package. (Microsoft Visual Studio) Program Location:    at Microsoft.DataTransformationServices.Design.Serialization.DtrDesignerSeriali

  • Picture choice for Sunflower theme

    Hello, With the Sunflower theme, when the DVD starts playing there is a picture inside the sunflowers that pass by until one gets to the main menu. I made a movie yesterday and it put the first photo from the Slideshow photos in that flower. Looked g

  • Charge Nomad Juke Zen Xtra with US

    Quick question, does anyone know if it's possible to charge a Nomad Zen Xtra (a few years old) with a USB 2.0 connection? I've tried connecting it, but it doesn't seem to charge like other portable USB devices I have do. Maybe there is software/ driv