Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everybody!
I have a problem and need your help. I want to display all the mark of student on the DatagridView, so below is my problem. Could anyone help me please. I'll appreciate your help.

create table STUDENT
(
   idstudent  char(10)primary key,
   .....info about student
)
//insert some data into table student
insert into STUDENT values('123456789',...)

create table SUBJECT
(
    idsubject  char(10),
    namesubject  char(10)
)
//i have two subject in this table such as ('111', 'Math') and ('112', 'Physics')

create table MARK
(
   idstudent  char(10) foreign key references STUDENT(idstudent),
   idsubject   char(10) foreign key references SUBJECT(idsubject),
   score   int
)
//i have two record like this ('123456789', '111', 9) and ('123456789', '112', 10)


So now i want to create a query to display all the mark of one student on a row like this:

idstudent    idsubject1(Math)  idsubject2(Physics)
123456789       9                      10
Posted

You should go with cross tab queries which help you to achieve the expected result. Here is best example (for student marks) which may help you.

http://www.sql-programmers.com/Blog/tabid/153/EntryId/6/Using-PIVOT-and-UNPIVOT.aspx[^]

Gud luck!!
 
Share this answer
 
Comments
nguyenle.it 20-Jun-11 22:33pm    
Yeah, using pivot.It work like a charm!! i got what i want.
Thank you very much!!!
The only way I can see you doing this with your current database structure is like this :

C#
SELECT DISTINCT m.idstudent, (SELECT score FROM mark where idsubject = 111) AS idsubject1, (SELECT score FROM mark where idsubject = 112) AS idsubject2 FROM mark m;
but that is pretty ugly. I think you should reconsider you database structure to be honest.

Hope this helps
 
Share this answer
 
Comments
nguyenle.it 20-Jun-11 22:34pm    
Thank you for your comment!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900