展平对SQL查询的响应

时间:2018-11-11 05:25:35

标签: mysql sql

我正在尝试使用mysql为问答表获取平面表。这是我的桌子。

PollQuestion

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

PollReponse

PollReponseID | PollID | UserID | Answer

PollAnswer表中,我得到五个答案,分别为VARCHAR A, B, C, D, E

我写了一个查询,将答案按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;

以下哪个给我答复。

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 

我想这样扁平化答案。

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

无论如何我可以在不更改表结构的情况下实现这一目标吗?

任何指针将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以尝试使用条件汇总功能。

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;

注意

我将使用JOIN而不是逗号,,因为关于连接两个表,JOIN的语法比,清晰。

答案 1 :(得分:1)

您可以将For Example 1, where the loop iteration count is not available before the loop executes: If the loop iteration count and iterations lower bound can be calculated for the whole loop: Move the calculation outside the loop using an additional variable. Rewrite the loop to avoid goto statements or other early exits from the loop. Identify the loop iterations lower bound using a constant. For example, introduce the new limit variable: void foo(float *A) { int i; int OuterCount = 90; int limit = bar(int(A[0])); while (OuterCount > 0) { for (i = 1; i < limit; i++) { A[i] = i + 4; } OuterCount--; } } For Example 2, where the compiler cannot determine if there is aliasing between all the pointers used inside the loop and loop boundaries: Assign the loop boundary value to a local variable. In most cases, this is enough for the compiler to determine aliasing may not occur. You can use a directive to accomplish the same thing automatically. Target ICL/ICC/ICPC Directive Source loop #pragma simd or #pragma omp simd Do not use global variables or indirect accesses as loop boundaries unless you also use one of the following: Directive to ignore vector dependencies Target ICL/ICC/ICPC Directive Source loop #pragma ivdep restrict keyword. group by一起使用,如下所示:

sum(case when...)