Use of select with function SUM ()

2

I'm developing a database for a small application.

My bank has 3 tables: students, disciplines and school_letter, the latter used to insert 3 individual notes.

I have been able to calculate the average of these NOTES through the SUM () function so far, my difficulty is to display the three notes of the TABLE followed by this result of the SUM () function.

How do I do this correctly? I'm using SQL SERVER 2008 R2.

- select from the notes table

SELECT * FROM boletim_escolar

- function that calculate the AVERAGE OF EACH STUDENT - Moisés 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, you would like to display the SUM () result next to the nota3 column

nota1 | note2 | note3 | Average ???

It will be possible to urgently need this HELP.

    
asked by anonymous 27.07.2016 / 16:12

3 answers

2
    SELECT sum(nota1) AS 'Media Final' FROM boletim_escolar GROUP BY id_codaluno 

    Decimal N1='Soma da primira nota'*1

    SELECT sum(nota2) AS 'Media Final' FROM boletim_escolar GROUP BY id_codaluno 

    Decimal N2='Soma da segunda nota'*1


    SELECT sum(nota3) AS 'Media Final' FROM boletim_escolar GROUP BY id_codaluno 

    Decimal N3='Soma da 3ª  nota'*2

   decimal  media=(N1+N2+N3)/4

WHAT IS THE PROGRAMMING LANGUAGE ??

    
28.07.2016 / 10:30
0

I do not know why you use the SUM () function since there is no more than one line for each user, when in fact just use + to add the values. >

declare @boletim_escolar table
(
    id_codaluno int,
    nota1 numeric(18,2),
    nota2 numeric(18,2),
    nota3 numeric(18,2)
)

insert into @boletim_escolar values
(1, 7.5 , 6.8 , 8.9),
(2, 1.5 , 7.8 , 3.9),
(3, 9.5 , 5.8 , 7.9)

SELECT nota1, (nota1*1) as 'nota1 Ponderada', nota2,  (nota2*1) AS 'nota2 Ponderada' ,
 nota3 ,(nota3*2) AS 'nota3 Ponderada',  
cast((((nota1*1)+(nota2*1)+(nota3*2))/4) as numeric(18,2)) AS 'Media Final Ponderada' 
FROM @boletim_escolar 

See the resolution below.

nota1   nota1 Ponderada nota2   nota2 Ponderada nota3   nota3 Ponderada Media Final Ponderada
7.50    7.50    6.80    6.80    8.90    17.80   8.03
1.50    1.50    7.80    7.80    3.90    7.80    4.28
9.50    9.50    5.80    5.80    7.90    15.80   7.78
    
29.07.2016 / 20:28
0
select nota1,nota2,nota3,sum(((nota1*1)+(nota2*1)+(nota3*2))/4) AS 'Media Final' 
from boletim_escolar 
GROUP BY id_codaluno

I did not understand why you use sum() instead of avg() , but then I noticed that the notes have weight.

    
28.07.2016 / 13:31