SQL to fetch row into column based on con





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a table in a below structure.and i want to create a sql which can fetch record in a specific order.






Name	Phone	B.Email           P.Email
Jack 12 ab12@test.com a21@test.com
Ram 12345 null Ram@test.com
Raja 543 Raj@test.om null
Rik 122 null null





Output i want in below order






Name	Phone	        EMAIL			IsPrimary	EmailType
Jack 123 ab12@test.com Y B
Jack 123 a21@test.com N P
Ram 321 Ram@test.com Y P
Raja 543 Raj@test.om Y B





if there is B.Email, then it will be primary, else P.Email will be primary. IF both the emails are null then that record wil not be there in the output.










share|improve this question























  • I see in our output for Ram IsPrimary is 'N' ...please clarify

    – kanagaraj
    Nov 16 '18 at 18:43


















0















I have a table in a below structure.and i want to create a sql which can fetch record in a specific order.






Name	Phone	B.Email           P.Email
Jack 12 ab12@test.com a21@test.com
Ram 12345 null Ram@test.com
Raja 543 Raj@test.om null
Rik 122 null null





Output i want in below order






Name	Phone	        EMAIL			IsPrimary	EmailType
Jack 123 ab12@test.com Y B
Jack 123 a21@test.com N P
Ram 321 Ram@test.com Y P
Raja 543 Raj@test.om Y B





if there is B.Email, then it will be primary, else P.Email will be primary. IF both the emails are null then that record wil not be there in the output.










share|improve this question























  • I see in our output for Ram IsPrimary is 'N' ...please clarify

    – kanagaraj
    Nov 16 '18 at 18:43














0












0








0








I have a table in a below structure.and i want to create a sql which can fetch record in a specific order.






Name	Phone	B.Email           P.Email
Jack 12 ab12@test.com a21@test.com
Ram 12345 null Ram@test.com
Raja 543 Raj@test.om null
Rik 122 null null





Output i want in below order






Name	Phone	        EMAIL			IsPrimary	EmailType
Jack 123 ab12@test.com Y B
Jack 123 a21@test.com N P
Ram 321 Ram@test.com Y P
Raja 543 Raj@test.om Y B





if there is B.Email, then it will be primary, else P.Email will be primary. IF both the emails are null then that record wil not be there in the output.










share|improve this question














I have a table in a below structure.and i want to create a sql which can fetch record in a specific order.






Name	Phone	B.Email           P.Email
Jack 12 ab12@test.com a21@test.com
Ram 12345 null Ram@test.com
Raja 543 Raj@test.om null
Rik 122 null null





Output i want in below order






Name	Phone	        EMAIL			IsPrimary	EmailType
Jack 123 ab12@test.com Y B
Jack 123 a21@test.com N P
Ram 321 Ram@test.com Y P
Raja 543 Raj@test.om Y B





if there is B.Email, then it will be primary, else P.Email will be primary. IF both the emails are null then that record wil not be there in the output.






Name	Phone	B.Email           P.Email
Jack 12 ab12@test.com a21@test.com
Ram 12345 null Ram@test.com
Raja 543 Raj@test.om null
Rik 122 null null





Name	Phone	B.Email           P.Email
Jack 12 ab12@test.com a21@test.com
Ram 12345 null Ram@test.com
Raja 543 Raj@test.om null
Rik 122 null null





Name	Phone	        EMAIL			IsPrimary	EmailType
Jack 123 ab12@test.com Y B
Jack 123 a21@test.com N P
Ram 321 Ram@test.com Y P
Raja 543 Raj@test.om Y B





Name	Phone	        EMAIL			IsPrimary	EmailType
Jack 123 ab12@test.com Y B
Jack 123 a21@test.com N P
Ram 321 Ram@test.com Y P
Raja 543 Raj@test.om Y B






sql oracle etl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 16:00









Piyush SinghPiyush Singh

326




326













  • I see in our output for Ram IsPrimary is 'N' ...please clarify

    – kanagaraj
    Nov 16 '18 at 18:43



















  • I see in our output for Ram IsPrimary is 'N' ...please clarify

    – kanagaraj
    Nov 16 '18 at 18:43

















