2
I am developing a database for a small application.
My bank has 3 tables: students, disciplines and boletim_escolar, this last one I use to insert 3 individual notes.
I was able to calculate the average of these NOTES through the SUM() function until then everything, my difficulty is in displaying the three notes of the TABLE followed by this result of the SUM function().
How do I do it correctly? I am using SQL SERVER 2008 R2.
-- select from the banknote table
SELECT * FROM boletim_escolar
-- function that calculate the AVERAGE OF EACH STUDENT - Moses Ramos 7 mins ago
SELECT sum(((nota1*1)+(nota2*1)+(nota3*2))/4) AS 'Media Final'
FROM boletim_escolar GROUP BY id_codaluno
Assuming the table is with the notes would like to display the result of the SUM() next to the nota3 column
Nota1 | nota2 | nota3 | Average ???
It will be possible to do this urgently needed such HELP.
get a little difficult understand what you are trying to do without having the table structure and your select. edit your question with this information.
– Marco Souza
Moses, edit your question and put your remarks to the question
– Marco Souza
Want to see the table script? HOW to upload
– Moisés Ramos
SELECT nota1, nota2, nota3, sum(((nota1*1)+(nota2*1)+(nota3*2))/4) AS 'Media Final' FROM
– Pedro Camara Junior
@Pedrocamarajunior, this is going to be a mistake.
– Marco Souza
This is the error : Msg 8120, Level 16, State 1, Line 1 Column 'school bulletin.Nota1' is invalid in the select list because it is not contained in either an Aggregate Function or the GROUP BY clause.
– Moisés Ramos
@We’re sorry, there’s no way you can display the note to each line that way. the fact that you use GROUP BY will group all a user’s lines this may exist many lines, but if it is the case to have the average for each line then it does not make sense you use the SUM() just sum and divided or use the AVG
– Marco Souza
@Marconcíliosouza is right, I did not pay much attention. I believe that if we add a
SUM()
each note will solve, as all values will be grouped. I had not committed myself to theGROUP BY
in consultation.SELECT id_codaluno, SUM(nota1), SUM(nota2), SUM(nota3), "MEDIA" FROM
– Pedro Camara Junior
@We are a student can or will have more than one
boletim_escolar
?– Pedro Camara Junior
Not at the first moment.
– Moisés Ramos
Note. I am calculating the weighted average where nota3 has weight 2.
– Moisés Ramos
I have inserted the following query : "SELECT sum(Nota11), sum(nota21), sum(note3*2) AS 'Student Media' FROM school bulletin GROUP BY id_codstudent". It returns only the result of the calculations. It would be possible to divide all these results by 4, whichever would be the sum of weighted averages?
– Moisés Ramos