How to convert the lines into columns dynamically

HII
I have a 'report_line' with the following data table

location_code line_code value
LOC1 L1 12
loc2 L1 13
LOC1 L2 15
loc2 L3 11
LOC3 L1 9

Now I want the o/p by converting the line_codes in clumns as below

L1 L2 L3
LOC1 12 15
13 11 loc2
LOC3 9

How can I do this dynamically. Line codes are not constatnt, they can change for each scenario.

Thank you
Sandeep

I want just a sql for this query and no. columns ' maximum of are 6.

In this case you can do.

--sample data
with t
as
(
select 'loc1' location_code, 'L1' line_code, 12 value from dual union all
select 'loc2', 'L1', 13 from dual union all
select 'loc1', 'L2', 15 from dual union all
select 'loc2', 'L3', 11 from dual union all
select 'loc3', 'L1', 9 from dual
)
--end of sample data
select location_code,
       max(decode(line_code, 'L1', value, null)) L1,
       max(decode(line_code, 'L2', value, null)) L2,
       max(decode(line_code, 'L3', value, null)) L3,
       max(decode(line_code, 'L4', value, null)) L4,
       max(decode(line_code, 'L5', value, null)) L5,
       max(decode(line_code, 'L6', value, null)) L6
  from t
 group by location_code

Here I used the WITH clause as an example of table. You can replace the "t" table name in the select statement with your actual table and remove the WITH clause.

Tags: Database

