break out each unique value of a column into multiple columns then display in single row












-1














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










share|improve this question




















  • 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














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










share|improve this question




















  • 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








-1







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










share|improve this question















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-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer





















  • 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











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%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









0














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.






share|improve this answer





















  • 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
















0














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.






share|improve this answer





















  • 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














0












0








0






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















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.





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.




draft saved


draft discarded














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





















































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

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly