Hint - when showing the results that you get compared to what you expected...
1. Do not include links to images - just put the data into your question as pre-formatted text
2. Make sure that your expected results use the same data as your actual results - the data items in the images you supplied are not related to each other
It's also an extremely good idea to provide samples of the data you are querying from
each of the tables. Help us to help you!
I'm intrigued how you are able to show your actual results, as when I run your query I get an error - "Invalid column name 'Vak'". When I fix that I then start getting errors like "Column '#student.ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause". This is caused by the calculation for
Gemiddelde
I've tried to reproduce your data as follows:
CREATE TABLE #student (ID int, student_Name VARCHAR(50))
insert into #student VALUES (1, 'Harmen'), (2, 'Remon'), (3, 'Stan'), (4, 'Frans de Boer'), (6, 'Kees')
CREATE TABLE #course (ID int, course_Name varchar(50))
insert into #course VALUES (1, 'Nederlands'), (2, 'Engels'), (3, 'Programmeren')
CREATE TABLE #test (ID int, Course_ID int, test_Name varchar(50))
insert into #test VALUES (1, 1, 'Dutch test 1'), (2, 2, 'English Test 1'), (3, 3, 'Programming Test 1')
CREATE TABLE #result (student_ID int, test_ID int, Result decimal(15,2))
insert into #result VALUES (1, 1, 7), (2, 1, 9), (2, 2, 10), (3, 1, 7.5), (3, 2, 8)
The first step in the process is to get all the results along with the average score per student:
SELECT (S.student_Name) AS Leerling, (C.course_Name) AS Vak, R.Result, AVG(R.Result) OVER (PARTITION BY R.student_ID)
FROM #course C
JOIN #test T ON C.ID = T.Course_ID
JOIN #result R ON T.ID = R.test_ID
JOIN #student S ON R.student_ID = S.ID
Which gives you
Harmen Nederlands 7.00 7.000000
Remon Nederlands 9.00 9.500000
Remon Engels 10.00 9.500000
Stan Nederlands 7.50 7.750000
Stan Engels 8.00 7.750000
You can then follow the instructions on this article
Simple Way To Use Pivot In SQL Query[
^] to get the data in the format you want e.g.
SELECT Leerling, ISNULL(Nederlands,0) AS Nederlands, ISNULL(Engels,0) AS Engels, ISNULL(Programmeren,0) as Programmeren, Gemiddeld
FROM
(
SELECT (S.student_Name) AS Leerling, (C.course_Name) AS Vak, R.Result, AVG(R.Result) OVER (PARTITION BY S.ID) AS Gemiddeld
FROM #course C
JOIN #test T ON C.ID = T.Course_ID
JOIN #result R ON T.ID = R.test_ID
JOIN #student S ON R.student_ID = S.ID
) as s
PIVOT
(
SUM(Result)
FOR Vak IN (Nederlands, Engels, Programmeren)
) as p
Results:
Leerling Nederlands Engels Program. Gemiddeld
Harmen 7.00 0.00 0.00 7.000000
Stan 7.50 8.00 0.00 7.750000
Remon 9.00 10.00 0.00 9.500000
Other things to note
- I've used temporary tables so you need to remove the # symbols
- I've used aliases because I don't want to waste time typing out the full table names
- By using a Window Function (AVG-OVER-PARTITION) I've removed the need for a GROUP BY (which overcomes the errors)