Display rows into multiple columns

I have a table in the following format:
id value
a1 0
a2 0
a3 0
a4 0
b1 0
b2 0
b3 0
b4 0
how do I go about displaying it into four columns in the following format:
id1 value1 id2 value2
a1 0 b1 0
a2 0 b2 0
a3 0 b3 0
a4 0 b4 0

Hi,
789063 wrote:
I got that error in toad. So now I am testing from command prompt. When you say "command prompt", do you mean SQL*Plus?
Sorry, I don't know much about Toad. If Toad is returning 0 rows, and the same query is returning 8 rows in SQL*Plus, then I suspect you are not connecting to the same database or the same user. ASre you sure you're running the exact same query in both front ends?
Here is the data set that I am testing for:
<pre>
id1 value
smple_val_1 0
smple_val_2 1
smple_val_3 1
smple_val_4 2
smple_other_1 3
smple_other_2 5
smple_other_3 8
smple_other_4 13
some_other_val1 1
some_other_val2 0
some_other_val3 3
some_other_val4 5
</pre>
I am trying to select only 'smple%' in the query, which should display in four columns like:
<pre>
id1 val1 id2 val2
smple_val_1 1 smple_other_1 3
smple_val_2 0 smple_other_2 5
smple_val_3 0 smple_other_3 8
smple_val_4 2 smple_other_4 13
</pre>Is that really the output you want? Why not
ID1             VAL1 ID2             VAL2
smple_val_1        0 smple_other_1      3
smple_val_2        1 smple_other_2      5
smple_val_3        1 smple_other_3      8
smple_val_4        2 smple_other_4     13with the 0's and 1's in the val1 column reversed?
Here is the ddl I used:
<pre>
CREATE TABLE     table_x
( id     VARCHAR2 (20)     PRIMARY KEY
, value     NUMBER
INSERT INTO table_x (id, value) VALUES ('smple_val_1', 0);
INSERT INTO table_x (id, value) VALUES ('smple_val_2', 1);
INSERT INTO table_x (id, value) VALUES ('smple_val_3', 1);
INSERT INTO table_x (id, value) VALUES ('smple_val_4', 2);
INSERT INTO table_x (id, value) VALUES ('smple_other_1', 3);
INSERT INTO table_x (id, value) VALUES ('smple_other_2', 5);
INSERT INTO table_x (id, value) VALUES ('smple_other_3', 8);
INSERT INTO table_x (id, value) VALUES ('smple_other_4', 13);
INSERT INTO table_x (id, value) VALUES ('some_other_val1', 1);
INSERT INTO table_x (id, value) VALUES ('some_other_val2', 0);
INSERT INTO table_x (id, value) VALUES ('some_other_val3', 3);
INSERT INTO table_x (id, value) VALUES ('some_other_val4', 5);
</pre>Thanks, that's good.
Where is the explanation of how you get the results you want from that data? There are a lot of different ways to get the same results from the same data, especially from a small set of sample data. I might provide a query that gets the right results for the wrong reasons, and when you run that query on a different set of data, it won't necessarily work. That's exactly what happened before. I guessed at what you wanted, but I guessed wrong. Guessing is not a very efficient or reliable way to solve problems. Don't make people guess: explain what you want.
In particular, explain how you can tell, buy looking at a row and column in the original table, to which row and which column of the output it corresponds. I'm pretty sure that the id column of the table always corresponds to either the id1 or idl2 column of the output, and that value always corresponds to either val1 or val2. From this latest set of sample data, it looks like all the rows in table_x that start with 'smple_val' will correspond to the id1 and val1 columns, and that all therows that start with 'smple_other_' will correspond to the id2 and val2 columns. It also looks like each row of the output represents a unique value of the last character of table_x.id.
If I guessed right this time, then what you want is:
SELECT       MIN (CASE WHEN id LIKE 'smple\_val\_%'   ESCAPE '\' THEN id    END)     AS id1
,       MIN (CASE WHEN id LIKE 'smple\_val\_%'   ESCAPE '\' THEN value END)     AS val1
,       MIN (CASE WHEN id LIKE 'smple\_other\_%' ESCAPE '\' THEN id    END)     AS id2
,       MIN (CASE WHEN id LIKE 'smple\_other\_%' ESCAPE '\' THEN value END)     AS val2
FROM       table_x
GROUP BY  SUBSTR (id, -1)
ORDER BY  SUBSTR (id, -1)
;But this is only a guess. I can't guarrantee that this will work on your real data any better than my last guess did. You'll get better answers faster if you provide an explanation, so that poeple don't have to guess so much.

Similar Messages

  • How to transpose rows into multiple columns using pivot table

    I have 1 row containing 12 columns with value "JAN", "FEB", "MAR", "J-1","F-1","M-1","J-2","F-2","M-2","J-3","F-3","M-3"
    I want to display as
    JAN J-1 F-1 M-1
    FEB J-2 F-2 M-2
    MAR J-3 F-3 M-3
    How do I achieve the above?

    Today you have only 3 months JAN, FEB, MAR. Is it always the same number of columns. What if there are more months added to this row?
    Is your data really coming from relational source or some sort of text file?
    There is a better way to do this in narrative view using HTML if your requirement is just to show them in multiple rows and do some calculations.
    Go to Narrative View;
    In prefix, use <html> <table>
    In 'Narrative' text box add something like this
    <tr> <td> @1 </td> <td> @4 </td> <td> @7 </td> </tr>
    <tr> <td> @2 </td> <td> @5 </td> <td> @8 </td> </tr>
    <tr> <td> @3 </td> <td> @6 </td> <td> @9 </td> </tr>
    In Suffix, use </table> </html>
    You can also add simple calculations like sum etc at the very bottom of these rows as grand totals.
    kris

  • Split one row into multiple columns

    Hi,
    Data in one CLOB column in a table storing with delimiter, ##~~##. Ex. ##~~##abc##~~##defgh##~~##ijklm##~~##nopqr (data starts with delimiter). Please help me to split the data into multiple rows like below and it should be in the same order.
    abc
    defgh
    ijklm
    nopqr
    I am using Oracle 11g.
    Thanks.

    Thanks Hoek for your response. Before posting my question in the forum, I tried similar query. It is working with one character as delimiter.
    with test as (select 'ABC,DEF,GHI,JKL,MNO' str from dual )
    select regexp_substr (str, '[^,]+', 1, rownum) split
    from test
    connect by level <= length (regexp_replace (str, '[^,]+')) + 1;
    Above query is giving correct result by fetching 5 rows. I have modified the query like below...
    with test as (select 'ABC,,,DEF,,,GHI,,,JKL,,,MNO' str from dual )
    select regexp_substr (str, '[^,,,]+', 1, rownum) split
    from test
    connect by level <= length (regexp_replace (str, '[^,,,]+')) + 1;
    Above query resulting 13 rows and last 8 rows are nulls. Number of null rows are increasing, if I increase number of characters in delimiter. Could you please tell me how to avoid those null rows.
    Thanks.

  • One Row into multiple Column

    CREATE TABLE #ids (empid VARCHAR(200))
    INSERT INTO #ids SELECT '100,200,300,400'
    INSERT INTO #ids SELECT '1100,1200,1300,1400'
    I am trying to get output following format
    ID_1    ID_2   ID_3  ID_4
    100     200    300   400
    1000    1200   1300  1400
    Each row in EmpId column will have only max. four values
    Thanks
    V

    One more method:
    CREATE TABLE #ids (empid VARCHAR(200))
    INSERT INTO #ids SELECT '100,200,300,400'
    INSERT INTO #ids SELECT '1100,1200,1300,1400'
    select * from #ids
    ;WITH
    L0 AS(SELECT 1 AS c UNION ALL SELECT 1),
    L1 AS(SELECT 1 AS c FROM L0 AS A, L0 AS B),
    L2 AS(SELECT 1 AS c FROM L1 AS A, L1 AS B),
    L3 AS(SELECT 1 AS c FROM L2 AS A, L2 AS B),
    Numbers AS(SELECT ROW_NUMBER() OVER(ORDER BY c) AS n FROM L3)
    SELECT [1] AS Column1, [2] AS Column2, [3] AS Column3, [4] AS Column4
    FROM
    (SELECT rn,
    ROW_NUMBER() OVER (PARTITION by rn ORDER BY nums.n) AS PositionInList,
    LTRIM(RTRIM(SUBSTRING(valueTable.empid, nums.n,
    charindex(N',', valueTable.empid + N',', nums.n) - nums.n))) AS [Value]
    FROM Numbers AS nums INNER JOIN (Select row_number()over (Order by (Select NULL)) rn , Empid From #ids) AS valueTable ON nums.n <= CONVERT(int, LEN(valueTable.empid))
    AND SUBSTRING(N',' + valueTable.empid, n, 1) = N',') AS SourceTable
    PIVOT
    MAX([VALUE]) FOR PositionInList IN ([1], [2], [3], [4])
    ) AS Table2
    drop table #ids

  • Converting a single row into multiple columns

    Hi All,
    I have a hierarchy table. Sample values are
    Parent Child
    1
    1 2
    1 3
    2 4
    3 5
    2 6
    I have used the connect by clause to get the following listing using sys_connect_by_path
    /1/
    /1/2/
    /1/3/
    /1/2/4/
    /1/2/6/
    /1/3/5/
    But now I need them in seperate columns like
    c1 c2 c3
    1
    1 2
    1 3
    1 2 4
    1 2 6
    1 3 5
    Please help me in getting this resultset.
    Thanks
    Subbu S

    SQL> create table hierarchy_table
      2  as
      3  select 1 parent, 2 child from dual union all
      4  select null, 1 from dual union all
      5  select 1, 3 from dual union all
      6  select 2, 4 from dual union all
      7  select 3, 5 from dual union all
      8  select 2, 6 from dual
      9  /
    Tabel is aangemaakt.
    SQL> column s format a30
    SQL> column c1 format a5
    SQL> column c2 format a5
    SQL> column c3 format a5
    SQL> select s
      2       , substr
      3         ( s
      4         , nullif(instr(s,'|',1,1),0) + 1
      5         , nvl(nullif(instr(s,'|',1,2),0),4000) - instr(s,'|',1,1) - 1
      6         ) c1
      7       , substr
      8         ( s
      9         , nullif(instr(s,'|',1,2),0) + 1
    10         , nvl(nullif(instr(s,'|',1,3),0),4000) - instr(s,'|',1,2) - 1
    11         ) c2
    12       , substr
    13         ( s
    14         , nullif(instr(s,'|',1,3),0) + 1
    15         , nvl(nullif(instr(s,'|',1,4),0),4000) - instr(s,'|',1,3) - 1
    16         ) c3
    17    from ( select sys_connect_by_path(child,'|') s
    18             from hierarchy_table
    19          connect by parent = prior child
    20            start with parent is null
    21         )
    22  /
    S                              C1    C2    C3
    |1                             1
    |1|2                           1     2
    |1|2|4                         1     2     4
    |1|2|6                         1     2     6
    |1|3                           1     3
    |1|3|5                         1     3     5
    6 rijen zijn geselecteerd.Regards,
    Rob.

  • Trying to convert multiple rows into multipe columns within a single row

    I am trying to convert data from multiple rows into multiple columns. Let me see if I can paint the picture for you.
    Here is a sample of the table i am trying to read from:
    Company Name Account
    1 Sam 123
    1 Sam 234
    1 Joe 345
    1 Sue 789
    1 Sue 987
    1 Sue 573
    I am trying to put this into a View that would have the data represented as such:
    Company Name Acct1 Acct2 Acct3 Acct4
    1 Sam 123 234 <null> <null>
    1 Joe 345 <null> <null> <null>
    1 Sue 789 987 573 <null>
    Many thanks in advance for your help!

    test@XE> --
    test@XE> with t as (
      2    select 1 as company, 'Sam' as name, 123 as account from dual union all
      3    select 1, 'Sam', 234 from dual union all
      4    select 1, 'Joe', 345 from dual union all
      5    select 1, 'Sue', 789 from dual union all
      6    select 1, 'Sue', 987 from dual union all
      7    select 1, 'Sue', 573 from dual)
      8  --
      9  select company,
    10         name,
    11         max(case when rn = 1 then account else null end) as acct1,
    12         max(case when rn = 2 then account else null end) as acct2,
    13         max(case when rn = 3 then account else null end) as acct3,
    14         max(case when rn = 4 then account else null end) as acct4
    15    from (select company,
    16                 name,
    17                 account,
    18                 row_number() over (partition by company, name order by 1) as rn
    19            from t)
    20   group by company, name;
       COMPANY NAM      ACCT1      ACCT2      ACCT3      ACCT4
             1 Joe        345
             1 Sam        234        123
             1 Sue        573        789        987
    3 rows selected.
    test@XE>
    test@XE>isotope

  • To display comma separted value into multiple column

    Hi,
    I want to display value into multiple column like below
    data is like this
    col1
    res_menaHome:MenaHome
    res_menaHomeEmp:MenaHome Employee
    res_MDSpecialSer:MD Special Services
    res_Smart:Smart
    now i want to display like
    col1 col2
    res_menaHome MenaHome
    res_menaHomeEmp MenaHome Employee
    res_MDSpecialSer MD Special Services
    res_Smart Smart
    Thanks in advance.

    You mean like this?
    with q as (select 'res_menaHome:MenaHome' myString from dual)
    select substr(myString, 1, instr(myString, ':') - 1) col1,
    substr(myString, instr(myString, ':') + 1) col2
    from q
    COL1,COL2
    res_menaHome,MenaHome

  • Merge multiple rows into single row (but multiple columns)

    How to merge multiple rows into single row (but multiple columns) efficiently.
    For example
    IDVal IDDesc IdNum Id_Information_Type Attribute_1 Attribute_2 Attribute_3 Attribute_4 Attribute_5
    23 asdc 1 Location USA NM ABQ Four Seasons 87106
    23 asdc 1 Stats 2300 91.7 8.2 85432
    23 asdc 1 Audit 1996 June 17 1200
    65 affc 2 Location USA TX AUS Hilton 92305
    65 affc 2 Stats 5510 42.7 46 9999
    65 affc 2 Audit 1996 July 172 1100
    where different attributes mean different thing for each Information_type.
    For example for Information_Type=Location
    Attribute_1 means Country
    Attribute_2 means State and so on.
    For example for Information_Type=Stats
    Attribute_1 means Population
    Attribute_2 means American Ethnicity percentage and so on.
    I want to create a view that shows like below:
    IDVal IDDesc IDNum Country State City Hotel ZipCode Population American% Other% Area Audit Year AuditMonth Audit Type AuditTime
    23 asdc 1 USA NM ABQ FourSeasons 87106 2300 91.7 46 85432 1996 June 17 1200
    65 affc 2 USA TX AUS Hilton 92305 5510 42.7 46 9999 1996 July 172 1100
    Thanks

    Hi,
    That's called Pivoting . The forum FAQ has a section on this subject: {message:id=9360005}
    I hope this answers your question.
    If not, post your best attempt, along with a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data. (You did post the results you wanted, but they're very hard to read because they're not formatted. Use \ tags, as described in the forum FAQ, below.)
    Explain, using specific examples, how you get the results you want from the data given.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).  This is always important, but especially so with pivots.
    See the forum FAQ {message:id=9360002}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Displaying single value (Record) into multiple columns

    Hi All,
    I want to display the single record into multiple columns. Please let me know How to achieve this..
    Record:
    Lvl Activity Acre
    6 Week 4 (Same value to be displayed into 3 columns.)
    REquired output:
    lvl Activity PH1 PH2 PH3
    6 Week 4 4 4
    Thanks
    Kavi

    user533671 wrote:
    Hi,
    Thanks for immediate reply.
    PH1, PH2, PH3, ... will go more columns based on parameter , what we are passing.An single SQL statement cannot have a dynamic number of columns based on the data itself. The projection (columns returned) have to be known before any data is fetched... and that includes some 'parameter' value.
    {thread:id=2309172}
    You can build a query dynamically, based on a parameter or even on the data, but 99 times out 100 people use dynamic queries where they are not needed.
    Perhaps explain to us what you are really trying to achieve and why, and we could suggest some better way.

  • I need to divide selected row into multiple rows when i navigate  ADF 11g

    Hi
    I'm using jdeveloper 11.1.1.2.0 with ADF 11g.
    I need to divide selected row into multiple rows when i navigate to other page . Scenario - in first page i'm displaying some records with columns like empno , empstatus , empworkdepts ,curdepts
    Here empworkdepts gives the numeric number like no of departments work shifts 3 or 4 or 5. when i select any particular employee and fire next button to navigate next page.I have to divide the selected employee with same information into multiple times based on the empworkdepts value.
    empno empstatus empworkdepts curdept
    001 eds 2 TS
    002 hr 1 FO
    003 eds 4 TS
    *004 eds 3 TS*
    now i selected employee 004 , when i navigate to next page.
    Empno EmpStatus EmpWorkDepts CurDept
    004 eds 3 TS
    004 eds 3 TS
    004 eds 3 TS
    i did with java code in bean .but not stable .
    any help............
    thanks advance.............
    Edited by: user9010551 on May 5, 2010 10:48 PM
    Edited by: user9010551 on May 10, 2010 11:31 PM

    user9086775 wrote:
    Hi Experts,
    I have a requirment where i need to fetch parts of a single row into multiple rows from a singlt Query, i am sure it is possible using Pivots but just cant figure out an approach. Any help on this is highly appriciapted.
    Requirment:
    This is a sample set record in a table
    Product     Sub Product          Name    Age
    New Car    Nissan                   Tom        49
    New Car    Nissan                   Jack         36
    Old Car      Audi                     Sam         24
    Old Car      Jaguar                  Pint          26
    Old Car      Audi                     Smith       41
    I need to be able to fetch the above data in the below fashion
    Product     Sub Product          Name    Age
    New Car
    Nissan
    Tom        49
    Jack        36
    Old Car     
    Audi            
    Sam        24
    Smith      41
    Jaguar                   Pint         26Please help with ideas as to how can i achive the above without using PLSQL.
    Thanks in advance!You should be doing this in the client on not in the DB. Use the reporting tool that you use to do this.
    For example if you are in SQL Plus you can use the BREAK command.

  • Adding Specific columns of dynamic internal table row into another column

    Hi Gurus,
    I need to add  few columns of a dynamic internal table row into another column:
    Article description hy01 hy02 total
    101      panza         10     12      22
    102      masht         12     12     24
    dynamic internal table is created and columns hy01 hy02.... can increase
    How to add the the values in hy01 hy 02... into total.
    Regards,
    Dep

    Hi,
    If you really want to have a dynamic table, then you will have to find a way to generate a whole new table, and then copy the data from the old table to the new one. There is no way to modify a type during runtime in ABAP.
    Here an example how to generate a dynamic table based on another internal table, hope this will help you.
    TYPE-POOLS: slis.
    PARAMETERS: p_nb_hy TYPE i DEFAULT 2. "Number of new HY columns to be added
    * Type ZST_T:
    *   matnr  TYPE matnr
    *   maktx  TYPE maktx
    *   hy01   TYPE i
    *   total  TYPE i
    TYPES: ty_t TYPE STANDARD TABLE OF zst_s.
    PERFORM main.
    *&      Form  main
    *       text
    FORM main.
      DATA: lt_fieldcat     TYPE slis_t_fieldcat_alv,
            lt_t            TYPE ty_t,
            lr_new_t        TYPE REF TO data.
      FIELD-SYMBOLS: <lt_new_t> TYPE STANDARD TABLE.
      "Add some lines to LT_T just to have something to display on screen
      DO 10 TIMES.
        APPEND INITIAL LINE TO lt_t.
      ENDDO.
      CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
        EXPORTING
          i_structure_name = 'ZST_S'
        CHANGING
          ct_fieldcat      = lt_fieldcat.
      "Copy LT_T to LR_NEW_T
      PERFORM extend_and_copy_table USING lt_t p_nb_hy CHANGING lr_new_t lt_fieldcat.
      CLEAR lt_t. "Not needed anymore...
      ASSIGN lr_new_t->* TO <lt_new_t>.
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          it_fieldcat = lt_fieldcat
        TABLES
          t_outtab    = <lt_new_t>.
    ENDFORM.                    "main
    *&      Form  extend_and_copy_table
    FORM extend_and_copy_table USING ut_t           TYPE STANDARD TABLE
                                     uv_nb_hy       TYPE i
                               CHANGING cr_t        TYPE REF TO data
                                        ct_fieldcat TYPE slis_t_fieldcat_alv
                               RAISING cx_sy_struct_creation cx_sy_table_creation.
      DATA: lo_tabledescr      TYPE REF TO cl_abap_tabledescr,
            lo_structdescr     TYPE REF TO cl_abap_structdescr,
            lo_new_structdescr TYPE REF TO cl_abap_structdescr,
            lo_new_tabledescr  TYPE REF TO cl_abap_tabledescr,
            lt_components      TYPE cl_abap_structdescr=>component_table,
            ls_component       TYPE cl_abap_structdescr=>component,
            lv_field_cnt       TYPE numc2,
            ls_fieldcat        TYPE slis_fieldcat_alv,
            lr_fieldcat        TYPE REF TO slis_fieldcat_alv.
      FIELD-SYMBOLS: <ls_old_s> TYPE ANY,
                     <lt_new_t> TYPE STANDARD TABLE,
                     <ls_new_s> TYPE ANY.
      "Get the list of all components from UT_T line structure
      lo_tabledescr  ?= cl_abap_tabledescr=>describe_by_data( ut_t ).
      lo_structdescr ?= lo_tabledescr->get_table_line_type( ).
      lt_components  = lo_structdescr->get_components( ).
      "The new columns will be from type of column HY01
      ls_component-type = lo_structdescr->get_component_type( 'HY01' ).
      "The new columns will have the same fieldcat info as column HY01
      READ TABLE ct_fieldcat INTO ls_fieldcat WITH KEY fieldname = 'HY01'.
      "HY<lv_field_cnt> = new field name
      lv_field_cnt = uv_nb_hy + 1.
      "For each new column...
      DO uv_nb_hy TIMES.
        "Generate the new column field name
        CONCATENATE  'HY' lv_field_cnt INTO ls_component-name.
        ls_fieldcat-fieldname = ls_component-name.
        "Add the new field to the components of the new structure
        INSERT ls_component INTO lt_components INDEX 4.
        "Add the new field's fieldcat info to the fieldcat
        INSERT ls_fieldcat  INTO ct_fieldcat   INDEX 4.
        lv_field_cnt = lv_field_cnt - 1.
      ENDDO.
      "Adjust the COL_POS from fieldcat
      LOOP AT ct_fieldcat REFERENCE INTO lr_fieldcat.
        lr_fieldcat->col_pos = sy-tabix.
      ENDLOOP.
      "Create the new table
      lo_new_structdescr = cl_abap_structdescr=>create( p_components = lt_components ).
      lo_new_tabledescr  = cl_abap_tabledescr=>create( p_line_type = lo_new_structdescr ).
      CREATE DATA cr_t TYPE HANDLE lo_new_tabledescr.
      ASSIGN cr_t->* TO <lt_new_t>.
      "Copy all data from old to new table
      LOOP AT ut_t ASSIGNING <ls_old_s>.
        APPEND INITIAL LINE TO <lt_new_t> ASSIGNING <ls_new_s>.
        MOVE-CORRESPONDING <ls_old_s> TO <ls_new_s>.
      ENDLOOP.
    ENDFORM.                    "main

  • Multipe rows to Multiple Columns - Oracle9i

    Hi
    I know there have been similar threads however I haven't found a suitable solution from them.
    I'm using Oracle 9i and trying to convert multiple rows to multiple columns.
    CREATE TABLE TEST_VALUES
    CUSTOMER_ID NUMBER(10),
    VAL VARCHAR2(100)
    INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(10,'VAL1');
    INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(10,'VAL2');
    INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(20,'VAL1');
    INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(30,'VAL3');
    INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(30,'VAL4');
    INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(40,'VAL5');
    INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(40,'VAL6');
    COMMIT;
    SELECT * FROM TEST_VALUES
    CUSTOMER_ID     VAL
    10     VAL1
    10     VAL2
    20     VAL1
    30     VAL3
    30     VAL4
    40     VAL5
    40     VAL6
    I want one row per CUSTOMER_ID with two columns to store the values
    CUSTOMER_ID FIRST_VAL SECOND_VAL
    10 VAL1 VAL2
    20 VAL1 NULL
    30 VAL3 VAL4
    40 VAL5 VAL6
    There are only 6 possible 'VAL' values however there are a large number of CUSTOMER_id values. I tried DECODE as suggested elsewhere but that creates 6 columns. However I only want two output columns as there are no more than two values per customer.
    If I had 11g I would try pivot, can anyone please suggest any solutions?
    Many Thanks
    GB

    Thanks Bhushan, that was nearly what I was after. For CUSTOMER 20 it gave
    20 VAL1 VAL1
    so to make it
    20 VAL1 NULL
    I just changed your query slightly to
    select distinct
    customer_id,
    min(val) over (partition by (customer_id)) val1,
    CASE WHEN max(val) over (partition by (customer_id)) = min(val) over (partition by (customer_id)) THEN NULL
    ELSE max(val) over (partition by (customer_id)) END val2
    from test_values
    order by customer_id
    and that gives the correct results. Just checking the full data set now.
    Thanks for your help.

  • Split one column  value and insert into multiple columns

    hi
    am new to plsql .
    i want to split a characters from one column and insert into multiple columns
    i tried used substr function the symbol ',' vary his place dynamically ,so i can't apply substr function.
    for eg:  before split
    col1 :
    col2 :
    col3 :
    col4 :
    colu5: adsdf,fgrty,erfth,oiunth,okujt
    after split
    col1 :adsd
    col2 :fgrty
    col3 :erfth
    col4 :oiunth
    col5 : adsdf,fgrty,erfth,oiunth,okujt
    can anyone help me
    thanks
    Edited by: 800324 on Dec 23, 2010 8:28 AM
    Edited by: 800324 on Dec 23, 2010 8:36 AM

    How about:
    SQL> create table t
      2  (col1 varchar2(30)
      3  ,col2 varchar2(30)
      4  ,col3 varchar2(30)
      5  ,col4 varchar2(30)
      6  ,col5 varchar2(30)
      7  );
    Table created.
    SQL> insert into t (col5) values ('adsdf,fgrty,erfth,oiunth,okujt');
    1 row created.
    SQL> insert into t (col5) values ('x,y');
    1 row created.
    SQL> insert into t (col5) values ('a,b,c,d');
    1 row created.
    SQL> select * from t;
    COL1                           COL2                           COL3                           COL4                           COL5
                                                                                                                                adsdf,fgrty,erfth,oiunth,okujt
                                                                                                                                x,y
                                                                                                                                a,b,c,d
    3 rows selected.
    SQL>
    SQL> merge into t a
      2  using ( with t1 as ( select col5||',' col5
      3                       from   t
      4                     )
      5          select substr(col5, 1, instr(col5, ',', 1, 1)-1) col1
      6          ,      substr(col5, instr(col5, ',', 1, 1)+1, instr(col5, ',', 1, 2)- instr(col5, ',', 1, 1)-1) col2
      7          ,      substr(col5, instr(col5, ',', 1, 2)+1, instr(col5, ',', 1, 3)- instr(col5, ',', 1, 2)-1) col3
      8          ,      substr(col5, instr(col5, ',', 1, 3)+1, instr(col5, ',', 1, 4)- instr(col5, ',', 1, 3)-1) col4
      9          ,      rtrim(col5, ',') col5
    10          from   t1
    11        ) b
    12  on ( a.col5 = b.col5 )
    13  when matched then update set a.col1 = b.col1
    14                             , a.col2 = b.col2
    15                             , a.col3 = b.col3
    16                             , a.col4 = b.col4
    17  when not matched then insert (a.col1) values (null);
    3 rows merged.
    SQL> select * from t;
    COL1                           COL2                           COL3                           COL4                           COL5
    adsdf                          fgrty                          erfth                          oiunth                         adsdf,fgrty,erfth,oiunth,okujt
    x                              y                                                                                            x,y
    a                              b                              c                              d                              a,b,c,d
    3 rows selected.
    SQL> Assuming you're on 9i...

  • Find MIN, MAX of multiple rows from multiple columns

    Hello,
    I need to figure out how to pull the MIN/MAX of multiple rows from multiple columns into one column. Even if some are NULL/blank.
    For Example: (C: Column, R: Row, N - NULL/Blank)
    C:____1____2____3____ 4____Max
    R:____20___22___13____4____*22*
    R:____N____N____32____14___*32*
    R:____N____12____N____N____*12*
    That is, it always gives a value for MIN/MAX unless there are NO values in all the rows of the columns.
    So if there is one value, it will select that for the MIN/MAX, as it's the smallest/biggest since there is nothing to compare it to.
    Here is my current code:
    CASE WHEN COLUMN 1 < COLUMN 2 THEN COLUMN 2 ELSE COLUMN 1 END

    Hi Thank you for your feedback, unfortunately, I just found out that EVALUATE Function is disabled in our environment for security reasons, so the only other way I've discovered is this:
    The problem is that none of the conditions in the case statement are met--so the column is set to null. You can add a WHEN statement (section 2 below) to catch the nulls. There are five cases to consider:
    Case 1: begin insp > bad order
    Case 2: begin insp < bad order
    Case 3: bad order only (begin insp is NULL)
    Case 4: begin insp only (bad order is NULL)
    Case 5: both begin insp is NULL and bad order is NULL
    1) If bgn-crm-insp-ob > report-bo-ob then bgn-crm-insp-ob
    (Case 1)
    CASE WHEN filter ("- Terminal Task Measures"."Task Reported DateTime (Local)" using "Task Detail"."Task Code" = 'bgn-crm-insp-ob') > filter ("- Terminal Task Measures"."Task Reported DateTime (Local)" using "Task Detail"."Task Code" = 'report-bo-ob') THEN filter ("- Terminal Task Measures"."Task Reported DateTime (Local)" using "Task Detail"."Task Code" = 'bgn-crm-insp-ob')
    2) If report-bo-ob is NULL then bgn-crm-insp-ob
    (Case 4, 5) for case 5, you will get NULL
    WHEN filter ("- Terminal Task Measures"."Task Reported DateTime (Local)" using "Task Detail"."Task Code" = 'report-bo-ob') IS NULL THEN filter ("- Terminal Task Measures"."Task Reported DateTime (Local)" using "Task Detail"."Task Code" = 'bgn-crm-insp-ob')
    3) Else report-bo-ob
    (Cases 2, 3)
    ELSE filter ("- Terminal Task Measures"."Task Reported DateTime (Local)" using "Task Detail"."Task Code" = 'report-bo-ob') END
    Hopefully this works, I'll give feedback if it does, or if you have any further suggestions please submit, again, THANK YOU SO MUCH ANYWAYS!

  • How to Transpose a Row into a Column ? (URGENT)

    I am trying to transpose a row into a column and please pay attention "Just one row of a table into a column of another table". Unlike Excel, in Numbers there is no easy way to transpose data but this is a bit urgent to me, any help would be appreciated!!!!

    Dear Friend,
    I have been looking for an answer the whole day and have performed every possible solution in the index, transpose, lookup and all the other recommended functions, none of them have worked for me in this case. I have gone through more than 20 posts on the discussion board but all of them were useless. I havent tried your method yet but although I appreciate your help and time, I dont remember forcing you or anyone else to help me, I just looked at posting my question  as an option to avoid writing long scripts for such a basic function that is easily available on excel under the special paste option.
    I thank you very much in advance for the advice.
    PS. I tried it and with a couple of modifications it worked, thank you very much for making my day
    Message was edited by: cool3pehr

Maybe you are looking for

  • Structure of Profit & Cost center

    Hi All, I am at the phase of defining the Organization structure of Company. And I am facing the problem in defining the Cost center and profit center for that. Hope any one can help me in that. Scenario is like 1. I have 5 Raw Material. 2.By  proces

  • Split & Import WMA file from windows to mac

    I have bought several wma files online that are rights managed that I want to play on my ipod. A few days ago, I found a workaround to getting them from the internet to my ipod - download file on the windows side of my mac (I have XP installed and us

  • BC login issues, account integrity

    Hi, Hoping someone can help me quite easily, When I try to login to any Adobe service including BC using my adobe ID I get the notice "To protect the security and integrity of your account, we have resest your password. Please check your inbox for an

  • X-file with db function returning a rowtype to forms. Positional binding?

    How to explain this ... First things first: Forms [32 bits] Versión 11.1.2.1.0 (Producción) Oracle Database 11g Release 11.2.0.3.0 - 64bit Production Now the db testcase: I've got two schemas, quite similar, SCHEMA_A and SCHEMA_B Imagine the same tab

  • Prepping for Aperture 3. What do I do with iPhoto Library?

    OK, gang, I’m getting ready for Aperture 3 and could use some friendly advice so that I don’t run off the road and into the ditch as I wend my way down the Aperture highway. Here’s the background story. In 2007 I bought a 20” iMac and commenced short