SQL add new line if value is in second column also











up vote
0
down vote

favorite












So I am trying to pull the data from the table to give me each score the person has and if they have two scores I would like it to be on a new line with the second score. If the user has no scores I don't want anything returned. My query returns the first score if the user has one and if they don't it returns the second one. But if the user has two scores is where i'm not sure how to return that one on a new line.



table 1



+---------+--------+--------+
| name | score1 | score2 |
+---------+--------+--------+
| jim | null | 87 |
| doug | 21 | 45 |
| brandon | null | null |
| susy | 11 | null |
+---------+--------+--------+


The result my query gives is



+------+----+
| jim | 87 |
| doug | 21 |
| susy | 11 |
+------+----+


Wanted output



+------+----+
| jim | 87 |
| doug | 21 |
| doug | 45 |
| susy | 11 |
+------+----+


The query I wrote is



SELECT
name
,COALESCE(score1, score2)
FROM
table
WHERE
score1 IS NOT NULL
OR score2 IS NOT NULL
ORDER BY
name;









share|improve this question









New contributor




Brandon Brown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Use APPLY or UNPIVOT. e.g. SELECT T.Name, C.Score FROM myTable AS T CROSS APPLY (VALUES (T.Score1), (T.Score2)) AS C(Score) WHERE C.Score IS NOT NULL;
    – ZLK
    Nov 21 at 21:45












  • On an unrelated note, have a look at this post for some tips and tools for formatting your posts.
    – Eric Brandt
    Nov 21 at 21:56










  • @ZLK I didn't know you could cross apply to VALUES from the original table. Thanks for teaching me something neat today.
    – SteveB
    Nov 21 at 22:02















up vote
0
down vote

favorite












So I am trying to pull the data from the table to give me each score the person has and if they have two scores I would like it to be on a new line with the second score. If the user has no scores I don't want anything returned. My query returns the first score if the user has one and if they don't it returns the second one. But if the user has two scores is where i'm not sure how to return that one on a new line.



table 1



+---------+--------+--------+
| name | score1 | score2 |
+---------+--------+--------+
| jim | null | 87 |
| doug | 21 | 45 |
| brandon | null | null |
| susy | 11 | null |
+---------+--------+--------+


The result my query gives is



+------+----+
| jim | 87 |
| doug | 21 |
| susy | 11 |
+------+----+


Wanted output



+------+----+
| jim | 87 |
| doug | 21 |
| doug | 45 |
| susy | 11 |
+------+----+


The query I wrote is



SELECT
name
,COALESCE(score1, score2)
FROM
table
WHERE
score1 IS NOT NULL
OR score2 IS NOT NULL
ORDER BY
name;









share|improve this question









New contributor




Brandon Brown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Use APPLY or UNPIVOT. e.g. SELECT T.Name, C.Score FROM myTable AS T CROSS APPLY (VALUES (T.Score1), (T.Score2)) AS C(Score) WHERE C.Score IS NOT NULL;
    – ZLK
    Nov 21 at 21:45












  • On an unrelated note, have a look at this post for some tips and tools for formatting your posts.
    – Eric Brandt
    Nov 21 at 21:56










  • @ZLK I didn't know you could cross apply to VALUES from the original table. Thanks for teaching me something neat today.
    – SteveB
    Nov 21 at 22:02













up vote
0
down vote

favorite









up vote
0
down vote

favorite











So I am trying to pull the data from the table to give me each score the person has and if they have two scores I would like it to be on a new line with the second score. If the user has no scores I don't want anything returned. My query returns the first score if the user has one and if they don't it returns the second one. But if the user has two scores is where i'm not sure how to return that one on a new line.



table 1



+---------+--------+--------+
| name | score1 | score2 |
+---------+--------+--------+
| jim | null | 87 |
| doug | 21 | 45 |
| brandon | null | null |
| susy | 11 | null |
+---------+--------+--------+


The result my query gives is



+------+----+
| jim | 87 |
| doug | 21 |
| susy | 11 |
+------+----+


Wanted output



+------+----+
| jim | 87 |
| doug | 21 |
| doug | 45 |
| susy | 11 |
+------+----+


The query I wrote is



SELECT
name
,COALESCE(score1, score2)
FROM
table
WHERE
score1 IS NOT NULL
OR score2 IS NOT NULL
ORDER BY
name;









share|improve this question









New contributor




Brandon Brown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











So I am trying to pull the data from the table to give me each score the person has and if they have two scores I would like it to be on a new line with the second score. If the user has no scores I don't want anything returned. My query returns the first score if the user has one and if they don't it returns the second one. But if the user has two scores is where i'm not sure how to return that one on a new line.



table 1



+---------+--------+--------+
| name | score1 | score2 |
+---------+--------+--------+
| jim | null | 87 |
| doug | 21 | 45 |
| brandon | null | null |
| susy | 11 | null |
+---------+--------+--------+


The result my query gives is



+------+----+
| jim | 87 |
| doug | 21 |
| susy | 11 |
+------+----+


Wanted output



+------+----+
| jim | 87 |
| doug | 21 |
| doug | 45 |
| susy | 11 |
+------+----+


The query I wrote is



SELECT
name
,COALESCE(score1, score2)
FROM
table
WHERE
score1 IS NOT NULL
OR score2 IS NOT NULL
ORDER BY
name;






sql sql-server






share|improve this question









New contributor




Brandon Brown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Brandon Brown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 21 at 21:56









Eric Brandt

1,9141518




1,9141518






New contributor




Brandon Brown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 21 at 21:44









Brandon Brown

144




144




New contributor




Brandon Brown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Brandon Brown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Brandon Brown is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Use APPLY or UNPIVOT. e.g. SELECT T.Name, C.Score FROM myTable AS T CROSS APPLY (VALUES (T.Score1), (T.Score2)) AS C(Score) WHERE C.Score IS NOT NULL;
    – ZLK
    Nov 21 at 21:45












  • On an unrelated note, have a look at this post for some tips and tools for formatting your posts.
    – Eric Brandt
    Nov 21 at 21:56










  • @ZLK I didn't know you could cross apply to VALUES from the original table. Thanks for teaching me something neat today.
    – SteveB
    Nov 21 at 22:02


















  • Use APPLY or UNPIVOT. e.g. SELECT T.Name, C.Score FROM myTable AS T CROSS APPLY (VALUES (T.Score1), (T.Score2)) AS C(Score) WHERE C.Score IS NOT NULL;
    – ZLK
    Nov 21 at 21:45












  • On an unrelated note, have a look at this post for some tips and tools for formatting your posts.
    – Eric Brandt
    Nov 21 at 21:56










  • @ZLK I didn't know you could cross apply to VALUES from the original table. Thanks for teaching me something neat today.
    – SteveB
    Nov 21 at 22:02
















Use APPLY or UNPIVOT. e.g. SELECT T.Name, C.Score FROM myTable AS T CROSS APPLY (VALUES (T.Score1), (T.Score2)) AS C(Score) WHERE C.Score IS NOT NULL;
– ZLK
Nov 21 at 21:45






Use APPLY or UNPIVOT. e.g. SELECT T.Name, C.Score FROM myTable AS T CROSS APPLY (VALUES (T.Score1), (T.Score2)) AS C(Score) WHERE C.Score IS NOT NULL;
– ZLK
Nov 21 at 21:45














On an unrelated note, have a look at this post for some tips and tools for formatting your posts.
– Eric Brandt
Nov 21 at 21:56




On an unrelated note, have a look at this post for some tips and tools for formatting your posts.
– Eric Brandt
Nov 21 at 21:56












@ZLK I didn't know you could cross apply to VALUES from the original table. Thanks for teaching me something neat today.
– SteveB
Nov 21 at 22:02




@ZLK I didn't know you could cross apply to VALUES from the original table. Thanks for teaching me something neat today.
– SteveB
Nov 21 at 22:02












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Treat this as two separate queries and combine the results together with UNION ALL. You'll want UNION ALL in this case and not just UNION so you get two rows returned in the case where the person has the same score in both columns.



SELECT name, Score1 as score
FROM table1
WHERE Score1 IS NOT NULL
UNION ALL
SELECT name, Score2 as score
FROM table1
WHERE Score2 IS NOT NULL
ORDER BY name, score;





