I need to create a query that returns all active students and their grades for each subject. If the student does not have a grade for a given subject, the student must return the subject and note the value "No grade".
I think I'll need to create using CASE, but I'm not sure yet. Well, I can not create this query. Can you give me a help?
Below is the structure of the bank (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,
)
Here is the query that I have been able to put together 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 does not have a note released.