I have 4 tables in a SQL Server database with following schema:
Attendance
CREATE TABLE [dbo].[Attendance] ( [AttendanceId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL, [CourseId] UNIQUEIDENTIFIER NOT NULL, [StudentId] UNIQUEIDENTIFIER NOT NULL, [SubjectId] UNIQUEIDENTIFIER NOT NULL, [Semester] INT NOT NULL, [Month] NVARCHAR (50) NOT NULL, [Count] INT NOT NULL, CONSTRAINT [PK_Attendance] PRIMARY KEY NONCLUSTERED ([AttendanceId] ASC), CONSTRAINT [FK_Attendance_Student] FOREIGN KEY ([StudentId]) REFERENCES [dbo].[Student] ([StudentId]) );Course
CREATE TABLE [dbo].[Course] ( [CourseId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL, [Name] NVARCHAR (50) NOT NULL, CONSTRAINT [PK_Course] PRIMARY KEY NONCLUSTERED ([CourseId] ASC) );Student
CREATE TABLE [dbo].[Student] ( [StudentId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL, [CourseId] UNIQUEIDENTIFIER NOT NULL, [Name] NVARCHAR (100) NOT NULL, [RollNo] INT NOT NULL, [Semester] INT NOT NULL, CONSTRAINT [PK_Student] PRIMARY KEY NONCLUSTERED ([StudentId] ASC), CONSTRAINT [FK_Student_Course] FOREIGN KEY ([CourseId]) REFERENCES [dbo].[Course] ([CourseId]) );Subject
CREATE TABLE [dbo].[Subject] ( [SubjectId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL, [CourseId] UNIQUEIDENTIFIER NOT NULL, [Name] NVARCHAR (100) NOT NULL, [Semester] INT NOT NULL, CONSTRAINT [PK_Subject] PRIMARY KEY NONCLUSTERED ([SubjectId] ASC), CONSTRAINT [FK_Subject_Course] FOREIGN KEY ([CourseId]) REFERENCES [dbo].[Course] ([CourseId]) );
I need to create a attendance report in the following format:
Course Name | Student Name | Subject Name | Semester | Month | Count
Please tell me what SQL Query I need to use and if there's any change in schema required then suggest the same.
I'm looking forward to have your replies.
Thanks,
SQL Joins.