share|improve this answer




























    up vote
    1
    down vote













    I would recommend cross apply:



    SELECT t.name, v.score
    FROM table t CROSS APPLY
    (VALUES (score1), (score2)) v(score)
    WHERE v.score IS NOT NULL
    ORDER BY name;


    This is usually the most efficient way to unpivot data in SQL Server.






    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',
      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
      });


      }
      });






      Brandon Brown is a new contributor. Be nice, and check out our Code of Conduct.










       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420892%2fsql-add-new-line-if-value-is-in-second-column-also%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      Treat this as two separate queries and combine the results together with UNION ALL. You'll want UNION ALL in this case and not just UNION so you get two rows returned in the case where the person has the same score in both columns.



      SELECT name, Score1 as score
      FROM table1
      WHERE Score1 IS NOT NULL
      UNION ALL
      SELECT name, Score2 as score
      FROM table1
      WHERE Score2 IS NOT NULL
      ORDER BY name, score;





      share|improve this answer

























        up vote
        1
        down vote













        Treat this as two separate queries and combine the results together with UNION ALL. You'll want UNION ALL in this case and not just UNION so you get two rows returned in the case where the person has the same score in both columns.



        SELECT name, Score1 as score
        FROM table1
        WHERE Score1 IS NOT NULL
        UNION ALL
        SELECT name, Score2 as score
        FROM table1
        WHERE Score2 IS NOT NULL
        ORDER BY name, score;





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          Treat this as two separate queries and combine the results together with UNION ALL. You'll want UNION ALL in this case and not just UNION so you get two rows returned in the case where the person has the same score in both columns.



          SELECT name, Score1 as score
          FROM table1
          WHERE Score1 IS NOT NULL
          UNION ALL
          SELECT name, Score2 as score
          FROM table1
          WHERE Score2 IS NOT NULL
          ORDER BY name, score;





          share|improve this answer












          Treat this as two separate queries and combine the results together with UNION ALL. You'll want UNION ALL in this case and not just UNION so you get two rows returned in the case where the person has the same score in both columns.



          SELECT name, Score1 as score
          FROM table1
          WHERE Score1 IS NOT NULL
          UNION ALL
          SELECT name, Score2 as score
          FROM table1
          WHERE Score2 IS NOT NULL
          ORDER BY name, score;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 21:57









          Joe Stefanelli

          109k13189203




          109k13189203
























              up vote
              1
              down vote













              I would recommend cross apply:



              SELECT t.name, v.score
              FROM table t CROSS APPLY
              (VALUES (score1), (score2)) v(score)
              WHERE v.score IS NOT NULL
              ORDER BY name;


              This is usually the most efficient way to unpivot data in SQL Server.






              share|improve this answer

























                up vote
                1
                down vote













                I would recommend cross apply:



                SELECT t.name, v.score
                FROM table t CROSS APPLY
                (VALUES (score1), (score2)) v(score)
                WHERE v.score IS NOT NULL
                ORDER BY name;


                This is usually the most efficient way to unpivot data in SQL Server.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  I would recommend cross apply:



                  SELECT t.name, v.score
                  FROM table t CROSS APPLY
                  (VALUES (score1), (score2)) v(score)
                  WHERE v.score IS NOT NULL
                  ORDER BY name;


                  This is usually the most efficient way to unpivot data in SQL Server.






                  share|improve this answer












                  I would recommend cross apply:



                  SELECT t.name, v.score
                  FROM table t CROSS APPLY
                  (VALUES (score1), (score2)) v(score)
                  WHERE v.score IS NOT NULL
                  ORDER BY name;


                  This is usually the most efficient way to unpivot data in SQL Server.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 at 22:36









                  Gordon Linoff

                  747k33285390




                  747k33285390






















                      Brandon Brown is a new contributor. Be nice, and check out our Code of Conduct.










                       

                      draft saved


                      draft discarded


















                      Brandon Brown is a new contributor. Be nice, and check out our Code of Conduct.













                      Brandon Brown is a new contributor. Be nice, and check out our Code of Conduct.












                      Brandon Brown is a new contributor. Be nice, and check out our Code of Conduct.















                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420892%2fsql-add-new-line-if-value-is-in-second-column-also%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

                      What visual should I use to simply compare current year value vs last year in Power BI desktop

                      Alexandru Averescu

                      Trompette piccolo