我有一个名为questions
的表,其中包含以下数据。数据是通过人们的回答收集的。每个问题包含最多20个选项。
form q1 q2 q3 q4 q5 q6
1 16 1 2 5 11 9
2 9 6 16 5 2 8
3 8 3 2 5 11 9
4 16 1 2 5 11 7
5 16 6 4 5 11 4
6 15 1 2 5 11 3
7 16 1 2 5 11 4
8 15 1 16 5 11 6
.
.
一个人只能回答一次。有超过10万人回答。每个人都必须回答这六个问题 结果每隔一小时公布一次,所以我必须在每个答案后找出摘要。还有另一张表,我保留了所有六个问题的正确答案
question Answer
1 16
2 1
3 2
4 5
5 11
6 9
现在我希望我的输出在第一个结果后如下所示:
1 / 6 (1 correct answer out of 6 )= 4
第二个结果后:
2 / 6 (1 correct answer out of 6 )= 3
1 / 6 (1 correct answer out of 6 )= 3
在第三个结果之后:
3 / 6 (1 correct answer out of 6 )= 3
2 / 6 (1 correct answer out of 6 )= 1
1 / 6 (1 correct answer out of 6 )= 3
.
.so on till sixth result
.
之后的第六个结果:
6/ 6 (1 correct answer out of 6 )= 1
5 / 6 (1 correct answer out of 6 )= 2
4 / 6 (1 correct answer out of 6 )= some value
3 / 6 (1 correct answer out of 6 )= some value
2 / 6 (1 correct answer out of 6 )= some value
1 / 6 (1 correct answer out of 6 )= some value
我希望结果按降序排列。如果没有匹配则会显示0.
答案 0 :(得分:0)
我认为你可以使用这样的查询:
;with unpivoted as (
select form, 1 question, q1 answer from questions q
union all
select form, 2 question, q2 answer from questions q
union all
select form, 3 question, q3 answer from questions q
union all
select form, 4 question, q4 answer from questions q
union all
select form, 5 question, q5 answer from questions q
union all
select form, 6 question, q6 answer from questions q
), results as (
select u.form, count(ca.Answer) corrects
from unpivoted u
left join correctAnswers ca
on u.question = ca.question and u.answer = ca.Answer
group by u.form
)
select cast(coalesce(u.question, r.corrects) as varchar(7)) + ' / 6' correctsPer6, count(distinct r.form) countOfForms
from unpivoted u
full outer join results r on u.question = r.corrects
group by u.question, r.corrects
order by u.question desc;
答案 1 :(得分:0)
I i do understand the question correctly, think this is what you wanted
-- Sample Table
declare @question table
(
form int,
q1 int,
q2 int,
q3 int,
q4 int,
q5 int,
q6 int
)
-- Sample Data
insert into @question
select 1, 16, 1, 2, 5, 11, 9 UNION all
select 2, 9, 6, 16, 5, 2, 8 UNION all
select 3, 8, 3, 2, 5, 11, 9 UNION all
select 4, 16, 1, 2, 5, 11, 7 UNION all
select 5, 16, 6, 4, 5, 11, 4 UNION all
select 6, 15, 1, 2, 5, 11, 3 UNION all
select 7, 16, 1, 2, 5, 11, 4 UNION all
select 8, 15, 1, 16, 5, 11, 6
declare @answer table
(
q int,
ans int
)
insert into @answer
select 1, 16 union all
select 2, 1 union all
select 3, 2 union all
select 4, 5 union all
select 5, 11 union all
select 6, 9
-- the query
; with q as
(
-- Pivot the Question table to form, q, ans
select form, q, ans
from (
select form, [1] = q1, [2] = q2, [3] = q3, [4] = q4, [5] = q5, [6] = q6
from @question
) q
unpivot
(
ans for
q in ([1], [2], [3], [4], [5], [6])
) up
)
select s.q,
c.correct
from @answer s
cross apply
(
select correct = count(*)
from
(
select q.form, correct = sum(case when a.ans = q.ans then 1 else 0 end)
from @answer a
inner join q on a.q = q.q
where a.q <= s.q
group by q.form
having sum(case when a.ans = q.ans then 1 else 0 end) = s.q
) c
) c