sql - Query on table joined with itself -


today have final exam. approved, happily :d 1 of problems blowing mind.

i need help, can rest in peace.

the problem

we have table "people"

(pk)id | name | fatherid     ---------------------     1  | gon  |   2     2  | cesar|   6     3  | luz  |   2     4  | maria|   5     5  | diego|   6     6  | john |  - 

this example of data. table has relation itself, on table fatherid(fk) table id(pk) need query show me 2 columns, in 1 name of person, , in one, his/her cousin.

pretty simple until here, right? problem have restrictions

  • only ansi allowed. no t-sql, or one. also, ansi 99 standard, not 2003 or higher
  • subquerys not allowed. , worst:
  • no relations repeated.

for example, considering in example, gon , maria cousins. if show, gon | maria in results, can't show maria | gon.

so, how can this? burning head.

what tried?

well, big problem in last requisite, repetition of data. ignoring that, put on exam (knowing wrong..)

select p3.name oneperson, p4.name cousin people p1 inner join people p2 on p1.fatherid = p2.fatherid , p1.id != p2.id inner join people p3 on p1.id = p3.fatherid inner join people p4 on p1.id = p4.fatherid 

of course, not solving last requeriment, , have 4 in test(we pass 4) anyway, head burning. please, me!

another options explored

one of friends, had same exam said me

"well, considering every relation duplicated, can use top count(*) , order , half correct"

but.. top not ansi!

you can add query where p3.id < p4.id. eliminate duplicate results gon | maria , maria | gon.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -