select student_id, class_id, section_id, exam_date, exam_id,
sum(number*(1-abs(sign(subject-1)))) as sub1,
sum(number*(1-abs(sign(subject-2)))) as sub2,
sum(number*(1-abs(sign(subject-3)))) as sub3,
sum(number*(1-abs(sign(subject-4)))) as sub4,
sum(number*(1-abs(sign(subject-5)))) as sub5,
sum(number*(1-abs(sign(subject-6)))) as sub6
from result
where class_id = '7' and section id = '3' and YEAR(exam_date) = '2012' and exam_id = '3'
GROUP BY student_id
我有一个问题,当我使用where子句过滤时,所有数值都为0,如果我在没有where子句的情况下运行查询,结果会很好但是来自所有数据库。 如何使用where子句过滤查询? 任何人都可以帮助我吗?
答案 0 :(得分:1)
将查询包裹在where。
之前SELECT * FROM (
select student_id, class_id,
sum(number*(1-abs(sign(subject-1)))) as sub1,
sum(number*(1-abs(sign(subject-2)))) as sub2,
sum(number*(1-abs(sign(subject-3)))) as sub3,
sum(number*(1-abs(sign(subject-4)))) as sub4,
sum(number*(1-abs(sign(subject-5)))) as sub5,
sum(number*(1-abs(sign(subject-6)))) as sub6
from result GROUP BY student_id) m
where class_id = '7'
答案 1 :(得分:1)
您还需要按class_id进行分组
SELECT * FROM (
select student_id, class_id,
sum(number*(1-abs(sign(subject-1)))) as sub1,
sum(number*(1-abs(sign(subject-2)))) as sub2,
sum(number*(1-abs(sign(subject-3)))) as sub3,
sum(number*(1-abs(sign(subject-4)))) as sub4,
sum(number*(1-abs(sign(subject-5)))) as sub5,
sum(number*(1-abs(sign(subject-6)))) as sub6
from result GROUP BY student_id,class_id) m
where class_id = '7'