Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 unary relationship

Author  Topic 

newbie2006
Starting Member

7 Posts

Posted - 2006-06-26 : 02:42:50
i have a problem with deriving sql statement which involve a unary relationship for one of the table.

i have tables with the following attributes:
studetn table: student_email (PK), student name
section table: section_no, section_semester (composite primary key)

the above has a a M:M relationship. (in my sql database, i will have a student_section table in btw).

There is also another evaluation table which records the student_email of student evaluating another student (this will be shown as team_member email)
evaluation table: evaluation_id (PK), student_email, teammember_email, studentinput

I use the following sql to get the student name for the student who is evaluating and the students being evaluated:
select form_id, temp_tb.student_email, temp_tb.student_name, teammember_email, student.student_name as teammember_name, studentinput
from (select form_id, student.student_email, student.student_name, teammember_email, studentinput
from student inner join form_evaluation
on evaluation.student_email=student.student_email) as temp_tb
inner join student
on temp_tb.teammember_email=student.student_email

After that, i need to join to the student_section table. However because a student can be in different sections, so the student section table will have 2 entries with the same studentemail, therefore the above query (when inner join with student_section table)actually retrieve the same result twice.

student section:
AAA@edu.com | section1 | 2005/2006
BBB@edu.com | section1 | 2005/2006
AAA@edu.com | section 2 | 2005/2006


final query results: evlauation table
formid| studentemail | teammemberemail | studentinput
11111| AAA@edu.com | BBB@edu.com | "very good"
11111| AAA@edu.com | BBB@edu.com | "very good"
11111| BBB @edu.com | AAA@edu.com | "ok"
11111| BBB @edu.com | AAA@edu.com | "ok"

how do i resolve the problem?



SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-06-26 : 04:22:31
The easy way would be to use DISTINCT.
select DISTINCT	form_id,
temp_tb.student_email,
temp_tb.student_name,
teammember_email,
student.student_name as teammember_name,
studentinput
from (
select form_id,
student.student_email,
student.student_name,
teammember_email,
studentinput
from student
inner join form_evaluation on evaluation.student_email = student.student_email
) as temp_tb
inner join student on temp_tb.teammember_email = student.student_email
Also, you should try to qualify your queries by putting table names before fields for easier reading and speedier selects.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-06-26 : 14:57:17
OK, what is that?

"unary relationship"

Is this a college course term?

Time to GOOGLE



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-06-26 : 15:00:02
Wow

http://en.wikipedia.org/wiki/Entity-relationship_model

Are they making these things up? I don't Remeber Dr. Codd ever discussing such things. How can that even be? What it does mean then is that the Data model is wrong...so they are teaching bad habits?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2006-06-26 : 17:00:30
The number of (different) entities participating in the relationship.
Then each relationship can have different cardinality (N:M) http://www.datamodel.org/DataModelCardinality.html

rockmoose
Go to Top of Page

CSK
Constraint Violating Yak Guru

489 Posts

Posted - 2006-06-27 : 00:11:27
quote:
Originally posted by X002548

OK, what is that?

"unary relationship"

Is this a college course term?

Time to GOOGLE



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam





A binary relation is a function which takes in two elements of the domain,
and returns true or false (indicating that those two have the desired relation,
or that they don't).


A unary relation takes in only one input instead of two. You can have ternary
and quaternary relations, etc.; however any given relation has a fixed arity
(number of inputs).

-- KK
Go to Top of Page
   

- Advertisement -