I see in our output for Ram IsPrimary is 'N' ...please clarify

– kanagaraj
Nov 16 '18 at 18:43





I see in our output for Ram IsPrimary is 'N' ...please clarify

– kanagaraj
Nov 16 '18 at 18:43












4 Answers
4






active

oldest

votes


















0














Is this what you want?



order by phone, name, isprimary desc





share|improve this answer































    0














    I used a UNION to get first people with b.email and then those with p.email and CASE to sort out primary and type



    SELECT name, phone, b.email EMAIL, 'Y' IsPrimary,
    CASE WHEN b.email IS NULL THEN 'P'
    ELSE 'B'
    END EmailType
    FROM mail
    WHERE b.email IS NOT NULL
    UNION
    SELECT name, phone, p.email EMAIL,
    CASE WHEN b.email IS NULL THEN 'Y'
    ELSE 'N'
    END IsPrimary,
    CASE WHEN b.email IS NULL THEN 'P'
    ELSE 'B'
    END EmailType
    FROM mail
    WHERE p.email IS NOT NULL
    ORDER BY name





    share|improve this answer































      0














      The other answers are close, but I think they all have errors in their output.



      Not sure what SQL engine this is, here's my answer for MySQL5.6:



      SELECT name,
      phone,
      CASE WHEN BEmail IS NOT NULL THEN BEMail ELSE PEmail END as EMAIL,
      'Y' as IsPrimary,
      CASE WHEN BEmail IS NOT NULL THEN 'B' ELSE 'P' END as EmailType
      FROM table_name t
      WHERE NOT (BEmail IS NULL AND PEmail IS NULL)
      UNION ALL
      SELECT name, phone, PEmail, 'N', 'P'
      from table_name
      WHERE BEmail IS NOT NULL
      AND PEmail IS NOT NULL
      ORDER BY Name;


      Note that I've changed B.Email to BEmail, P.Email to PEMail, and the table is called "table_name".



      SQL Fiddle here






      share|improve this answer































        0














        Try below query :



        create table     Table3
        (Name varchar2(20),
        phone number,
        "B.mail" varchar2(50),
        "P.mail" varchar2(50));

        insert into Table3 values ('Jack',12,'ab12@test.com','a21@test.com');
        insert into Table3 values ('Ram',12345,null,'Ram@test.com');
        insert into Table3 values ('Raja',543,'Raj@test.com',null);
        insert into Table3 values ('Rik',122,null,null);

        SELECT Name, Phone, COALESCE("B.mail","P.mail") Email, CASE WHEN "B.mail" IS NOT NULL THEN 'Y' ELSE 'N' END as IsPrimary,CASE WHEN "B.mail" IS NOT NULL THEN 'B' ELSE 'P' END as EmailType
        FROM Table3 t
        where "B.mail" IS NOT NULL OR "P.mail" IS NOT NULL
        UNION
        SELECT Name, Phone, COALESCE("P.mail","B.mail") Email, CASE WHEN "P.mail" IS NOT NULL THEN 'N' ELSE 'Y' END as IsPrimary,CASE WHEN "P.mail" IS NOT NULL THEN 'P' ELSE 'B' END as EmailType
        FROM Table3 t
        where "B.mail" IS NOT NULL OR "P.mail" IS NOT NULL





        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%2f53341427%2fsql-to-fetch-row-into-column-based-on-con%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Is this what you want?



          order by phone, name, isprimary desc





          share|improve this answer




























            0














            Is this what you want?



            order by phone, name, isprimary desc





            share|improve this answer


























              0












              0








              0







              Is this what you want?



              order by phone, name, isprimary desc





              share|improve this answer













              Is this what you want?



              order by phone, name, isprimary desc






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 16 '18 at 16:03









              Gordon LinoffGordon Linoff

              797k37318423




              797k37318423

























                  0














                  I used a UNION to get first people with b.email and then those with p.email and CASE to sort out primary and type



                  SELECT name, phone, b.email EMAIL, 'Y' IsPrimary,
                  CASE WHEN b.email IS NULL THEN 'P'
                  ELSE 'B'
                  END EmailType
                  FROM mail
                  WHERE b.email IS NOT NULL
                  UNION
                  SELECT name, phone, p.email EMAIL,
                  CASE WHEN b.email IS NULL THEN 'Y'
                  ELSE 'N'
                  END IsPrimary,
                  CASE WHEN b.email IS NULL THEN 'P'
                  ELSE 'B'
                  END EmailType
                  FROM mail
                  WHERE p.email IS NOT NULL
                  ORDER BY name





                  share|improve this answer




























                    0














                    I used a UNION to get first people with b.email and then those with p.email and CASE to sort out primary and type



                    SELECT name, phone, b.email EMAIL, 'Y' IsPrimary,
                    CASE WHEN b.email IS NULL THEN 'P'
                    ELSE 'B'
                    END EmailType
                    FROM mail
                    WHERE b.email IS NOT NULL
                    UNION
                    SELECT name, phone, p.email EMAIL,
                    CASE WHEN b.email IS NULL THEN 'Y'
                    ELSE 'N'
                    END IsPrimary,
                    CASE WHEN b.email IS NULL THEN 'P'
                    ELSE 'B'
                    END EmailType
                    FROM mail
                    WHERE p.email IS NOT NULL
                    ORDER BY name





                    share|improve this answer


























                      0












                      0








                      0







                      I used a UNION to get first people with b.email and then those with p.email and CASE to sort out primary and type



                      SELECT name, phone, b.email EMAIL, 'Y' IsPrimary,
                      CASE WHEN b.email IS NULL THEN 'P'
                      ELSE 'B'
                      END EmailType
                      FROM mail
                      WHERE b.email IS NOT NULL
                      UNION
                      SELECT name, phone, p.email EMAIL,
                      CASE WHEN b.email IS NULL THEN 'Y'
                      ELSE 'N'
                      END IsPrimary,
                      CASE WHEN b.email IS NULL THEN 'P'
                      ELSE 'B'
                      END EmailType
                      FROM mail
                      WHERE p.email IS NOT NULL
                      ORDER BY name





                      share|improve this answer













                      I used a UNION to get first people with b.email and then those with p.email and CASE to sort out primary and type



                      SELECT name, phone, b.email EMAIL, 'Y' IsPrimary,
                      CASE WHEN b.email IS NULL THEN 'P'
                      ELSE 'B'
                      END EmailType
                      FROM mail
                      WHERE b.email IS NOT NULL
                      UNION
                      SELECT name, phone, p.email EMAIL,
                      CASE WHEN b.email IS NULL THEN 'Y'
                      ELSE 'N'
                      END IsPrimary,
                      CASE WHEN b.email IS NULL THEN 'P'
                      ELSE 'B'
                      END EmailType
                      FROM mail
                      WHERE p.email IS NOT NULL
                      ORDER BY name






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 16 '18 at 16:31









                      Joakim DanielsonJoakim Danielson

                      10.8k3725




                      10.8k3725























                          0














                          The other answers are close, but I think they all have errors in their output.



                          Not sure what SQL engine this is, here's my answer for MySQL5.6:



                          SELECT name,
                          phone,
                          CASE WHEN BEmail IS NOT NULL THEN BEMail ELSE PEmail END as EMAIL,
                          'Y' as IsPrimary,
                          CASE WHEN BEmail IS NOT NULL THEN 'B' ELSE 'P' END as EmailType
                          FROM table_name t
                          WHERE NOT (BEmail IS NULL AND PEmail IS NULL)
                          UNION ALL
                          SELECT name, phone, PEmail, 'N', 'P'
                          from table_name
                          WHERE BEmail IS NOT NULL
                          AND PEmail IS NOT NULL
                          ORDER BY Name;


                          Note that I've changed B.Email to BEmail, P.Email to PEMail, and the table is called "table_name".



                          SQL Fiddle here






                          share|improve this answer




























                            0














                            The other answers are close, but I think they all have errors in their output.



                            Not sure what SQL engine this is, here's my answer for MySQL5.6:



                            SELECT name,
                            phone,
                            CASE WHEN BEmail IS NOT NULL THEN BEMail ELSE PEmail END as EMAIL,
                            'Y' as IsPrimary,
                            CASE WHEN BEmail IS NOT NULL THEN 'B' ELSE 'P' END as EmailType
                            FROM table_name t
                            WHERE NOT (BEmail IS NULL AND PEmail IS NULL)
                            UNION ALL
                            SELECT name, phone, PEmail, 'N', 'P'
                            from table_name
                            WHERE BEmail IS NOT NULL
                            AND PEmail IS NOT NULL
                            ORDER BY Name;


                            Note that I've changed B.Email to BEmail, P.Email to PEMail, and the table is called "table_name".



                            SQL Fiddle here






                            share|improve this answer


























                              0












                              0








                              0







                              The other answers are close, but I think they all have errors in their output.



                              Not sure what SQL engine this is, here's my answer for MySQL5.6:



                              SELECT name,
                              phone,
                              CASE WHEN BEmail IS NOT NULL THEN BEMail ELSE PEmail END as EMAIL,
                              'Y' as IsPrimary,
                              CASE WHEN BEmail IS NOT NULL THEN 'B' ELSE 'P' END as EmailType
                              FROM table_name t
                              WHERE NOT (BEmail IS NULL AND PEmail IS NULL)
                              UNION ALL
                              SELECT name, phone, PEmail, 'N', 'P'
                              from table_name
                              WHERE BEmail IS NOT NULL
                              AND PEmail IS NOT NULL
                              ORDER BY Name;


                              Note that I've changed B.Email to BEmail, P.Email to PEMail, and the table is called "table_name".



                              SQL Fiddle here






                              share|improve this answer













                              The other answers are close, but I think they all have errors in their output.



                              Not sure what SQL engine this is, here's my answer for MySQL5.6:



                              SELECT name,
                              phone,
                              CASE WHEN BEmail IS NOT NULL THEN BEMail ELSE PEmail END as EMAIL,
                              'Y' as IsPrimary,
                              CASE WHEN BEmail IS NOT NULL THEN 'B' ELSE 'P' END as EmailType
                              FROM table_name t
                              WHERE NOT (BEmail IS NULL AND PEmail IS NULL)
                              UNION ALL
                              SELECT name, phone, PEmail, 'N', 'P'
                              from table_name
                              WHERE BEmail IS NOT NULL
                              AND PEmail IS NOT NULL
                              ORDER BY Name;


                              Note that I've changed B.Email to BEmail, P.Email to PEMail, and the table is called "table_name".



                              SQL Fiddle here







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 16 '18 at 16:54









                              user2752159user2752159

                              1421221




                              1421221























                                  0














                                  Try below query :



                                  create table     Table3
                                  (Name varchar2(20),
                                  phone number,
                                  "B.mail" varchar2(50),
                                  "P.mail" varchar2(50));

                                  insert into Table3 values ('Jack',12,'ab12@test.com','a21@test.com');
                                  insert into Table3 values ('Ram',12345,null,'Ram@test.com');
                                  insert into Table3 values ('Raja',543,'Raj@test.com',null);
                                  insert into Table3 values ('Rik',122,null,null);

                                  SELECT Name, Phone, COALESCE("B.mail","P.mail") Email, CASE WHEN "B.mail" IS NOT NULL THEN 'Y' ELSE 'N' END as IsPrimary,CASE WHEN "B.mail" IS NOT NULL THEN 'B' ELSE 'P' END as EmailType
                                  FROM Table3 t
                                  where "B.mail" IS NOT NULL OR "P.mail" IS NOT NULL
                                  UNION
                                  SELECT Name, Phone, COALESCE("P.mail","B.mail") Email, CASE WHEN "P.mail" IS NOT NULL THEN 'N' ELSE 'Y' END as IsPrimary,CASE WHEN "P.mail" IS NOT NULL THEN 'P' ELSE 'B' END as EmailType
                                  FROM Table3 t
                                  where "B.mail" IS NOT NULL OR "P.mail" IS NOT NULL





                                  share|improve this answer




























                                    0














                                    Try below query :



                                    create table     Table3
                                    (Name varchar2(20),
                                    phone number,
                                    "B.mail" varchar2(50),
                                    "P.mail" varchar2(50));

                                    insert into Table3 values ('Jack',12,'ab12@test.com','a21@test.com');
                                    insert into Table3 values ('Ram',12345,null,'Ram@test.com');
                                    insert into Table3 values ('Raja',543,'Raj@test.com',null);
                                    insert into Table3 values ('Rik',122,null,null);

                                    SELECT Name, Phone, COALESCE("B.mail","P.mail") Email, CASE WHEN "B.mail" IS NOT NULL THEN 'Y' ELSE 'N' END as IsPrimary,CASE WHEN "B.mail" IS NOT NULL THEN 'B' ELSE 'P' END as EmailType
                                    FROM Table3 t
                                    where "B.mail" IS NOT NULL OR "P.mail" IS NOT NULL
                                    UNION
                                    SELECT Name, Phone, COALESCE("P.mail","B.mail") Email, CASE WHEN "P.mail" IS NOT NULL THEN 'N' ELSE 'Y' END as IsPrimary,CASE WHEN "P.mail" IS NOT NULL THEN 'P' ELSE 'B' END as EmailType
                                    FROM Table3 t
                                    where "B.mail" IS NOT NULL OR "P.mail" IS NOT NULL





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Try below query :



                                      create table     Table3
                                      (Name varchar2(20),
                                      phone number,
                                      "B.mail" varchar2(50),
                                      "P.mail" varchar2(50));

                                      insert into Table3 values ('Jack',12,'ab12@test.com','a21@test.com');
                                      insert into Table3 values ('Ram',12345,null,'Ram@test.com');
                                      insert into Table3 values ('Raja',543,'Raj@test.com',null);
                                      insert into Table3 values ('Rik',122,null,null);

                                      SELECT Name, Phone, COALESCE("B.mail","P.mail") Email, CASE WHEN "B.mail" IS NOT NULL THEN 'Y' ELSE 'N' END as IsPrimary,CASE WHEN "B.mail" IS NOT NULL THEN 'B' ELSE 'P' END as EmailType
                                      FROM Table3 t
                                      where "B.mail" IS NOT NULL OR "P.mail" IS NOT NULL
                                      UNION
                                      SELECT Name, Phone, COALESCE("P.mail","B.mail") Email, CASE WHEN "P.mail" IS NOT NULL THEN 'N' ELSE 'Y' END as IsPrimary,CASE WHEN "P.mail" IS NOT NULL THEN 'P' ELSE 'B' END as EmailType
                                      FROM Table3 t
                                      where "B.mail" IS NOT NULL OR "P.mail" IS NOT NULL





                                      share|improve this answer













                                      Try below query :



                                      create table     Table3
                                      (Name varchar2(20),
                                      phone number,
                                      "B.mail" varchar2(50),
                                      "P.mail" varchar2(50));

                                      insert into Table3 values ('Jack',12,'ab12@test.com','a21@test.com');
                                      insert into Table3 values ('Ram',12345,null,'Ram@test.com');
                                      insert into Table3 values ('Raja',543,'Raj@test.com',null);
                                      insert into Table3 values ('Rik',122,null,null);

                                      SELECT Name, Phone, COALESCE("B.mail","P.mail") Email, CASE WHEN "B.mail" IS NOT NULL THEN 'Y' ELSE 'N' END as IsPrimary,CASE WHEN "B.mail" IS NOT NULL THEN 'B' ELSE 'P' END as EmailType
                                      FROM Table3 t
                                      where "B.mail" IS NOT NULL OR "P.mail" IS NOT NULL
                                      UNION
                                      SELECT Name, Phone, COALESCE("P.mail","B.mail") Email, CASE WHEN "P.mail" IS NOT NULL THEN 'N' ELSE 'Y' END as IsPrimary,CASE WHEN "P.mail" IS NOT NULL THEN 'P' ELSE 'B' END as EmailType
                                      FROM Table3 t
                                      where "B.mail" IS NOT NULL OR "P.mail" IS NOT NULL






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 16 '18 at 18:41









                                      kanagarajkanagaraj

                                      38417




                                      38417






























                                          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%2f53341427%2fsql-to-fetch-row-into-column-based-on-con%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

                                          Florida Star v. B. J. F.

                                          Danny Elfman

                                          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues