[英]Consolidate Rows with SQL
Trying to consolidate test results so that each test is in one row.试图合并测试结果,使每个测试都排成一行。 Database I am pulling from has an answer result per row.
我从中提取的数据库每行都有一个答案结果。 Sample Data:
样本数据:
Student Name![]() |
Test Name![]() |
Question Attempt![]() |
Answered Correctly (0 = no, 1= yes)![]() |
---|---|---|---|
Mary![]() |
Intro Assessment![]() |
1 ![]() |
1 ![]() |
Mary![]() |
Intro Assessment![]() |
1 ![]() |
0 ![]() |
Mary![]() |
Intro Assessment![]() |
1 ![]() |
1 ![]() |
Joseph![]() |
Intro Assessment![]() |
1 ![]() |
1 ![]() |
Joseph![]() |
Intro Assessment![]() |
1 ![]() |
1 ![]() |
Joseph![]() |
Intro Assessment![]() |
1 ![]() |
1 ![]() |
Tom![]() |
Intro Assessment![]() |
1 ![]() |
0 ![]() |
Tom![]() |
Intro Assessment![]() |
1 ![]() |
0 ![]() |
Tom![]() |
Intro Assessment![]() |
1 ![]() |
1 ![]() |
Desired result if possible:如果可能的话,期望的结果:
Student Name![]() |
Test Name![]() |
Question Attempt![]() |
Answered Correctly![]() |
---|---|---|---|
Mary![]() |
Intro Assessment![]() |
3 ![]() |
2 ![]() |
Joseph![]() |
Intro Assessment![]() |
3 ![]() |
3 ![]() |
Tom![]() |
Intro Assessment![]() |
3 ![]() |
1 ![]() |
Any guidance would be appreciated.任何指导将不胜感激。
It sounds like you just want a basic GROUP BY
with some SUM
s:听起来您只想要一个带有一些
SUM
的基本GROUP BY
:
SELECT "Student Name", "Test Name"
, SUM("Question Attempt") "Question Attempt"
, SUM("Answered Correctly") "Answered Correctly"
FROM mytable
GROUP BY "Student Name", "Test Name"
GROUP BY "Student Name", "Test Name"
means you want one row per unique student/test and SUM
explains how you want the other data combined. GROUP BY "Student Name", "Test Name"
表示您希望每个唯一的学生/测试有一行, SUM
说明您希望如何组合其他数据。
select student_name, test_name
, count(question_attempt), sum(answered_correctly)
from table
group by student_name, test_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.