我有这种表:
Problem UserID Grade
Probleme1 ID1 33
Probleme1 ID2 100
Probleme1 ID4 57
Probleme1 ID6 57
Probleme1 ID78 24
Probleme1 ID5 24
Probleme2 ID1 37
Probleme2 ID12 88
Probleme2 ID6846 100
Probleme2 ID2 2
我想要实现的目标是创建一个包含以下内容的表:
所以我试着像这样做:
DROP TABLE IF EXISTS Problem_Grades_Point;
CREATE TABLE Problem_Grades_Point (
Platform VARCHAR(20),
AnalyticsDomain VARCHAR(255),
ShortTitle VARCHAR(255),
Problem VARCHAR(255),
Grade0 INT,
Grade10 INT,
Grade20 INT,
INDEX (Problem)
) ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_bin;
INSERT INTO Problem_Grades_Point
SELECT
Platform,
AnalyticsDomain,
ShortTitle,
Problem,
COUNT(Problem) AS Grade0,
0 as Grade10,
0 as Grade20
FROM First_Attempt_Problem_Grades
WHERE First_Grade = 0
GROUP BY Problem
UNION ALL
SELECT
Platform,
AnalyticsDomain,
ShortTitle,
Problem,
0 as Grade0,
COUNT(t.Problem) AS Grade10,
0 as Grade20
FROM First_Attempt_Problem_Grades t
WHERE First_Grade = 10
GROUP BY Problem
UNION ALL
SELECT
Platform,
AnalyticsDomain,
ShortTitle,
Problem,
0 as Grade0,
0 as Grade10,
COUNT(Problem) AS Grade20
FROM First_Attempt_Problem_Grades
WHERE First_Grade = 20
GROUP BY Problem
当然,它并不包含我想要的一切,当然,它也不起作用。它仅显示0级的结果。所有其他成绩均为空,如下所示:
我该如何做这个SOV社区? :)
答案 0 :(得分:0)
首先,您需要一个成绩列表。因此,您创建一个数字表,其成绩从1到100,并将其命名为SELECT P.Problem,
G.Grade,
COUNT(Y.UserID)
FROM ( SELECT DISTINCT Problem
FROM YourTable) as P -- if you have a table `Problems` you can use it instead
CROSS JOIN Grades as G
LEFT JOIN YourTable Y
ON P.Problem = Y.Problem
AND G.Grade = Y.Grade
GROUP BY P.Problem, G.Grade
How do I get a list of numbers in MySQL?
现在你做:
public class StudentController : ApiController
{
[HttpDelete]
[Route("student/DeleteStudent/{id}")]
public IHttpActionResult DeleteStudent(string id)
{
using (var sd = new SchoolDBEntities())
{
var student = sd.Students
.Where(s => s.StudentID == id)
.FirstOrDefault();
sd.Entry(student).State = System.Data.Entity.EntityState.Deleted;
sd.SaveChanges();
}
if (id == null)
return BadRequest("Not a valid student id");
return Ok();
}
}