行转列,动态列枚举分组
【问题】
Hi All,
Thanks for the wonderful support the community gets from this forum.
I am trying to accomplish this in MongoDB. Didn’t think it could get this complicated. thought the problem was interesting to solve.
I am trying to get a count of students by scores for various subjects.
the example below shows 2 subjects, in reality we could run this ad-hoc query for 1 or more subjects. (so cannot “can” and should be real-time)
(for e.g. run the query for a bunch of schools and a bunch of subjects)
the grades are always 1-5 no decimal point
use students
db.studentsummary.insert ({school:‘atl1’, sname : ‘Sean’ , sub1: 4, sub2 :5})
db.studentsummary.insert ({school:‘atl1’, sname : ‘chris’ , sub1: 4, sub2 :3})
db.studentsummary.insert ({school:‘atl1’, sname : ‘becky’ , sub1: 5, sub2 :4})
db.studentsummary.insert ({school:‘