我有一个关于计算调查结果并将数据放在列而不是行中的问题... a.VARNAME是一个问题而a.value就是答案。我想计算每个问题0,1,2,3,4,5的答案数。我得到了我想要的结果,但它只是一行......我如何得到问题'varname'作为列标题,然后回答0,1,2,3,4,作为行??
select
sum(CASE WHEN a.VARNAME = 'R2' and a.value = '0' then 1 else 0 end) as 'Answered 0',
sum(CASE WHEN a.VARNAME = 'R2' and a.value = '1' then 1 else 0 end) as 'Answered 1',
sum(CASE WHEN a.VARNAME = 'R2' and a.value = '2' then 1 else 0 end) as 'Answered 2',
sum(CASE WHEN a.VARNAME = 'R2' and a.value = '3' then 1 else 0 end) as 'Answered 3',
sum(CASE WHEN a.VARNAME = 'R2' and a.value = '4' then 1 else 0 end) as 'Answered 4',
sum(CASE WHEN a.VARNAME = 'R2' and a.value = '5' then 1 else 0 end) as 'Answered 5',
sum(CASE WHEN a.VARNAME = 'R2' then 1 else 0 end) as 'Total',
sum(CASE WHEN a.VARNAME = 'A1' and a.value = '0' then 1 else 0 end) as 'Answered 0 ',
sum(CASE WHEN a.VARNAME = 'A1' and a.value = '1' then 1 else 0 end) as 'Answered 1 ',
sum(CASE WHEN a.VARNAME = 'A1' and a.value = '2' then 1 else 0 end) as 'Answered 2 ',
sum(CASE WHEN a.VARNAME = 'A1' and a.value = '3' then 1 else 0 end) as 'Answered 3',
sum(CASE WHEN a.VARNAME = 'A1' and a.value = '4' then 1 else 0 end) as 'Answered 4 ',
sum(CASE WHEN a.VARNAME = 'A1' and a.value = '5' then 1 else 0 end) as 'Answered 5 '
from survey_patient p
inner join survey_analysis a
on p.survey_id = a.survey_id
inner join survey_question_map q
on a.varname = q.varname and p.[service] = q.[service]
答案 0 :(得分:1)
您希望每行中的计数作为varname。怎么样?
select a.VarName, vals.value,
sum(case when vals.value = a.value then 1 else 0 end) as TheCount
from (select '0' as value union all
select '1' union all
select '2' union all
select '3' union all
select '4' union all
select '5'
) as vals cross join
survey_patient p inner join
survey_analysis a
on p.survey_id = a.survey_id inner join
survey_question_map q
on a.varname = q.varname and p.[service] = q.[service];
group by a.varname, vals.value;
您只是从survey_analysis
提取数据。如果不需要其他表,可以将其简化为:
select a.VarName, vals.value,
sum(case when vals.value = a.value then 1 else 0 end) as TheCount
from (select '0' as value union all
select '1' union all
select '2' union all
select '3' union all
select '4' union all
select '5'
) as vals cross join
survey_analysis a
group by a.varname, vals.value;
答案 1 :(得分:1)
我刚从大学毕业后的第一份工作是在开始时进行医学调查。
我们的报告是项目分析。基本上,每个值(0..5)的每个答案计数+1作为子总数并计算总答案。将小计除以总计得到平均值,然后用一个漂亮的小条形图将其打印在调查问题旁边。
我认为这是你想要做的事情?
让我们创建一个小测试数据库。
-- Just play
USE Tempdb;
Go
-- Create the table
CREATE TABLE survey_analysis
(
-- links to survey_patient (FK)
survey_id INT not null,
-- links to survey_question_map (FK)
varname VARCHAR(20),
-- actual answer (0=n/a)
varvalue INT DEFAULT 0
);
GO
-- Remove data when playing
truncate table survey_analysis;
GO
-- Create some data
insert into survey_analysis values
(1, 'R2', 1),
(1, 'A1', 1),
(2, 'R2', 2),
(2, 'A1', 2),
(3, 'R2', 3),
(3, 'A1', 3),
(4, 'R2', 4),
(4, 'A1', 4),
(5, 'R2', 5),
(5, 'A1', 5),
(6, 'X1', 1),
(6, 'X2', 2),
(7, 'R2', 5);
GO
-- Show the data
select * from survey_analysis;
GO
虽然您有survey_patient和survey_question_map表,但它们只是背景噪音。您想在survey_analysis表上计算总计。
--
-- Get sub-total by question (varname), answer(myvalue), and running total
--
SELECT
A.varname,
Q.myvalue,
Q.myanswer,
SUM(CASE WHEN Q.myvalue = A.varvalue THEN 1 ELSE 0 END) as mysubtotal,
SUM(1) as mytotal
FROM
-- Driver table if we do not have all combinations
(
VALUES
(0, 'Answered 0'),
(1, 'Answered 1'),
(2, 'Answered 2'),
(3, 'Answered 3'),
(4, 'Answered 4'),
(5, 'Answered 5')
) AS Q (myvalue, myanswer)
CROSS JOIN survey_analysis as A
-- Filter for questions with 0-5 answers
WHERE
A.varname in ('A1', 'R2')
GROUP BY
A.varname,
Q.myvalue,
Q.myanswer;
GO
以下是我解决问题的方法。
1 - 为有5个答案的问题(Q)创建 DERIVED 表。
2 - 在survey_analysis上使用 CROSS JOIN ,以确保我们拥有所有组合。
3 - 使用 WHERE 子句进行过滤。不包括< 5个答案
4 - GROUP BY 问题,答案,回答标签。
5 - 按项目包括小计和总计,以显示统计信息。
如果你有一堆不同答案的问题:重复查询;调整派生表的答案数量;使用UNION ALL将所有结果合并到一个结果集中;添加一些像问题#的ID,以便您可以订购最终结果。