0
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 子句过滤查询?谁能帮我?

4

2 回答 2

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'
于 2012-08-07T09:32:33.067 回答
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' 
于 2012-08-07T09:31:02.810 回答