2
I need to create a query that returns all active students and their grades for each subject. If the student has no grade for a particular subject should be returned the student, the subject and in note the value "No grade".
I think I’ll need to create using CASE, but I’m not sure yet. Well, I’m not getting to create this query. Can you give me a help?
Below follows the seat structure (reduced)
CREATE TABLE [dbo].[Aluno](
[codigoAluno] [int] NOT NULL,
[nome] [varchar](60) NOT NULL,
[cpf] [decimal](15, 0) NOT NULL,
[situacao] [bit] NOT NULL,
)
CREATE TABLE [dbo].[Materia](
[codigoMateria] [int] NOT NULL,
[nome] [varchar](60) NOT NULL,
[situacao] [bit] NOT NULL
)
CREATE TABLE [dbo].[Nota](
[codigoAluno] [int] NOT NULL,
[codigoMateria] [int] NOT NULL,
[sequencialMateria] [int] NOT NULL,
[valor] [decimal](15, 2) NOT NULL,
)
Follow the query I’ve been able to mount so far.
SELECT
A.NOME,
M.NOME,
CASE WHEN SUM(M.valor) > 0
THEN SUM(M.valor)
ELSE '0' -- Aqui ainda tem que mudar para 'Sem nota'
END AS 'NOTA'
FROM MATERIA M
LEFT JOIN NOTA N ON M.codigoMateria = N.codigoMateria
LEFT JOIN ALUNO A ON A.codigoALUNO = N.codigoALUNO
--WHERE F.situacao = 1
GROUP BY A.NOME, M.nome, N.valor
ORDER BY A.NOME, M.NOME
In this query, it is only necessary to add the subject whose student has not launched grade.
https://technet.microsoft.com/pt-br/library/ms187518(v=sql.105). aspx search for Outer Join
– Motta
Try changing the line
CASE WHEN SUM(M.valor) > 0
forCASE WHEN SUM(M.valor) IS NOT NULL
, see if it solves.– PedroMVM
Pedromvm does not work because it does not display the subject whose student has not launched grade.
– Bruno Heringer
@Brunoheringer: Should every student do all the subjects in the MATERIA table? // What is the sequential column? // Is it to list each subject as a column? What does the column mean
situacao
, tableMATERIA
?– José Diz