Flatten a response to an SQL query











up vote
2
down vote

favorite
1












I am trying to get a flat table for a question and answer table with mysql. This are my tables.



PollQuestion



PollID | Question| A | B | C | D | E | TimeStamp


PollReponse



PollReponseID | PollID | UserID | Answer


In the PollAnswer table I get Five answers as VARCHAR A, B, C, D, E.



I have written a query to group the answers by A, B, C, D, E.



    select q.Question
, q.PollID
, r.Answer
, count(r.Answer)
from pollQuestions q
, pollResponse r
where q.PollID = r.PollID
group
by r.Answer
, q.Question
, q.PollID
order
by r.PollID;


Which gives me a response as follows.



Question | PollID | Answer | count
alpha | 1 | A | 2
alpha | 1 | B | 3
alpha | 1 | C | 4
alpha | 1 | D | 0
alpha | 1 | E | 0
betas | 2 | A | 3
betas | 2 | B | 4
betas | 2 | C | 4
betas | 2 | D | 6
betas | 2 | E | 0


I want to flatten the answer like this.



Question | PollID | countA | countB | countC | countD | countE
alpha | 1 | 2 | 2 | 4 | 0 | 0
betas | 2 | 3 | 4 | 4 | 6 | 0


Is there anyway I can achieve this without changing the table structure?



Any pointer would be appreciated.










share|improve this question
























  • Consider handling issues of data display in application code
    – Strawberry
    Nov 11 at 9:25















up vote
2
down vote

favorite
1












I am trying to get a flat table for a question and answer table with mysql. This are my tables.



PollQuestion



PollID | Question| A | B | C | D | E | TimeStamp


PollReponse



PollReponseID | PollID | UserID | Answer


In the PollAnswer table I get Five answers as VARCHAR A, B, C, D, E.



I have written a query to group the answers by A, B, C, D, E.



    select q.Question
, q.PollID
, r.Answer
, count(r.Answer)
from pollQuestions q
, pollResponse r
where q.PollID = r.PollID
group
by r.Answer
, q.Question
, q.PollID
order
by r.PollID;


Which gives me a response as follows.



Question | PollID | Answer | count
alpha | 1 | A | 2
alpha | 1 | B | 3
alpha | 1 | C | 4
alpha | 1 | D | 0
alpha | 1 | E | 0
betas | 2 | A | 3
betas | 2 | B | 4
betas | 2 | C | 4
betas | 2 | D | 6
betas | 2 | E | 0


I want to flatten the answer like this.



Question | PollID | countA | countB | countC | countD | countE
alpha | 1 | 2 | 2 | 4 | 0 | 0
betas | 2 | 3 | 4 | 4 | 6 | 0


Is there anyway I can achieve this without changing the table structure?



Any pointer would be appreciated.










share|improve this question
























  • Consider handling issues of data display in application code
    – Strawberry
    Nov 11 at 9:25













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I am trying to get a flat table for a question and answer table with mysql. This are my tables.



PollQuestion



PollID | Question| A | B | C | D | E | TimeStamp


PollReponse



PollReponseID | PollID | UserID | Answer


In the PollAnswer table I get Five answers as VARCHAR A, B, C, D, E.



I have written a query to group the answers by A, B, C, D, E.



    select q.Question
, q.PollID
, r.Answer
, count(r.Answer)
from pollQuestions q
, pollResponse r
where q.PollID = r.PollID
group
by r.Answer
, q.Question
, q.PollID
order
by r.PollID;


Which gives me a response as follows.



Question | PollID | Answer | count
alpha | 1 | A | 2
alpha | 1 | B | 3
alpha | 1 | C | 4
alpha | 1 | D | 0
alpha | 1 | E | 0
betas | 2 | A | 3
betas | 2 | B | 4
betas | 2 | C | 4
betas | 2 | D | 6
betas | 2 | E | 0


I want to flatten the answer like this.



Question | PollID | countA | countB | countC | countD | countE
alpha | 1 | 2 | 2 | 4 | 0 | 0
betas | 2 | 3 | 4 | 4 | 6 | 0


Is there anyway I can achieve this without changing the table structure?



Any pointer would be appreciated.










share|improve this question















I am trying to get a flat table for a question and answer table with mysql. This are my tables.



PollQuestion



PollID | Question| A | B | C | D | E | TimeStamp


PollReponse



PollReponseID | PollID | UserID | Answer


In the PollAnswer table I get Five answers as VARCHAR A, B, C, D, E.



I have written a query to group the answers by A, B, C, D, E.



    select q.Question
, q.PollID
, r.Answer
, count(r.Answer)
from pollQuestions q
, pollResponse r
where q.PollID = r.PollID
group
by r.Answer
, q.Question
, q.PollID
order
by r.PollID;


Which gives me a response as follows.



Question | PollID | Answer | count
alpha | 1 | A | 2
alpha | 1 | B | 3
alpha | 1 | C | 4
alpha | 1 | D | 0
alpha | 1 | E | 0
betas | 2 | A | 3
betas | 2 | B | 4
betas | 2 | C | 4
betas | 2 | D | 6
betas | 2 | E | 0


I want to flatten the answer like this.



Question | PollID | countA | countB | countC | countD | countE
alpha | 1 | 2 | 2 | 4 | 0 | 0
betas | 2 | 3 | 4 | 4 | 6 | 0


Is there anyway I can achieve this without changing the table structure?



Any pointer would be appreciated.







mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 9:24









Strawberry

25.7k83149




25.7k83149










asked Nov 11 at 5:25









Fawzan

2,77432353




2,77432353












  • Consider handling issues of data display in application code
    – Strawberry
    Nov 11 at 9:25


















  • Consider handling issues of data display in application code
    – Strawberry
    Nov 11 at 9:25
















Consider handling issues of data display in application code
– Strawberry
Nov 11 at 9:25




Consider handling issues of data display in application code
– Strawberry
Nov 11 at 9:25












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You can try to use condition aggregate function.



select 
pollQuestions.Question,
pollQuestions.PollID,
count(CASE WHEN pollResponse.Answer ='A' THEN 1 END) countA,
count(CASE WHEN pollResponse.Answer ='B' THEN 1 END) countB,
count(CASE WHEN pollResponse.Answer ='C' THEN 1 END) countC,
count(CASE WHEN pollResponse.Answer ='D' THEN 1 END) countD,
count(CASE WHEN pollResponse.Answer ='E' THEN 1 END) countE
from pollQuestions
JOIN pollResponse on pollQuestions.PollID = pollResponse.PollID
group by
pollQuestions.Question,
pollQuestions.PollID
order by
pollResponse.PollID;


NOTE



I would use JOIN instead of comma , because JOIN syntax is clearer than , about connect two tabls.






share|improve this answer




























    up vote
    1
    down vote













    You can use group by with sum(case when...) like below:



    select pq.Question, pq.PollID,
    sum(case when pr.Answer = 'A' then 1 else 0 end) as 'countA',
    sum(case when pr.Answer = 'B' then 1 else 0 end) as 'countB',
    sum(case when pr.Answer = 'C' then 1 else 0 end) as 'countC',
    sum(case when pr.Answer = 'D' then 1 else 0 end) as 'countD',
    sum(case when pr.Answer = 'E' then 1 else 0 end) as 'countE'
    from PollQuestion pq
    left join PollResponse pr on pq.PollID = pr.PollID
    group by pq.Question, pq.PollID





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


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246081%2fflatten-a-response-to-an-sql-query%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



      accepted










      You can try to use condition aggregate function.



      select 
      pollQuestions.Question,
      pollQuestions.PollID,
      count(CASE WHEN pollResponse.Answer ='A' THEN 1 END) countA,
      count(CASE WHEN pollResponse.Answer ='B' THEN 1 END) countB,
      count(CASE WHEN pollResponse.Answer ='C' THEN 1 END) countC,
      count(CASE WHEN pollResponse.Answer ='D' THEN 1 END) countD,
      count(CASE WHEN pollResponse.Answer ='E' THEN 1 END) countE
      from pollQuestions
      JOIN pollResponse on pollQuestions.PollID = pollResponse.PollID
      group by
      pollQuestions.Question,
      pollQuestions.PollID
      order by
      pollResponse.PollID;


      NOTE



      I would use JOIN instead of comma , because JOIN syntax is clearer than , about connect two tabls.






      share|improve this answer

























        up vote
        1
        down vote



        accepted










        You can try to use condition aggregate function.



        select 
        pollQuestions.Question,
        pollQuestions.PollID,
        count(CASE WHEN pollResponse.Answer ='A' THEN 1 END) countA,
        count(CASE WHEN pollResponse.Answer ='B' THEN 1 END) countB,
        count(CASE WHEN pollResponse.Answer ='C' THEN 1 END) countC,
        count(CASE WHEN pollResponse.Answer ='D' THEN 1 END) countD,
        count(CASE WHEN pollResponse.Answer ='E' THEN 1 END) countE
        from pollQuestions
        JOIN pollResponse on pollQuestions.PollID = pollResponse.PollID
        group by
        pollQuestions.Question,
        pollQuestions.PollID
        order by
        pollResponse.PollID;


        NOTE



        I would use JOIN instead of comma , because JOIN syntax is clearer than , about connect two tabls.






        share|improve this answer























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You can try to use condition aggregate function.



          select 
          pollQuestions.Question,
          pollQuestions.PollID,
          count(CASE WHEN pollResponse.Answer ='A' THEN 1 END) countA,
          count(CASE WHEN pollResponse.Answer ='B' THEN 1 END) countB,
          count(CASE WHEN pollResponse.Answer ='C' THEN 1 END) countC,
          count(CASE WHEN pollResponse.Answer ='D' THEN 1 END) countD,
          count(CASE WHEN pollResponse.Answer ='E' THEN 1 END) countE
          from pollQuestions
          JOIN pollResponse on pollQuestions.PollID = pollResponse.PollID
          group by
          pollQuestions.Question,
          pollQuestions.PollID
          order by
          pollResponse.PollID;


          NOTE



          I would use JOIN instead of comma , because JOIN syntax is clearer than , about connect two tabls.






          share|improve this answer












          You can try to use condition aggregate function.



          select 
          pollQuestions.Question,
          pollQuestions.PollID,
          count(CASE WHEN pollResponse.Answer ='A' THEN 1 END) countA,
          count(CASE WHEN pollResponse.Answer ='B' THEN 1 END) countB,
          count(CASE WHEN pollResponse.Answer ='C' THEN 1 END) countC,
          count(CASE WHEN pollResponse.Answer ='D' THEN 1 END) countD,
          count(CASE WHEN pollResponse.Answer ='E' THEN 1 END) countE
          from pollQuestions
          JOIN pollResponse on pollQuestions.PollID = pollResponse.PollID
          group by
          pollQuestions.Question,
          pollQuestions.PollID
          order by
          pollResponse.PollID;


          NOTE



          I would use JOIN instead of comma , because JOIN syntax is clearer than , about connect two tabls.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 5:31









          D-Shih

          24.4k61431




          24.4k61431
























              up vote
              1
              down vote













              You can use group by with sum(case when...) like below:



              select pq.Question, pq.PollID,
              sum(case when pr.Answer = 'A' then 1 else 0 end) as 'countA',
              sum(case when pr.Answer = 'B' then 1 else 0 end) as 'countB',
              sum(case when pr.Answer = 'C' then 1 else 0 end) as 'countC',
              sum(case when pr.Answer = 'D' then 1 else 0 end) as 'countD',
              sum(case when pr.Answer = 'E' then 1 else 0 end) as 'countE'
              from PollQuestion pq
              left join PollResponse pr on pq.PollID = pr.PollID
              group by pq.Question, pq.PollID





              share|improve this answer

























                up vote
                1
                down vote













                You can use group by with sum(case when...) like below:



                select pq.Question, pq.PollID,
                sum(case when pr.Answer = 'A' then 1 else 0 end) as 'countA',
                sum(case when pr.Answer = 'B' then 1 else 0 end) as 'countB',
                sum(case when pr.Answer = 'C' then 1 else 0 end) as 'countC',
                sum(case when pr.Answer = 'D' then 1 else 0 end) as 'countD',
                sum(case when pr.Answer = 'E' then 1 else 0 end) as 'countE'
                from PollQuestion pq
                left join PollResponse pr on pq.PollID = pr.PollID
                group by pq.Question, pq.PollID





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You can use group by with sum(case when...) like below:



                  select pq.Question, pq.PollID,
                  sum(case when pr.Answer = 'A' then 1 else 0 end) as 'countA',
                  sum(case when pr.Answer = 'B' then 1 else 0 end) as 'countB',
                  sum(case when pr.Answer = 'C' then 1 else 0 end) as 'countC',
                  sum(case when pr.Answer = 'D' then 1 else 0 end) as 'countD',
                  sum(case when pr.Answer = 'E' then 1 else 0 end) as 'countE'
                  from PollQuestion pq
                  left join PollResponse pr on pq.PollID = pr.PollID
                  group by pq.Question, pq.PollID





                  share|improve this answer












                  You can use group by with sum(case when...) like below:



                  select pq.Question, pq.PollID,
                  sum(case when pr.Answer = 'A' then 1 else 0 end) as 'countA',
                  sum(case when pr.Answer = 'B' then 1 else 0 end) as 'countB',
                  sum(case when pr.Answer = 'C' then 1 else 0 end) as 'countC',
                  sum(case when pr.Answer = 'D' then 1 else 0 end) as 'countD',
                  sum(case when pr.Answer = 'E' then 1 else 0 end) as 'countE'
                  from PollQuestion pq
                  left join PollResponse pr on pq.PollID = pr.PollID
                  group by pq.Question, pq.PollID






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 5:32









                  Eray Balkanli

                  3,85741943




                  3,85741943






























                      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%2f53246081%2fflatten-a-response-to-an-sql-query%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

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Lugert, Oklahoma