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;
sql sql-server
New contributor
add a comment |
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;
sql sql-server
New contributor
UseAPPLY
orUNPIVOT
. 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
add a comment |
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;
sql sql-server
New contributor
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
sql sql-server
New contributor
New contributor
edited Nov 21 at 21:56
Eric Brandt
1,9141518
1,9141518
New contributor
asked Nov 21 at 21:44
Brandon Brown
144
144
New contributor
New contributor
UseAPPLY
orUNPIVOT
. 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
add a comment |
UseAPPLY
orUNPIVOT
. 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
add a comment |
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;
add a comment |
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.
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 21 at 21:57
Joe Stefanelli
109k13189203
109k13189203
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 at 22:36
Gordon Linoff
747k33285390
747k33285390
add a comment |
add a comment |
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.
Brandon Brown is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Use
APPLY
orUNPIVOT
. 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