Skip to content
Advertisement

Getting wrong output in pivot query

Attendance Table Sample Data –

Student Table Sample Data –

StudentSubject Table Sample Data –

Subject Table Sample Data –

Output –

Output

Wrong output is displaying with parameters –

As 2,1,0 and NA,NA,1 should be displayed instead 2,1,1 and 2,1,1 is displaying.

Couldn’t point out the mistake I am making. Can somebody take a look ?

Advertisement

Answer

You are missing a join condition, Attendance table needs to be joined with SubjectCode as well. Due to the missing condition, it is picking the dates from both the SubjectCodes.

Also start using INNER JOIN syntax over the old style comma separated joins. Keep the join conditions in ON clause and filters in Where clause.

Having said that all, you don’t need those two tables in first place according to your expected result

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement