为什么sum()函数在这种情况下不给出确切的值?

时间:2018-05-29 10:05:24

标签: mysql sql rdbms

table elaborate_term_result_sheet

select s_id, s_f_name, s_l_name, s_roll_no, s_class, 
       SUM(mid_1_english + mid_2_english)/2 as mid_english, 
       SUM(mid_1_mathematics + mid_2_mathematics)/2 as mid_mathematics,
       semester_final_english,
       semester_final_mathematics,
       SUM((mid_1_english + mid_2_english)/2 + semester_final_english) as total_english, 
       SUM((mid_1_mathematics + mid_2_mathematics)/2 + semester_final_mathematics) as total_mathematics,
       SUM((mid_1_mathematics + mid_2_mathematics)/2 + (mid_1_english + mid_2_english)/2 + semester_final_mathematics + semester_final_mathematics) as total
from elaborate_term_result_sheet 
group by s_f_name, s_l_name
order by s_roll_no;

运行此查询后,它会显示输出,但值会有点波动

Result of the query

mid_1_english + mid_2_english = mid_english

mid_1_mathematics + mid_2_mathematics = mid_mathematics

mid_english + semester_final_english = total_english

mid_mathematics + semester_final_mathematics = total_mathematics

total = total_english + total_mathematics

1 个答案:

答案 0 :(得分:0)

select s_id, s_f_name, s_l_name, s_roll_no, s_class, 
   SUM(mid_1_english + mid_2_english)/2 as mid_english, 
   SUM(mid_1_mathematics + mid_2_mathematics)/2 as mid_mathematics,
   semester_final_english,
   semester_final_mathematics,
   SUM((mid_1_english + mid_2_english)/2 + semester_final_english) as total_english, 
   SUM((mid_1_mathematics + mid_2_mathematics)/2 + semester_final_mathematics) as total_mathematics,
   SUM((mid_1_english + mid_2_english)/2 + semester_final_english + (mid_1_mathematics + mid_2_mathematics)/2 + semester_final_mathematics) as total from result_sheet_1 group by s_f_name, s_l_name order by s_roll_no;

给出正确输出的唯一变化是

SUM((mid_1_english + mid_2_english)/2 + semester_final_english + (mid_1_mathematics + mid_2_mathematics)/2 + semester_final_mathematics) as total

the snapshot of the output of this query