Similar Questions

  • SQL to convert the lines into columns

    Can someone please help with this? It sounds easy, but I can't do it on my own

    I need to create the output table something like this using the input table

    Entry table
    BOOKID ORDNAM ORDID
    100 1 XXX
    100 2 YYY
    101 AAA 3
    101 4 BBB

    For each ID value, there will be 2 ORDID leave a sequence

    Output table
    BOOKID ORDNAM ORDID ORDNAM ORDID
    YYY XXX 100 1 2
    101 BBB 3 4 AAA

    Hello

    It's called swing , and there are a few good links in the FAQ forum on this subject:
    SQL and PL/SQL FAQ

    Exactly how do depends on your version of Oracle and exactly to your needs.
    If you use Oracle 11, you can use SELECT... Function PIVOT.

    If you have only two lines (by ID) of the original table you need to view side by side, then a self-join can be easier, regardless of your version.
    For example:

    SELECT     l.bookid
    ,     l.ordnam
    ,     l.ordid
    ,     r.ordnam
    ,     r.ordid
    FROM          input_table  l
    LEFT OUTER JOIN     input_table  r  ON   l.bookid  = r.bookid
                                     AND  l.ordid   < r.ordid
    ;
    

    I hope that answers your question.
    If this isn't the case, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all tables, or post a question involving tables commonly available, such as those in the scott schema. Whatever it is, after the desired results from these data.
    Explain, using specific examples, how you get these results from these data.
    Always tell what version of Oracle you are using.
    You will find the answers better faster if you always provide this information whenever you post a question.

    Published by: Frank Kulash, July 22, 2011 11:21

  • Convert the lines into columns... very urgent

    Hello

    I have a small doubt... I create it follows and insert commands.

    CREATE THE TABLE SCOTT. ABC
    (
    ROOM_NAME VARCHAR2 (20 BYTE),
    MET_MONTH VARCHAR2 (2 BYTE),
    NUMBER OF TOTAL_SEATS
    );

    TOGETHER TO DEFINE
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("Synergy Room 3', '06' 18");
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("NP2-18', ' 08,' 35 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("Synergy room 2', '08' 50 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("Synergy room 2', '05' 20 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("NP5-26 ', ' 05,' 10 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("NP2-18 ', ' 11', 35 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("NP3-49 ', ' 07,' 10 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("Synergy Room1", '08', 45);
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("Wallace Stevens', '09', 95 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ('Synergy Room1', '04', 73);
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("Wallace Stevens ', 10', 205 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("Wallace Stevens', '04', 100 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("Synergy Room 3', '04' 25 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("NP3-49 ', ' 04', 8 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("Synergy Room 3', '07' 10 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("NP5-26', ' 07,' 13 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("NP2-18', ' 05,' 28 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("NP3-49 ', ' 09', 15 ');
    Insert into ABC
    (ROOM_NAME, MET_MONTH, TOTAL_SEATS)
    Values
    ("NP5-26 ', ' 10', 15 ');
    COMMIT;

    So I need to get the result like this:

    RoomName Jan Feb Mar Apr May June July Aug 7 Oct Nov Dec

    Synergy room 1 0 0 0 73 0 0 0 45 0 0 0 0
    Synergy room 2 0 0 0 0 20 0 0 50 0 0 0 0
    Synergy Room 3 0 0 0 25 0 18 10 0 0 0 0 0
    Wallace Stevens 0 0 0 100 0 0 0 0 95 205 0 0
    NP5-26 0 0 0 0 10 0 13 0 0 15 0 0
    NP2-18 0 0 0 0 28 0 0 35 0 0 35 0
    NP3-49 0 0 0 0 0 0 10 0 15 0 0 0



    Thank you
    Rajiv

    If you do not want null values then try like this

    NVL (sum (decode (met_month, ' 01', total_seats)), 0) such as jan,.

    S

  • How to view the line of columns without using the pivot keyword

    Hello
    could someone help me how to display lines in columns without using the keyword pivot and actuall is my scenario, iam having two tables with names and examples of data is shown below

    MIDDLE MINAME TASKID TASKNAME IDENTIFICATION PROJECT

    1 PROJ1 1 AA 100 PR1_TASK1
    1 PROJ1 3 CC PR1_TASK3 102
    1 PROJ1 DD 4 103 PR1_TASK4
    1 PROJ1 EE 5 104 PR1_TASK5
    1 PROJ1 6 105 FF PR1_TASK6
    2 PROJ2 EE 5 114 PR2_TASK1
    2 PROJ2 6 115 FF PR2_TASK2
    2 PROJ2 GG 7 116 PR2_TASK3
    2 PROJ2 HH 8 117 PR2_TASK4
    2 PROJ2 9 118 PR2_TASK5 JJ
    2 PROJ2 KK 10 119 PR2_TASK6
    2 PROJ2 1 AA PR2_TASK7 120


    The output should show the project and County of tasks at a given stage as shown below

    project AA BB CC DD EE FF GG HH JJ KK
    1 2 0 1 5 3 2 0 2 1 0
    2 1 2 0 2 1 0 2 4 3 1


    Thanks in advance,
    VVR
    CREATE TABLE pivot_test (
    ID           NUMBER,
    PROJECT      VARCHAR2(10),
    MID          NUMBER,
    MINAME       VARCHAR2(5),
    TASKID       NUMBER,
    TASKNAME     VARCHAR2(10)
    );
    
    INSERT INTO pivot_test VALUES (1, 'PROJ1', 1,  'AA', 100, 'PR1_TASK1');
    INSERT INTO pivot_test VALUES (1, 'PROJ1', 3,  'CC', 102, 'PR1_TASK3');
    INSERT INTO pivot_test VALUES (1, 'PROJ1', 4,  'DD', 103, 'PR1_TASK4');
    INSERT INTO pivot_test VALUES (1, 'PROJ1', 5,  'EE', 104, 'PR1_TASK5');
    INSERT INTO pivot_test VALUES (1, 'PROJ1', 6,  'FF', 105, 'PR1_TASK6');
    INSERT INTO pivot_test VALUES (2, 'PROJ2', 5,  'EE', 114, 'PR2_TASK1');
    INSERT INTO pivot_test VALUES (2, 'PROJ2', 6,  'FF', 115, 'PR2_TASK2');
    INSERT INTO pivot_test VALUES (2, 'PROJ2', 7,  'GG', 116, 'PR2_TASK3');
    INSERT INTO pivot_test VALUES (2, 'PROJ2', 8,  'HH', 117, 'PR2_TASK4');
    INSERT INTO pivot_test VALUES (2, 'PROJ2', 9,  'JJ', 118, 'PR2_TASK5');
    INSERT INTO pivot_test VALUES (2, 'PROJ2', 10, 'KK', 119, 'PR2_TASK6');
    INSERT INTO pivot_test VALUES (2, 'PROJ2', 1,  'AA', 120, 'PR2_TASK7');
    
    SELECT ID as PROJECT,
           SUM(DECODE(miname, 'AA', 1, 0)) AS AA,
           SUM(DECODE(miname, 'BB', 1, 0)) AS BB,
           SUM(DECODE(miname, 'CC', 1, 0)) AS CC,
           SUM(DECODE(miname, 'DD', 1, 0)) AS DD,
           SUM(DECODE(miname, 'EE', 1, 0)) AS EE,
           SUM(DECODE(miname, 'FF', 1, 0)) AS FF,
           SUM(DECODE(miname, 'GG', 1, 0)) AS GG,
           SUM(DECODE(miname, 'HH', 1, 0)) AS HH,
           SUM(DECODE(miname, 'JJ', 1, 0)) AS JJ,
           SUM(DECODE(miname, 'KK', 1, 0)) AS KK
    FROM   pivot_test
    GROUP BY ID;
    
    PROJECT AA BB CC DD EE FF GG HH JJ KK
    ------- -- -- -- -- -- -- -- -- -- --
          1  1  0  1  1  1  1  0  0  0  0
          2  1  0  0  0  1  1  1  1  1  1 
    
  • How to convert the attribute into a processing instruction in indesign?

    Hi all Experts,


    How to convert attributes selected in a processing instruction using javascript in indesign.

    Please suggest me.

    Kind regards

    Jack

    The problem came from namespace 'help '. You must add the namespace to the root element if they do not exist.

    Try this...

    var doc = app.activeDocument;
    var _root = doc.xmlElements[0]; 
    
    if( !_root.xmlAttributes.itemByName("xmlns:aid").isValid ){
        _root.xmlAttributes.add( "xmlns:aid", "http://ns.adobe.com/Adobe InDesign/4.0/" );
    }
    var _xmlElements = _root.evaluateXPathExpression("descendant-or-self::*[@*[name()='aid:pstyle']]");
    var i = _xmlElements.length;  
    
    while( i-- ){
        _xmlElements[i].xmlInstructions.add('aid:pstyle', _xmlElements[i].xmlAttributes.item('aid:pstyle').value);
        _xmlElements[i].xmlAttributes.item('aid:pstyle').remove();
    }
    
  • Convert the lines into a single column

    create table suresh
    (
    Identification number,
    ch char (1)
    )
    ;

    Insert in suresh values(1,'i');
    Insert in suresh values(1,'a');
    Insert in suresh values(1,'m');
    Insert in suresh values(1,'b');
    Insert in suresh values(1,'o');
    Insert in suresh values(1,'y');

    Select * from suresh

    ID ch
    1 I
    1 a
    1 m
    1 b
    1 o
    1 y


    I'm looking for output something like this

    ID ch
    1 iamboy
    ..
    select id,listagg(ch) within group(order by ch) as ch
    from suresh
    group by id 
    
  • How to convert the Qbytearray into image in cascades

    Hi guys,.

    How can I convert a Qbytearray in image, so that I can show to imageView.Any the code examples will be useful

    Concerning

    Rakesh Shankar.P

    Thanks for your help, I was able to retrieve image

     QImage swapped;
         qDebug()<<"data"<setImage(image);     container->add(testImage);
    

    Concerning

    Rakesh Shankar.P

  • How to make the lines dynimac columns in Oracle PL/SQL?

    I have this request:
    select cso.branch,
      decode(NVL(cso.YES,0)+ NVL(cso.NO,0)+ NVL(cso.NA,0),0,'N','Y') ||' - '|| NVL(cso.officer,0) AS CSO_YN
    from (
    SELECT A.BRAN_CODE_PK as branch, a.emp_code_pk as officer, 
       COUNT(CASE WHEN D.STATUS = 1 THEN D.STATUS ELSE NULL END) YES,
       COUNT(CASE WHEN D.STATUS = 2 THEN D.STATUS ELSE NULL END) NO,
       COUNT(CASE WHEN D.STATUS = 3 THEN D.STATUS ELSE NULL END) NA
    FROM sales_team_accounts A, users U LEFT OUTER JOIN daily_check_lists D ON (D.USER_ID_PK = U.EMP_OFFICER_CODE
                                                                    and to_date(D.CHECK_LIST_DATE, 'DD-MON-YYYY') = '08-JUL-2010' ) -- date should be the parameter date)
    WHERE 1=1
    AND A.EMP_OFFICER_CODE = U.EMP_OFFICER_CODE
    AND A.sale_type_fk = 101 -- CSO
    AND A.SUPV_EMP_CODE_FK != 0
    and a.BRAN_CODE_PK = 1001
    group by A.BRAN_CODE_PK, a.emp_code_pk
    ) cso;
    The result of this query is the following:
    BRANCH     CSO_YN
    --------------------------------------
    1001             Y - 321
    1001             N - 335
    1001             Y - 90050
    1001             N - 303
    The scenario is, there could be maximum 7 officers in a branch,
    the total number of columns must therefore 7, and employee Y or N must be shaped in these 7 columns.

    So for each branch, it will always be 1 row to have 7 columns:
    So I want that it resembles the following:
    BRANCH     CSO1      CSO2      CSO3         CSO4       CSO5     CSO6     CSO7
    -----------------------------------------------------------------------------------------------------------
    1001        Y - 321    N - 335    Y - 90050    N - 303
    Need to do using only the PL/SQL query

    Published by: imation3m on October 22, 2010 12:21

    http://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:766825833740
    http://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:15151874723724
    http://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:7644594042547

  • Divide the line into several columns

    Hi all

    I have a question where I want to divide the line into multiple columns based on the delimiter ' |'.

    Staging of Table structure: People_STG, I have people in it.

    Create table People_STG(col1 varchar2(4000));


    Insert into People_STG(Emp_id|) User name | FirstName. LastName. JobTitle | hire_date | Location_id)


    SELECT REGEXP_SUBSTR ( COL1, '[^,|]+', 1, 1) EMP_ID,
           REGEXP_SUBSTR ( COL1, '[^|]+', 1, 2) USERNAME,
           REGEXP_SUBSTR ( COL1, '[^,|]+', 1, 3) FIRSTNAME,
           REGEXP_SUBSTR ( COL1, '[^,|]+', 1, 4) LASTNAME,
           REGEXP_SUBSTR ( COL1, '[^,|]+', 1, 5) JOBTITLE,
           REGEXP_SUBSTR ( COL1, '[^,|]+', 1, 6) HIRE_DATE,
           REGEXP_SUBSTR ( COL1, '[^,|]+', 1, 7) LOCATION_ID
    FROM PEOPLE_STG
    WHERE length(regexp_replace(COL1, '[^|]', '')) = 14;
    

    But I am facing a problem here, as in some lines, function is null, but when I run the above query, it is not recognizing the empty element and inserting Hire_date values of function and location_id in Hire_date where function is null.

    for example: 1 | akash51 | Akaksh | C | 22/11/14 | 15

    Here the function is null, so when I run top to divide query it will insert 11/22/14 in the function column and 15 in Hire_Date.

    Please need help on this one.

    Oracle Version: 11.2 g

    Thanks in advance,

    Akash.

    There are different techniques to cope with this.  Is a simple...

    SQL > ed
    A written file afiedt.buf

    1 with t as (select 1 | akash51 |) Akaksh | C | 22/11/14 | 15' as col1 of union double all the
    2 Select 2 | akash52 | Akaksh | C | Jobs jobs | 23/11/14 | 15' of the double
    3            )
    4  --
    5. end of test data
    6  --
    7 select trim (REGEXP_SUBSTR (COL1, ' [^, |] +', 1, 1)) EMP_ID,.
    8 toppings (REGEXP_SUBSTR (COL1, ' [^, |] +', 1, 2)) USERNAME,.
    9 garnish (REGEXP_SUBSTR (COL1, ' [^, |] +' 1, 3)) FIRSTNAME,
    10 pads (REGEXP_SUBSTR (COL1, ' [^, |] +' 1, 4)) LASTNAME,.
    11 garnish (REGEXP_SUBSTR (COL1, ' [^, |] +' 1, 5)) JOBTITLE,.
    12 pads (REGEXP_SUBSTR (COL1, ' [^, |] +', 1, 6)) HIRE_DATE,.
    13 garnish (REGEXP_SUBSTR (COL1, ' [^, |] +' 1, 7)) location_id
    14 * from (select replace (col1, ' |)) ',' | as col1 of t)
    SQL > /.

    EMP_ID USERNAME FIRSTNAME LASTNAME, JOBTITLE HIRE_DATE LOCATION_I
    ---------- ---------- ---------- ---------- ---------- ---------- ----------
    1 akash51 Akaksh C 22/11/14 15
    2 akash52 Akaksh C Job 23/11/14 15

  • Hello, I have a full desktop in Adobe Muse, site and I want to convert it into a version Tablet &amp; phone, then, how would convert the site Office to Tablet &amp; phone to copy the entire office content and site structure? Thanks for any help.

    Hello, I have a full desktop in Adobe Muse, site and I want to convert it into a version Tablet & phone, then, how would convert the site Office to Tablet & phone to copy the entire office content and site structure? Thanks for any help.

    You can not automatically convert. Click on the "tablet" or "Phone" at the top of your plan view to create these versions.

    You can then copy and paste on any content you want has more of your office.

  • How to convert a PDF into a jpeg file

    How to convert a PDF into a jpeg file

    Hello

    If you use the drive, would not be possible because it is not apt software to convert PDF to any other form of file including jpeg.

    You would need either to pack PDF, which is an online service to export PDFS for some file formats, or you would need Acrobat to perform this action.

    Please follow the steps to convert pdf to word.


    Launch Acrobat > open the PDF > navigate to file > export to > Image > Jpeg.

    Concerning

    Sukrit diallo

  • How to convert pdf files into word?

    Hello

    How to convert pdf files into word?  do I need to purchase software?

    You can do this with the free Adobe Acrobat Reader DC but yes, you should always buy a subscription to ExportPDF.

  • view the lines of columns

    I have a table which has used and services

    I have question
    Select d.department_name, count (*)
    e employees, departments d
    where
    e.department_id = d.department_id
    D.department_name group;

    This will return charges and the name of the Department.

    Can any body guide me how to display these rows into columns.

    using pivot or some other way.

    Thank you
    Lavan

    It is explained, including examples in the SQL and PL/SQL FAQ: {message identifier: = 9360005}
    If you need more help then post a unit test and your version of the database , as explained here: {message identifier: = 9360002}

  • How to load the image into the Blob field in Sql?

    Hello

    I want to know how to load the image into the Blob field in Sql

    Concerning


    Therese

    Re: How to load the image into the Long Raw column?

  • How to choose the line PFI to use on the port of the 5761?

    I understand that the VIDEO that I use on my DAC 5761 allows me to access 8 PFI lines.  However, when I access my 5761 devices in the project window by clicking the "+" next to the name of the module, I see that 'PFI Input' and ' output PFI.  I tried to get this to send a signal in the past, but I have been unable to determine how to select the line PFI I want to use.   How can I select the PFI line?

    Any ideas?  I was able to get the my AWG PFI (5422) for work and it is amazing.  I want to order an attenuator digital step for a ground penetration radar that I develop.

    Your time and your help is very appreciated.

    PS: I have worked with this PXI system (chassis, embedded controller, FPGA digitizer 5761 and AWG) for 1 year and I am afraid that it is time to learn this one way or another.

    Good day!

    -Daniel

    I found the solution. In addistion to the node which is visible in my previous post, there must be a node to enable PFI write with an entry of decimal number indicating the line is activated. Then, there must be a node PFI active connector, set to true. Not sure if the node of the latter is really necessary, however. Otherwise above examples should work as is.

Maybe you are looking for

  • Bose Soundlink Bluetooth no longer works with El Capitan

    I just upgraded to El Capitan on my iMac (2007) 10.6.8 antique. My Bose Soundlink bluetooth device still appears in the sound preferences panel, the two th USB stick and the receiver on the Bose device light up, but I have no sound. How can I fix?

  • intermittent access WiFi hotspot

    Thanks in advance if anyone can offer some insight from the information that I'll summarize... I recently attended a convention, and free WiFi is available on the site.  I have had no problems connecting to the wireless network, good signal, but more

  • Could use a little help from the system recovery

    My son has a HP DV something 21XX Entertainment laptop computer running XP. He had some serious spyware that I couldn't get cleaned up and I "discovered" restoration of the system using the F10 key. I did it. He acted as a whole new computer I have r

  • Delay on the 'Starting Windows' screen

    The problem I have is this, whenever I start my PC there is a delay on the 'Starting Windows' screen. The screen does not freeze or anything just ten minutes to bring up the Log In screen. It's the only time where that the PC is delayed, it has no ef

  • Authorization of RADIUS WebVPN ASA

    Hi, guys. I'm working on an ASA 5510 and plan to work as a waiter webvpn. Currently I am facing a Raius permission problem. I can't config Raiuds AV pair in ACS server to designate the webvpn different policies for each group of users. Until I have i