How to get the values of different rows into single row in Oracle












0















I need to select data from different rows returning one row.
I have table like this:



SNO USEFUL COUNTER PINCODE SUBSNO    DATA                            REFERENCE
--- ------ ------- ------- ------ ----------------------------- ---------
1 Y null 504293 null 504293 Sl.No
2 null null 504293 null 1 null
3 null null 504293 null iciseva00031 null
4 null null 504293 null SANTHOSH KUMAR null
5 null null 504293 null SANTHOSH MANTHENA null
6 null null 504293 null 12-52 BRAHMAN WADA null
7 null null 504293 null ASIFABAD null
8 null null 504293 null Andhra Pradesh null
9 null null 504293 null 9248859222 null
10 null null 504293 null 2 null
11 null null 504293 null WSGLDPL12415 null
12 null null 504293 null SIDDHARTHA COMMUNICATIONS null
13 null null 504293 null MASADE SATISH null
14 null null 504293 null HNO10-143POSTOFFICEROADBAPUNAGAR null
ASIFABADDISTKUMRAMBHEEMASIFABAD
15 null null 504293 null ADILABAD null
16 null null 504293 null ANDHRA PRADESH null
17 null null 504293 null 9059187009 null


And I have to return output like this:



PINCODE SUBSNO USER          COMPANY       AGENT            ADDRESS CITY         STATE        PHONE
------- ------ ---- ------- ----- ------- ---- ----- -----
504293 1 iciseva00031 SANTHOSHKUMAR SANTHOSHMANTHENA 12-52 BRAHMAN WADA ASIFABAD Andhra Pradesh 9248859222


Please help me.










share|improve this question

























  • Data are missing in source table, for example phone.

    – mjpolak
    Sep 16 '17 at 8:09
















0















I need to select data from different rows returning one row.
I have table like this:



SNO USEFUL COUNTER PINCODE SUBSNO    DATA                            REFERENCE
--- ------ ------- ------- ------ ----------------------------- ---------
1 Y null 504293 null 504293 Sl.No
2 null null 504293 null 1 null
3 null null 504293 null iciseva00031 null
4 null null 504293 null SANTHOSH KUMAR null
5 null null 504293 null SANTHOSH MANTHENA null
6 null null 504293 null 12-52 BRAHMAN WADA null
7 null null 504293 null ASIFABAD null
8 null null 504293 null Andhra Pradesh null
9 null null 504293 null 9248859222 null
10 null null 504293 null 2 null
11 null null 504293 null WSGLDPL12415 null
12 null null 504293 null SIDDHARTHA COMMUNICATIONS null
13 null null 504293 null MASADE SATISH null
14 null null 504293 null HNO10-143POSTOFFICEROADBAPUNAGAR null
ASIFABADDISTKUMRAMBHEEMASIFABAD
15 null null 504293 null ADILABAD null
16 null null 504293 null ANDHRA PRADESH null
17 null null 504293 null 9059187009 null


And I have to return output like this:



PINCODE SUBSNO USER          COMPANY       AGENT            ADDRESS CITY         STATE        PHONE
------- ------ ---- ------- ----- ------- ---- ----- -----
504293 1 iciseva00031 SANTHOSHKUMAR SANTHOSHMANTHENA 12-52 BRAHMAN WADA ASIFABAD Andhra Pradesh 9248859222


Please help me.










share|improve this question

























  • Data are missing in source table, for example phone.

    – mjpolak
    Sep 16 '17 at 8:09














0












0








0








I need to select data from different rows returning one row.
I have table like this:



SNO USEFUL COUNTER PINCODE SUBSNO    DATA                            REFERENCE
--- ------ ------- ------- ------ ----------------------------- ---------
1 Y null 504293 null 504293 Sl.No
2 null null 504293 null 1 null
3 null null 504293 null iciseva00031 null
4 null null 504293 null SANTHOSH KUMAR null
5 null null 504293 null SANTHOSH MANTHENA null
6 null null 504293 null 12-52 BRAHMAN WADA null
7 null null 504293 null ASIFABAD null
8 null null 504293 null Andhra Pradesh null
9 null null 504293 null 9248859222 null
10 null null 504293 null 2 null
11 null null 504293 null WSGLDPL12415 null
12 null null 504293 null SIDDHARTHA COMMUNICATIONS null
13 null null 504293 null MASADE SATISH null
14 null null 504293 null HNO10-143POSTOFFICEROADBAPUNAGAR null
ASIFABADDISTKUMRAMBHEEMASIFABAD
15 null null 504293 null ADILABAD null
16 null null 504293 null ANDHRA PRADESH null
17 null null 504293 null 9059187009 null


And I have to return output like this:



PINCODE SUBSNO USER          COMPANY       AGENT            ADDRESS CITY         STATE        PHONE
------- ------ ---- ------- ----- ------- ---- ----- -----
504293 1 iciseva00031 SANTHOSHKUMAR SANTHOSHMANTHENA 12-52 BRAHMAN WADA ASIFABAD Andhra Pradesh 9248859222


Please help me.










share|improve this question
















I need to select data from different rows returning one row.
I have table like this:



SNO USEFUL COUNTER PINCODE SUBSNO    DATA                            REFERENCE
--- ------ ------- ------- ------ ----------------------------- ---------
1 Y null 504293 null 504293 Sl.No
2 null null 504293 null 1 null
3 null null 504293 null iciseva00031 null
4 null null 504293 null SANTHOSH KUMAR null
5 null null 504293 null SANTHOSH MANTHENA null
6 null null 504293 null 12-52 BRAHMAN WADA null
7 null null 504293 null ASIFABAD null
8 null null 504293 null Andhra Pradesh null
9 null null 504293 null 9248859222 null
10 null null 504293 null 2 null
11 null null 504293 null WSGLDPL12415 null
12 null null 504293 null SIDDHARTHA COMMUNICATIONS null
13 null null 504293 null MASADE SATISH null
14 null null 504293 null HNO10-143POSTOFFICEROADBAPUNAGAR null
ASIFABADDISTKUMRAMBHEEMASIFABAD
15 null null 504293 null ADILABAD null
16 null null 504293 null ANDHRA PRADESH null
17 null null 504293 null 9059187009 null


And I have to return output like this:



PINCODE SUBSNO USER          COMPANY       AGENT            ADDRESS CITY         STATE        PHONE
------- ------ ---- ------- ----- ------- ---- ----- -----
504293 1 iciseva00031 SANTHOSHKUMAR SANTHOSHMANTHENA 12-52 BRAHMAN WADA ASIFABAD Andhra Pradesh 9248859222


Please help me.







oracle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 16 '17 at 15:20









APC

119k15118229




119k15118229










asked Sep 16 '17 at 8:03









Hemanth sai kumarHemanth sai kumar

11




11













  • Data are missing in source table, for example phone.

    – mjpolak
    Sep 16 '17 at 8:09



















  • Data are missing in source table, for example phone.

    – mjpolak
    Sep 16 '17 at 8:09

















Data are missing in source table, for example phone.

– mjpolak
Sep 16 '17 at 8:09





Data are missing in source table, for example phone.

– mjpolak
Sep 16 '17 at 8:09












1 Answer
1






active

oldest

votes


















0















" I have to show output table like this:"




It's easy enough to grab that one row (use an editor that has regex support to help wrangle the repetition):



