我有这张桌子:
╔════════════════╤═══════════════════╤═══════════╤═══════════╗
║ 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 ║
╚══════════╧════════════════╧═══════╧══════════╧═══════════════════╝
我尝试了几次查询,但总是给我错误的结果。任何帮助,将不胜感激。感谢。
答案 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
如果您可以更改结构
以下是我的建议:
2表:问题& QuestionResponse
答案 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
<强>输出强>