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.
| Author |
Topic |
|
PatriciaMarie_31
Starting Member
3 Posts |
Posted - 2009-03-26 : 16:39:55
|
| I am customizing reports out of our database. I need to add the parent org to the user. By using the student table and the org relation table, i can mentally map the relationship to the parent org. (There are many child to a parent) Just can seem to figure out how to write the code.if I select s.stud_id s.orggives me the student org (with s=student table)now I want to map this to the parent org on the org relation table (ort) and (ortp) for the parents.org = ort.org childthen select ortp.org parentand of course how would I get this be the final org id that shows in the report.Sorry, I am very new at this and have been able to teach myself how to add in a new column for supervisor since it was in the student table. |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-03-26 : 16:49:43
|
| It would be better if you give us some sample data and expected output. |
 |
|
|
PatriciaMarie_31
Starting Member
3 Posts |
Posted - 2009-03-26 : 17:23:49
|
quote: Originally posted by vijayisonly It would be better if you give us some sample data and expected output.
Does this help?Stud TableStudent ID: PMWName: PatriciaOrg: Mom4Stud ID: LJBName: LindaOrg: Mom2Stud ID: DEBName: DaleOrg: Mom3Stud ID: CPWOrg: Dad4Org Relation TableParent Org: MomOrg Child: Mom4Parent Org: MomOrg Child: Mom2Parent Org: MomOrg Child: Mom3Parent Org: DadOrg Child: Dad4My report would produce (CSV)Student Id, Student Name, Parent Org |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-03-26 : 17:29:36
|
| You dint give the expected output...but maybe thisSELECT s.Student_Id,s.Student_Name,o.Parent_OrgFROM Stud_Table s INNER JOIN Org_Relation_Table o ON s.Org = o.Org_Child |
 |
|
|
PatriciaMarie_31
Starting Member
3 Posts |
Posted - 2009-03-26 : 17:57:06
|
| This makes sense.My output would only be a report (I don't create any other tables)PMW, Patricia, MomLJB, Linda, MomCPW, Chris, DadI will give this a try to see what happens.Thanks,Patricia |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-29 : 03:25:51
|
| [code]select StudentID,studname,parentorg from @stud sLEFT JOIN @orgrelation o on s.org = o.orgchildtry this too with cte;with cte(StudentID,studname,org)as(select StudentID,studname,org from @studunion allselect studentid,studname,o.parentorgfrom cte cinner join@orgrelation o on c.org = o.orgchild)select StudentID,studname,orgfrom (select *,row_number()over(partition by studname order by org) as rid from cte )s where rid = 1[/code] |
 |
|
|
|
|
|
|
|