break out each unique value of a column into multiple columns then display in single row
In my actual data I have the following query
SELECT Sales Person,
(CASE WHEN [ProductType] = 'car' THEN [GOAL_VOLUME]
END) AS 'car Goal'
, (CASE WHEN [ProductType] = 'boat' THEN [GOAL_VOLUME]
END) AS 'boat Goal'
, (CASE WHEN [ProductType] IN ('bike', 'scooter') THEN [GOAL_VOLUME]
END) AS 'bike/scooter Goal'
FROM PRODUCT_GOALS
My current results are each Sales Person's name broken out 3 times into 3 rows with each row having only one non-NULL value for the Goal_Volume... I need everything on one row... Might need multiple subqueries to join against for each product?
See image below.
enter image description here
Thanks for the help
sql
add a comment |
In my actual data I have the following query
SELECT Sales Person,
(CASE WHEN [ProductType] = 'car' THEN [GOAL_VOLUME]
END) AS 'car Goal'
, (CASE WHEN [ProductType] = 'boat' THEN [GOAL_VOLUME]
END) AS 'boat Goal'
, (CASE WHEN [ProductType] IN ('bike', 'scooter') THEN [GOAL_VOLUME]
END) AS 'bike/scooter Goal'
FROM PRODUCT_GOALS
My current results are each Sales Person's name broken out 3 times into 3 rows with each row having only one non-NULL value for the Goal_Volume... I need everything on one row... Might need multiple subqueries to join against for each product?
See image below.
enter image description here
Thanks for the help
sql
1
Take a look at the PIVOT documentation - sorry I don't have time to do it for you.
– Dale Burrell
Nov 13 '18 at 1:33
2
Are these two different questions?
– Josh Part
Nov 13 '18 at 1:35
1
how can I roll this up so that Clothes and Entertainment are in 2 columns while each of the USR_NAME values are their own columns (so would be 8 columns in this case).This is a bit confusing, can you show us how it should looks like ?
– Squirrel
Nov 13 '18 at 1:46
My apologies, updated to add image link explaining my issue and desired output. Thanks
– pdangelo4
Nov 13 '18 at 16:52
Read this. stackoverflow.com/help/how-to-ask
– Eric
Nov 13 '18 at 17:11
add a comment |
In my actual data I have the following query
SELECT Sales Person,
(CASE WHEN [ProductType] = 'car' THEN [GOAL_VOLUME]
END) AS 'car Goal'
, (CASE WHEN [ProductType] = 'boat' THEN [GOAL_VOLUME]
END) AS 'boat Goal'
, (CASE WHEN [ProductType] IN ('bike', 'scooter') THEN [GOAL_VOLUME]
END) AS 'bike/scooter Goal'
FROM PRODUCT_GOALS
My current results are each Sales Person's name broken out 3 times into 3 rows with each row having only one non-NULL value for the Goal_Volume... I need everything on one row... Might need multiple subqueries to join against for each product?
See image below.
enter image description here
Thanks for the help
sql
In my actual data I have the following query
SELECT Sales Person,
(CASE WHEN [ProductType] = 'car' THEN [GOAL_VOLUME]
END) AS 'car Goal'
, (CASE WHEN [ProductType] = 'boat' THEN [GOAL_VOLUME]
END) AS 'boat Goal'
, (CASE WHEN [ProductType] IN ('bike', 'scooter') THEN [GOAL_VOLUME]
END) AS 'bike/scooter Goal'
FROM PRODUCT_GOALS
My current results are each Sales Person's name broken out 3 times into 3 rows with each row having only one non-NULL value for the Goal_Volume... I need everything on one row... Might need multiple subqueries to join against for each product?
See image below.
enter image description here
Thanks for the help
sql
sql
edited Nov 13 '18 at 16:51
asked Nov 13 '18 at 1:31
pdangelo4
93
93
1
Take a look at the PIVOT documentation - sorry I don't have time to do it for you.
– Dale Burrell
Nov 13 '18 at 1:33
2
Are these two different questions?
– Josh Part
Nov 13 '18 at 1:35
1
how can I roll this up so that Clothes and Entertainment are in 2 columns while each of the USR_NAME values are their own columns (so would be 8 columns in this case).This is a bit confusing, can you show us how it should looks like ?
– Squirrel
Nov 13 '18 at 1:46
My apologies, updated to add image link explaining my issue and desired output. Thanks
– pdangelo4
Nov 13 '18 at 16:52
Read this. stackoverflow.com/help/how-to-ask
– Eric
Nov 13 '18 at 17:11
add a comment |
1
Take a look at the PIVOT documentation - sorry I don't have time to do it for you.
– Dale Burrell
Nov 13 '18 at 1:33
2
Are these two different questions?
– Josh Part
Nov 13 '18 at 1:35
1
how can I roll this up so that Clothes and Entertainment are in 2 columns while each of the USR_NAME values are their own columns (so would be 8 columns in this case).This is a bit confusing, can you show us how it should looks like ?
– Squirrel
Nov 13 '18 at 1:46
My apologies, updated to add image link explaining my issue and desired output. Thanks
– pdangelo4
Nov 13 '18 at 16:52
Read this. stackoverflow.com/help/how-to-ask
– Eric
Nov 13 '18 at 17:11
1
1
Take a look at the PIVOT documentation - sorry I don't have time to do it for you.
– Dale Burrell
Nov 13 '18 at 1:33
Take a look at the PIVOT documentation - sorry I don't have time to do it for you.
– Dale Burrell
Nov 13 '18 at 1:33
2
2
Are these two different questions?
– Josh Part
Nov 13 '18 at 1:35
Are these two different questions?
– Josh Part
Nov 13 '18 at 1:35
1
1
how can I roll this up so that Clothes and Entertainment are in 2 columns while each of the USR_NAME values are their own columns (so would be 8 columns in this case). This is a bit confusing, can you show us how it should looks like ?– Squirrel
Nov 13 '18 at 1:46
how can I roll this up so that Clothes and Entertainment are in 2 columns while each of the USR_NAME values are their own columns (so would be 8 columns in this case). This is a bit confusing, can you show us how it should looks like ?– Squirrel
Nov 13 '18 at 1:46
My apologies, updated to add image link explaining my issue and desired output. Thanks
– pdangelo4
Nov 13 '18 at 16:52
My apologies, updated to add image link explaining my issue and desired output. Thanks
– pdangelo4
Nov 13 '18 at 16:52
Read this. stackoverflow.com/help/how-to-ask
– Eric
Nov 13 '18 at 17:11
Read this. stackoverflow.com/help/how-to-ask
– Eric
Nov 13 '18 at 17:11
add a comment |
1 Answer
1
active
oldest
votes
You can use conditional aggregation:
SELECT [Sales Person]
, MAX(CASE WHEN [ProductType] = 'car' THEN [GOAL_VOLUME]
END) AS 'car Goal'
, MAX(CASE WHEN [ProductType] = 'boat' THEN [GOAL_VOLUME]
END) AS 'boat Goal'
, MAX(CASE WHEN [ProductType] IN ('bike', 'scooter') THEN [GOAL_VOLUME]
END) AS 'bike/scooter Goal'
FROM PRODUCT_GOALS
GROUP BY [Sales Person]
Almost the same query as yours, just added MAX and GROUP BY.
Thank you! Since the GOAL_VOLUME is a static value manually inputted, MAX is a great and simple solution!
– pdangelo4
Nov 13 '18 at 17:18
Glad to help :-)
– Zohar Peled
Nov 13 '18 at 19:48
add a comment |
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
});
}
});
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%2f53272481%2fbreak-out-each-unique-value-of-a-column-into-multiple-columns-then-display-in-si%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
You can use conditional aggregation:
SELECT [Sales Person]
, MAX(CASE WHEN [ProductType] = 'car' THEN [GOAL_VOLUME]
END) AS 'car Goal'
, MAX(CASE WHEN [ProductType] = 'boat' THEN [GOAL_VOLUME]
END) AS 'boat Goal'
, MAX(CASE WHEN [ProductType] IN ('bike', 'scooter') THEN [GOAL_VOLUME]
END) AS 'bike/scooter Goal'
FROM PRODUCT_GOALS
GROUP BY [Sales Person]
Almost the same query as yours, just added MAX and GROUP BY.
Thank you! Since the GOAL_VOLUME is a static value manually inputted, MAX is a great and simple solution!
– pdangelo4
Nov 13 '18 at 17:18
Glad to help :-)
– Zohar Peled
Nov 13 '18 at 19:48
add a comment |
You can use conditional aggregation:
SELECT [Sales Person]
, MAX(CASE WHEN [ProductType] = 'car' THEN [GOAL_VOLUME]
END) AS 'car Goal'
, MAX(CASE WHEN [ProductType] = 'boat' THEN [GOAL_VOLUME]
END) AS 'boat Goal'
, MAX(CASE WHEN [ProductType] IN ('bike', 'scooter') THEN [GOAL_VOLUME]
END) AS 'bike/scooter Goal'
FROM PRODUCT_GOALS
GROUP BY [Sales Person]
Almost the same query as yours, just added MAX and GROUP BY.
Thank you! Since the GOAL_VOLUME is a static value manually inputted, MAX is a great and simple solution!
– pdangelo4
Nov 13 '18 at 17:18
Glad to help :-)
– Zohar Peled
Nov 13 '18 at 19:48
add a comment |
You can use conditional aggregation:
SELECT [Sales Person]
, MAX(CASE WHEN [ProductType] = 'car' THEN [GOAL_VOLUME]
END) AS 'car Goal'
, MAX(CASE WHEN [ProductType] = 'boat' THEN [GOAL_VOLUME]
END) AS 'boat Goal'
, MAX(CASE WHEN [ProductType] IN ('bike', 'scooter') THEN [GOAL_VOLUME]
END) AS 'bike/scooter Goal'
FROM PRODUCT_GOALS
GROUP BY [Sales Person]
Almost the same query as yours, just added MAX and GROUP BY.
You can use conditional aggregation:
SELECT [Sales Person]
, MAX(CASE WHEN [ProductType] = 'car' THEN [GOAL_VOLUME]
END) AS 'car Goal'
, MAX(CASE WHEN [ProductType] = 'boat' THEN [GOAL_VOLUME]
END) AS 'boat Goal'
, MAX(CASE WHEN [ProductType] IN ('bike', 'scooter') THEN [GOAL_VOLUME]
END) AS 'bike/scooter Goal'
FROM PRODUCT_GOALS
GROUP BY [Sales Person]
Almost the same query as yours, just added MAX and GROUP BY.
answered Nov 13 '18 at 17:08
Zohar Peled
52.6k73273
52.6k73273
Thank you! Since the GOAL_VOLUME is a static value manually inputted, MAX is a great and simple solution!
– pdangelo4
Nov 13 '18 at 17:18
Glad to help :-)
– Zohar Peled
Nov 13 '18 at 19:48
add a comment |
Thank you! Since the GOAL_VOLUME is a static value manually inputted, MAX is a great and simple solution!
– pdangelo4
Nov 13 '18 at 17:18
Glad to help :-)
– Zohar Peled
Nov 13 '18 at 19:48
Thank you! Since the GOAL_VOLUME is a static value manually inputted, MAX is a great and simple solution!
– pdangelo4
Nov 13 '18 at 17:18
Thank you! Since the GOAL_VOLUME is a static value manually inputted, MAX is a great and simple solution!
– pdangelo4
Nov 13 '18 at 17:18
Glad to help :-)
– Zohar Peled
Nov 13 '18 at 19:48
Glad to help :-)
– Zohar Peled
Nov 13 '18 at 19:48
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2f53272481%2fbreak-out-each-unique-value-of-a-column-into-multiple-columns-then-display-in-si%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
1
Take a look at the PIVOT documentation - sorry I don't have time to do it for you.
– Dale Burrell
Nov 13 '18 at 1:33
2
Are these two different questions?
– Josh Part
Nov 13 '18 at 1:35
1
how can I roll this up so that Clothes and Entertainment are in 2 columns while each of the USR_NAME values are their own columns (so would be 8 columns in this case).This is a bit confusing, can you show us how it should looks like ?– Squirrel
Nov 13 '18 at 1:46
My apologies, updated to add image link explaining my issue and desired output. Thanks
– pdangelo4
Nov 13 '18 at 16:52
Read this. stackoverflow.com/help/how-to-ask
– Eric
Nov 13 '18 at 17:11