MySQL - Count and CASE WHEN










0















My google and stockoverflow searches failed me so need your help. I have a below table



+------+------+------+------------+
| id | Loc | MEL | COUNT(MEL) |
+------+------+------+------------+
| 1 | AAA | A | 1 |
| 2 | BBB | B | 1 |
| 3 | CCC | C | 1 |
| 4 | AAA | D | 1 |
| 5 | AAA | A | 1 |
| 6 | BBB | B | 1 |
| 7 | BBB | C | 1 |
| 8 | AAA | D | 1 |
+------+------+------+------------+


I want to transform it to the following table:



+------+------+------+------+
| MEL | AAA | BBB | CCC |
+------+------+------+------+
| A | 2 | | |
| B | | 2 | |
| C | | 1 | 1 |
| D | 2 | | 1 |
+------+------+------+------+


All conbinations of COUNT and CASE WHEN did not work?



Code to get the below tables:



CREATE TABLE Orders
(
id INT,
Loc char(255),
MEL char (10))

insert into Orders values
(1, "AAA", "A"),
(2, "BBB", "B"),
(3, "CCC", "C"),
(4, "AAA", "D"),
(5, "AAA", "A"),
(6, "BBB", "B"),
(7, "BBB", "C"),
(8, "AAA", "D");









share|improve this question



















  • 1





    try googling or searching PIVOT

    – Randy
    Nov 15 '18 at 14:36






  • 1





    Alternatively, consider handling issues of data display in application code.

    – Strawberry
    Nov 15 '18 at 14:37











  • I have this code but as I had said before it did not work. SELECT MEL, COUNT(Loc = 'AAA')AS 'AAA', COUNT(Loc = 'BBB')AS 'BBB', COUNT(Loc = 'CCC')AS 'CCC', COUNT(Loc = 'DDD')AS 'DDD' FROM ORDERS GROUP BY MEL;

    – Kalenji
    Nov 15 '18 at 14:41











  • You probably need conditional aggregation and dynamic sql.

    – P.Salmon
    Nov 15 '18 at 14:41















0















My google and stockoverflow searches failed me so need your help. I have a below table



+------+------+------+------------+
| id | Loc | MEL | COUNT(MEL) |
+------+------+------+------------+
| 1 | AAA | A | 1 |
| 2 | BBB | B | 1 |
| 3 | CCC | C | 1 |
| 4 | AAA | D | 1 |
| 5 | AAA | A | 1 |
| 6 | BBB | B | 1 |
| 7 | BBB | C | 1 |
| 8 | AAA | D | 1 |
+------+------+------+------------+


I want to transform it to the following table:



+------+------+------+------+
| MEL | AAA | BBB | CCC |
+------+------+------+------+
| A | 2 | | |
| B | | 2 | |
| C | | 1 | 1 |
| D | 2 | | 1 |
+------+------+------+------+


All conbinations of COUNT and CASE WHEN did not work?



Code to get the below tables:



CREATE TABLE Orders
(
id INT,
Loc char(255),
MEL char (10))

insert into Orders values
(1, "AAA", "A"),
(2, "BBB", "B"),
(3, "CCC", "C"),
(4, "AAA", "D"),
(5, "AAA", "A"),
(6, "BBB", "B"),
(7, "BBB", "C"),
(8, "AAA", "D");









share|improve this question



















  • 1





    try googling or searching PIVOT

    – Randy
    Nov 15 '18 at 14:36






  • 1





    Alternatively, consider handling issues of data display in application code.

    – Strawberry
    Nov 15 '18 at 14:37











  • I have this code but as I had said before it did not work. SELECT MEL, COUNT(Loc = 'AAA')AS 'AAA', COUNT(Loc = 'BBB')AS 'BBB', COUNT(Loc = 'CCC')AS 'CCC', COUNT(Loc = 'DDD')AS 'DDD' FROM ORDERS GROUP BY MEL;

    – Kalenji
    Nov 15 '18 at 14:41











  • You probably need conditional aggregation and dynamic sql.

    – P.Salmon
    Nov 15 '18 at 14:41













0












0








0








My google and stockoverflow searches failed me so need your help. I have a below table



+------+------+------+------------+
| id | Loc | MEL | COUNT(MEL) |
+------+------+------+------------+
| 1 | AAA | A | 1 |
| 2 | BBB | B | 1 |
| 3 | CCC | C | 1 |
| 4 | AAA | D | 1 |
| 5 | AAA | A | 1 |
| 6 | BBB | B | 1 |
| 7 | BBB | C | 1 |
| 8 | AAA | D | 1 |
+------+------+------+------------+


I want to transform it to the following table:



+------+------+------+------+
| MEL | AAA | BBB | CCC |
+------+------+------+------+
| A | 2 | | |
| B | | 2 | |
| C | | 1 | 1 |
| D | 2 | | 1 |
+------+------+------+------+


All conbinations of COUNT and CASE WHEN did not work?



Code to get the below tables:



CREATE TABLE Orders
(
id INT,
Loc char(255),
MEL char (10))

insert into Orders values
(1, "AAA", "A"),
(2, "BBB", "B"),
(3, "CCC", "C"),
(4, "AAA", "D"),
(5, "AAA", "A"),
(6, "BBB", "B"),
(7, "BBB", "C"),
(8, "AAA", "D");









share|improve this question
















My google and stockoverflow searches failed me so need your help. I have a below table



+------+------+------+------------+
| id | Loc | MEL | COUNT(MEL) |
+------+------+------+------------+
| 1 | AAA | A | 1 |
| 2 | BBB | B | 1 |
| 3 | CCC | C | 1 |
| 4 | AAA | D | 1 |
| 5 | AAA | A | 1 |
| 6 | BBB | B | 1 |
| 7 | BBB | C | 1 |
| 8 | AAA | D | 1 |
+------+------+------+------------+


I want to transform it to the following table:



+------+------+------+------+
| MEL | AAA | BBB | CCC |
+------+------+------+------+
| A | 2 | | |
| B | | 2 | |
| C | | 1 | 1 |
| D | 2 | | 1 |
+------+------+------+------+


All conbinations of COUNT and CASE WHEN did not work?



Code to get the below tables:



CREATE TABLE Orders
(
id INT,
Loc char(255),
MEL char (10))

insert into Orders values
(1, "AAA", "A"),
(2, "BBB", "B"),
(3, "CCC", "C"),
(4, "AAA", "D"),
(5, "AAA", "A"),
(6, "BBB", "B"),
(7, "BBB", "C"),
(8, "AAA", "D");






mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:31









Strawberry

26.7k83250




26.7k83250










asked Nov 15 '18 at 14:25









KalenjiKalenji

269316




269316







  • 1





    try googling or searching PIVOT

    – Randy
    Nov 15 '18 at 14:36






  • 1





    Alternatively, consider handling issues of data display in application code.

    – Strawberry
    Nov 15 '18 at 14:37











  • I have this code but as I had said before it did not work. SELECT MEL, COUNT(Loc = 'AAA')AS 'AAA', COUNT(Loc = 'BBB')AS 'BBB', COUNT(Loc = 'CCC')AS 'CCC', COUNT(Loc = 'DDD')AS 'DDD' FROM ORDERS GROUP BY MEL;

    – Kalenji
    Nov 15 '18 at 14:41











  • You probably need conditional aggregation and dynamic sql.

    – P.Salmon
    Nov 15 '18 at 14:41












  • 1





    try googling or searching PIVOT

    – Randy
    Nov 15 '18 at 14:36






  • 1





    Alternatively, consider handling issues of data display in application code.

    – Strawberry
    Nov 15 '18 at 14:37











  • I have this code but as I had said before it did not work. SELECT MEL, COUNT(Loc = 'AAA')AS 'AAA', COUNT(Loc = 'BBB')AS 'BBB', COUNT(Loc = 'CCC')AS 'CCC', COUNT(Loc = 'DDD')AS 'DDD' FROM ORDERS GROUP BY MEL;

    – Kalenji
    Nov 15 '18 at 14:41











  • You probably need conditional aggregation and dynamic sql.

    – P.Salmon
    Nov 15 '18 at 14:41







1




1





try googling or searching PIVOT

– Randy
Nov 15 '18 at 14:36





try googling or searching PIVOT

– Randy
Nov 15 '18 at 14:36




1




1





Alternatively, consider handling issues of data display in application code.

– Strawberry
Nov 15 '18 at 14:37





Alternatively, consider handling issues of data display in application code.

– Strawberry
Nov 15 '18 at 14:37













I have this code but as I had said before it did not work. SELECT MEL, COUNT(Loc = 'AAA')AS 'AAA', COUNT(Loc = 'BBB')AS 'BBB', COUNT(Loc = 'CCC')AS 'CCC', COUNT(Loc = 'DDD')AS 'DDD' FROM ORDERS GROUP BY MEL;

– Kalenji
Nov 15 '18 at 14:41





I have this code but as I had said before it did not work. SELECT MEL, COUNT(Loc = 'AAA')AS 'AAA', COUNT(Loc = 'BBB')AS 'BBB', COUNT(Loc = 'CCC')AS 'CCC', COUNT(Loc = 'DDD')AS 'DDD' FROM ORDERS GROUP BY MEL;

– Kalenji
Nov 15 '18 at 14:41













You probably need conditional aggregation and dynamic sql.

– P.Salmon
Nov 15 '18 at 14:41





You probably need conditional aggregation and dynamic sql.

– P.Salmon
Nov 15 '18 at 14:41












1 Answer
1






active

oldest

votes


















3














select mel, 
sum(Loc="AAA") AS AAA,
sum(Loc="BBB") AS BBB /* and so on */
from Orders group by mel;


You need to use sum instead of count, because the boolean expression returns 0 or 1. count doesn't care about that, it just counts, whether it's 0 or 1 doesn't matter.






share|improve this answer























  • fiddle: sqlfiddle.com/#!9/e2bb6c/20

    – vivoconunxino
    Nov 15 '18 at 14:52










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%2f53321587%2fmysql-count-and-case-when%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









3














select mel, 
sum(Loc="AAA") AS AAA,
sum(Loc="BBB") AS BBB /* and so on */
from Orders group by mel;


You need to use sum instead of count, because the boolean expression returns 0 or 1. count doesn't care about that, it just counts, whether it's 0 or 1 doesn't matter.






share|improve this answer























  • fiddle: sqlfiddle.com/#!9/e2bb6c/20

    – vivoconunxino
    Nov 15 '18 at 14:52















3














select mel, 
sum(Loc="AAA") AS AAA,
sum(Loc="BBB") AS BBB /* and so on */
from Orders group by mel;


You need to use sum instead of count, because the boolean expression returns 0 or 1. count doesn't care about that, it just counts, whether it's 0 or 1 doesn't matter.






share|improve this answer























  • fiddle: sqlfiddle.com/#!9/e2bb6c/20

    – vivoconunxino
    Nov 15 '18 at 14:52













3












3








3







select mel, 
sum(Loc="AAA") AS AAA,
sum(Loc="BBB") AS BBB /* and so on */
from Orders group by mel;


You need to use sum instead of count, because the boolean expression returns 0 or 1. count doesn't care about that, it just counts, whether it's 0 or 1 doesn't matter.






share|improve this answer













select mel, 
sum(Loc="AAA") AS AAA,
sum(Loc="BBB") AS BBB /* and so on */
from Orders group by mel;


You need to use sum instead of count, because the boolean expression returns 0 or 1. count doesn't care about that, it just counts, whether it's 0 or 1 doesn't matter.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 14:41









fancyPantsfancyPants

39.8k167085




39.8k167085












  • fiddle: sqlfiddle.com/#!9/e2bb6c/20

    – vivoconunxino
    Nov 15 '18 at 14:52

















  • fiddle: sqlfiddle.com/#!9/e2bb6c/20

    – vivoconunxino
    Nov 15 '18 at 14:52
















fiddle: sqlfiddle.com/#!9/e2bb6c/20

– vivoconunxino
Nov 15 '18 at 14:52





fiddle: sqlfiddle.com/#!9/e2bb6c/20

– vivoconunxino
Nov 15 '18 at 14:52



















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%2f53321587%2fmysql-count-and-case-when%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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3