select t1.data as PINCODE
, t2.data as SUBSNO
, t3.data as USER
, t4.data as COMPANY
, t5.data as AGENT
, t6.data as ADDRESS
, t7.data as CITY
, t8.data as STATE
, t9.data as PHONE
from ( select PINCODE, DATA from your_table where PINCODE = DATA ) t1
left join ( select PINCODE, DATA from your_table where SNO = 2) t2
on t2.pincode = t1.pincode
left join ( select PINCODE, DATA from your_table where SNO = 3) t3
on t3.pincode = t1.pincode
left join ( select PINCODE, DATA from your_table where SNO = 4) t4
on t4.pincode = t1.pincode
left join ( select PINCODE, DATA from your_table where SNO = 5) t5
on t5.pincode = t1.pincode
left join ( select PINCODE, DATA from your_table where SNO = 6) t6
on t6.pincode = t1.pincode
left join ( select PINCODE, DATA from your_table where SNO = 7) t7
on t7.pincode = t1.pincode
left join ( select PINCODE, DATA from your_table where SNO = 8) t8
on t8.pincode = t1.pincode
left join ( select PINCODE, DATA from your_table where SNO = 9) t9
on t9.pincode = t1.pincode
where t1.pincode = '504293'
/


Of course, this is probably not what you need. No doubt you want to have all the additional rows - sno >= 10 too. Simple: just keep repeating the above process for as many columns as you need.



Notes.





  1. Do you have to hard code the column names in the projection? Yes. The presented value table has no attribute type indicator so there's no way of knowing what the column represents. So you need to inject that missing intelligence into the query yourself.


  2. If it turns out the table does have an attribute type column, can you use that to label the columns in the projection? Yes, but it means dynamically generating the query.


  3. What happens if another PINCODE holds the columns in a different order? How will you tell? Without an attribute type indicator you don't know what any column is supposed to be, all you have to go on is the presumed precedence represented by your sample output.


  4. All those joins: won't the performance suck? Yes, yes it probably will.


Unfortunately for you there is no simple. elegant, performative solution because the source data is a car crash.



These generic data storage tables are seductive for developers writing data input routines. They get to stash the data in a very simple manner and don't have to worry themselves with data modelling and all that tedious stuff. Plus it's super flexible and future-proof. But what they have actually done is chalked up a massive pile of technical debt, which has to be paid off by the developers who need to get the data out.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46251649%2fhow-to-get-the-values-of-different-rows-into-single-row-in-oracle%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















    " I have to show output table like this:"




    It's easy enough to grab that one row (use an editor that has regex support to help wrangle the repetition):



    select t1.data as PINCODE
    , t2.data as SUBSNO
    , t3.data as USER
    , t4.data as COMPANY
    , t5.data as AGENT
    , t6.data as ADDRESS
    , t7.data as CITY
    , t8.data as STATE
    , t9.data as PHONE
    from ( select PINCODE, DATA from your_table where PINCODE = DATA ) t1
    left join ( select PINCODE, DATA from your_table where SNO = 2) t2
    on t2.pincode = t1.pincode
    left join ( select PINCODE, DATA from your_table where SNO = 3) t3
    on t3.pincode = t1.pincode
    left join ( select PINCODE, DATA from your_table where SNO = 4) t4
    on t4.pincode = t1.pincode
    left join ( select PINCODE, DATA from your_table where SNO = 5) t5
    on t5.pincode = t1.pincode
    left join ( select PINCODE, DATA from your_table where SNO = 6) t6
    on t6.pincode = t1.pincode
    left join ( select PINCODE, DATA from your_table where SNO = 7) t7
    on t7.pincode = t1.pincode
    left join ( select PINCODE, DATA from your_table where SNO = 8) t8
    on t8.pincode = t1.pincode
    left join ( select PINCODE, DATA from your_table where SNO = 9) t9
    on t9.pincode = t1.pincode
    where t1.pincode = '504293'
    /


    Of course, this is probably not what you need. No doubt you want to have all the additional rows - sno >= 10 too. Simple: just keep repeating the above process for as many columns as you need.



    Notes.





    1. Do you have to hard code the column names in the projection? Yes. The presented value table has no attribute type indicator so there's no way of knowing what the column represents. So you need to inject that missing intelligence into the query yourself.


    2. If it turns out the table does have an attribute type column, can you use that to label the columns in the projection? Yes, but it means dynamically generating the query.


    3. What happens if another PINCODE holds the columns in a different order? How will you tell? Without an attribute type indicator you don't know what any column is supposed to be, all you have to go on is the presumed precedence represented by your sample output.


    4. All those joins: won't the performance suck? Yes, yes it probably will.


    Unfortunately for you there is no simple. elegant, performative solution because the source data is a car crash.



    These generic data storage tables are seductive for developers writing data input routines. They get to stash the data in a very simple manner and don't have to worry themselves with data modelling and all that tedious stuff. Plus it's super flexible and future-proof. But what they have actually done is chalked up a massive pile of technical debt, which has to be paid off by the developers who need to get the data out.






    share|improve this answer






























      0















      " I have to show output table like this:"




      It's easy enough to grab that one row (use an editor that has regex support to help wrangle the repetition):



      select t1.data as PINCODE
      , t2.data as SUBSNO
      , t3.data as USER
      , t4.data as COMPANY
      , t5.data as AGENT
      , t6.data as ADDRESS
      , t7.data as CITY
      , t8.data as STATE
      , t9.data as PHONE
      from ( select PINCODE, DATA from your_table where PINCODE = DATA ) t1
      left join ( select PINCODE, DATA from your_table where SNO = 2) t2
      on t2.pincode = t1.pincode
      left join ( select PINCODE, DATA from your_table where SNO = 3) t3
      on t3.pincode = t1.pincode
      left join ( select PINCODE, DATA from your_table where SNO = 4) t4
      on t4.pincode = t1.pincode
      left join ( select PINCODE, DATA from your_table where SNO = 5) t5
      on t5.pincode = t1.pincode
      left join ( select PINCODE, DATA from your_table where SNO = 6) t6
      on t6.pincode = t1.pincode
      left join ( select PINCODE, DATA from your_table where SNO = 7) t7
      on t7.pincode = t1.pincode
      left join ( select PINCODE, DATA from your_table where SNO = 8) t8
      on t8.pincode = t1.pincode
      left join ( select PINCODE, DATA from your_table where SNO = 9) t9
      on t9.pincode = t1.pincode
      where t1.pincode = '504293'
      /


      Of course, this is probably not what you need. No doubt you want to have all the additional rows - sno >= 10 too. Simple: just keep repeating the above process for as many columns as you need.



      Notes.





      1. Do you have to hard code the column names in the projection? Yes. The presented value table has no attribute type indicator so there's no way of knowing what the column represents. So you need to inject that missing intelligence into the query yourself.


      2. If it turns out the table does have an attribute type column, can you use that to label the columns in the projection? Yes, but it means dynamically generating the query.


      3. What happens if another PINCODE holds the columns in a different order? How will you tell? Without an attribute type indicator you don't know what any column is supposed to be, all you have to go on is the presumed precedence represented by your sample output.


      4. All those joins: won't the performance suck? Yes, yes it probably will.


      Unfortunately for you there is no simple. elegant, performative solution because the source data is a car crash.



      These generic data storage tables are seductive for developers writing data input routines. They get to stash the data in a very simple manner and don't have to worry themselves with data modelling and all that tedious stuff. Plus it's super flexible and future-proof. But what they have actually done is chalked up a massive pile of technical debt, which has to be paid off by the developers who need to get the data out.






      share|improve this answer




























        0












        0








        0








        " I have to show output table like this:"




        It's easy enough to grab that one row (use an editor that has regex support to help wrangle the repetition):



        select t1.data as PINCODE
        , t2.data as SUBSNO
        , t3.data as USER
        , t4.data as COMPANY
        , t5.data as AGENT
        , t6.data as ADDRESS
        , t7.data as CITY
        , t8.data as STATE
        , t9.data as PHONE
        from ( select PINCODE, DATA from your_table where PINCODE = DATA ) t1
        left join ( select PINCODE, DATA from your_table where SNO = 2) t2
        on t2.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 3) t3
        on t3.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 4) t4
        on t4.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 5) t5
        on t5.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 6) t6
        on t6.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 7) t7
        on t7.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 8) t8
        on t8.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 9) t9
        on t9.pincode = t1.pincode
        where t1.pincode = '504293'
        /


        Of course, this is probably not what you need. No doubt you want to have all the additional rows - sno >= 10 too. Simple: just keep repeating the above process for as many columns as you need.



        Notes.





        1. Do you have to hard code the column names in the projection? Yes. The presented value table has no attribute type indicator so there's no way of knowing what the column represents. So you need to inject that missing intelligence into the query yourself.


        2. If it turns out the table does have an attribute type column, can you use that to label the columns in the projection? Yes, but it means dynamically generating the query.


        3. What happens if another PINCODE holds the columns in a different order? How will you tell? Without an attribute type indicator you don't know what any column is supposed to be, all you have to go on is the presumed precedence represented by your sample output.


        4. All those joins: won't the performance suck? Yes, yes it probably will.


        Unfortunately for you there is no simple. elegant, performative solution because the source data is a car crash.



        These generic data storage tables are seductive for developers writing data input routines. They get to stash the data in a very simple manner and don't have to worry themselves with data modelling and all that tedious stuff. Plus it's super flexible and future-proof. But what they have actually done is chalked up a massive pile of technical debt, which has to be paid off by the developers who need to get the data out.






        share|improve this answer
















        " I have to show output table like this:"




        It's easy enough to grab that one row (use an editor that has regex support to help wrangle the repetition):



        select t1.data as PINCODE
        , t2.data as SUBSNO
        , t3.data as USER
        , t4.data as COMPANY
        , t5.data as AGENT
        , t6.data as ADDRESS
        , t7.data as CITY
        , t8.data as STATE
        , t9.data as PHONE
        from ( select PINCODE, DATA from your_table where PINCODE = DATA ) t1
        left join ( select PINCODE, DATA from your_table where SNO = 2) t2
        on t2.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 3) t3
        on t3.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 4) t4
        on t4.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 5) t5
        on t5.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 6) t6
        on t6.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 7) t7
        on t7.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 8) t8
        on t8.pincode = t1.pincode
        left join ( select PINCODE, DATA from your_table where SNO = 9) t9
        on t9.pincode = t1.pincode
        where t1.pincode = '504293'
        /


        Of course, this is probably not what you need. No doubt you want to have all the additional rows - sno >= 10 too. Simple: just keep repeating the above process for as many columns as you need.



        Notes.





        1. Do you have to hard code the column names in the projection? Yes. The presented value table has no attribute type indicator so there's no way of knowing what the column represents. So you need to inject that missing intelligence into the query yourself.


        2. If it turns out the table does have an attribute type column, can you use that to label the columns in the projection? Yes, but it means dynamically generating the query.


        3. What happens if another PINCODE holds the columns in a different order? How will you tell? Without an attribute type indicator you don't know what any column is supposed to be, all you have to go on is the presumed precedence represented by your sample output.


        4. All those joins: won't the performance suck? Yes, yes it probably will.


        Unfortunately for you there is no simple. elegant, performative solution because the source data is a car crash.



        These generic data storage tables are seductive for developers writing data input routines. They get to stash the data in a very simple manner and don't have to worry themselves with data modelling and all that tedious stuff. Plus it's super flexible and future-proof. But what they have actually done is chalked up a massive pile of technical debt, which has to be paid off by the developers who need to get the data out.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 16:07

























        answered Sep 16 '17 at 9:21









        APCAPC

        119k15118229




        119k15118229
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46251649%2fhow-to-get-the-values-of-different-rows-into-single-row-in-oracle%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin