计算多列中的不同值

时间:2016-06-03 18:22:41

标签: mysql

我有这张桌子:

╔════════════════╤═══════════════════╤═══════════╤═══════════╗
║ question1      │ question2         │ question3 │ question4 ║
╠════════════════╪═══════════════════╪═══════════╪═══════════╣
║ Agree          │ Disagree          │ Agree     │ Disagree  ║
╟────────────────┼───────────────────┼───────────┼───────────╢
║ Strongly Agree │ Strongly Disagree │ Agree     │ Disagree  ║
╚════════════════╧═══════════════════╧═══════════╧═══════════╝

我正在尝试使用COUNT()编写一个查询,它显示每个问题的回复数量,如下所示:

╔══════════╤════════════════╤═══════╤══════════╤═══════════════════╗
║ Question │ Strongly Agree │ Agree │ Disagree │ Strongly Disagree ║
╠══════════╪════════════════╪═══════╪══════════╪═══════════════════╣
║ Q1       │ 1              │ 1     │ 0        │ 0                 ║
╟──────────┼────────────────┼───────┼──────────┼───────────────────╢
║ Q2       │ 0              │ 0     │ 1        │ 1                 ║
╟──────────┼────────────────┼───────┼──────────┼───────────────────╢
║ Q3       │ 0              │ 2     │ 0        │ 0                 ║
╟──────────┼────────────────┼───────┼──────────┼───────────────────╢
║ Q4       │ 0              │ 0     │ 2        │ 0                 ║
╚══════════╧════════════════╧═══════╧══════════╧═══════════════════╝

我尝试了几次查询,但总是给我错误的结果。任何帮助,将不胜感激。感谢。

2 个答案:

答案 0 :(得分:3)

不确定为什么选择按照显示的方式构建表格,但如果您可以灵活地更改它,我建议您这样做。使用现在提供的结构,您不仅在使用所需结果获得正确查询时遇到问题,而且还有一个结构无法在没有DB模式更新的情况下添加新问题。

如果您无法修改表格结构 SQL DEMO

SELECT 'Q1' as Question , 
        Count(CASE WHEN Question1 = 'Strongly Agree'    THEN 1 END) AS 'Strongly Agree',
        Count(CASE WHEN Question1 = 'Agree'             THEN 1 END) AS 'Agree',
        Count(CASE WHEN Question1 = 'Disagree'          THEN 1 END) AS 'Disagree',
        Count(CASE WHEN Question1 = 'Strongly Disagree' THEN 1 END) AS 'Strongly Disagree'
FROM QandR
UNION ALL
SELECT 'Q2' as Question , 
        Count(CASE WHEN Question2 = 'Strongly Agree'    THEN 1 END) AS 'Strongly Agree',
        Count(CASE WHEN Question2 = 'Agree'             THEN 1 END) AS 'Agree',
        Count(CASE WHEN Question2 = 'Disagree'          THEN 1 END) AS 'Disagree',
        Count(CASE WHEN Question2 = 'Strongly Disagree' THEN 1 END) AS 'Strongly Disagree'
FROM QandR
UNION ALL
SELECT 'Q3' as Question , 
        Count(CASE WHEN Question3 = 'Strongly Agree'    THEN 1 END) AS 'Strongly Agree',
        Count(CASE WHEN Question3 = 'Agree'             THEN 1 END) AS 'Agree',
        Count(CASE WHEN Question3 = 'Disagree'          THEN 1 END) AS 'Disagree',
        Count(CASE WHEN Question3 = 'Strongly Disagree' THEN 1 END) AS 'Strongly Disagree'
FROM QandR
UNION ALL
SELECT 'Q4' as Question , 
        Count(CASE WHEN Question4 = 'Strongly Agree'    THEN 1 END) AS 'Strongly Agree',
        Count(CASE WHEN Question4 = 'Agree'             THEN 1 END) AS 'Agree',
        Count(CASE WHEN Question4 = 'Disagree'          THEN 1 END) AS 'Disagree',
        Count(CASE WHEN Question4 = 'Strongly Disagree' THEN 1 END) AS 'Strongly Disagree'
FROM QandR

enter image description here

如果您可以更改结构

以下是我的建议:

2表:问题& QuestionResponse

  1. 问题有2列
    1. id(int; autoincrement)
    2. 问题(varchar)
  2. enter image description here

    1. QuestionRresponse有3列
      1. id(int; autoincrement)
      2. QuestionId(int; FK to Question:id)
      3. 响应(varchar)
    2. enter image description here

      然后,您可以使用此查询和输出获取您正在查找的数据:

      SELECT q.Question, qr.Response, Count(qr.Response) as Count
      FROM  `Question` q
      LEFT JOIN QuestionResponse qr ON q.id = qr.QuestionId
      GROUP BY q.Question,qr.Response
      

      enter image description here

答案 1 :(得分:0)

首先,您需要两个表{​​{1}}和Questions,以便执行Answers并填写LEFT JOIN

NULL's

然后你需要打开你的桌子。 MySQL - turn table into different table

CREATE TABLE `Questions` (`id` int, `Question` varchar(17));  
INSERT INTO `Questions`  (`id`, `Question`)
VALUES
    (1, 'question1'),(2, 'question2'),(3, 'question3'),(4, 'question4');

CREATE TABLE `Answers`  (`id` int, `choice` varchar(17));
INSERT INTO `Answers`   (`id`, `choice`)
VALUES
    (1, 'Strongly Agree'),(2, 'Agree'),(3, 'Disagree'),(4, 'Strongly Disagree');

然后,您将两个结果连接在一起并执行数据透视MySQL pivot table

<强> SQL Fiddle Demo

select c.col,
       case c.col
            when 'question1' then question1
            when 'question2' then question2
            when 'question3' then question3
            when 'question4' then question4
       end as `data` 
from yourTable t     
cross join
       (
        select 'question1' as col
        union all select 'question2'
        union all select 'question3'
        union all select 'question4'
       ) c

<强>输出

enter image description here