Retrieve Data from Different Tables (SQL Server) -
i have 4 tables in sql server database 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 create attendance report in following format:
course name | student name | subject name | semester | month | count
please tell me sql query need use , if there's change in schema required suggest same.
i'm looking forward have replies.
thanks,
you need use join
in query returns rows match [studentid]
attendance table.
e.g.
select c.coursename, s.studentname, u.subjectname, u.semester, a.month, a.count student s join attendance on s.studentid = a.studentid join course c on a.courseid = c.courseid join subject u on c.courseid = u.courseid
something along these lines return rows match
Comments
Post a Comment