Flatten a response to an SQL query
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
Consider handling issues of data display in application code
– Strawberry
Nov 11 at 9:25
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
mysql sql
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
add a comment |
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
add a comment |
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.
add a comment |
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
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 at 5:31
D-Shih
24.4k61431
24.4k61431
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 11 at 5:32
Eray Balkanli
3,85741943
3,85741943
add a comment |
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%2f53246081%2fflatten-a-response-to-an-sql-query%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
Consider handling issues of data display in application code
– Strawberry
Nov 11 at 9:25