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 |
|
kzielinski
Starting Member
2 Posts |
Posted - 2006-03-12 : 19:48:26
|
| I am trying to get a query to return a record set where there is aparent - child relationship between some of the columns, such thatthere will be several child records for a parent.Runtime properties of the dataset:Queires will retrive 0 - 5000 parent records (largest known case is about 2700 results) There will 99% of the time there will be be 2 - 5 child records. however larger numbers are possilbe (largest know count being 150 child records).The hiarchy is a flat 2 level hiarchy, there is no overlap betweenparent and child records (no posiblity of recusion).My question is which is more efficent1) using JOIN and returning the parent data muyltiple times (once for each child).2) Using a UNION of two quries one for the parent records and one for the child records. Somewhat along the lines of the universal table fromat employed in FOR XML EXPLICIT quries. This wayParent records have the child columns set to NULL and child recordshave the Parent Columns set to null (except for a key which identifies the parent)3) Having the calling code execute a seperate querie for each parent to retrive the chidren. This is the current approach which i suspect is the least optimal (hence my desire to change it).regardsKonrad |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-03-12 : 20:07:42
|
| It would be easier to help if you posted the table structures, some sample data, and the output you are trying to get.CODO ERGO SUM |
 |
|
|
kzielinski
Starting Member
2 Posts |
Posted - 2006-03-12 : 21:36:21
|
| TThe Whole point of the excersise is that the calling code will be converting the query result into an XML document.The folling is a simplification of the table structure.In practive there are something like 10 items from the parent table and 4 itemsfrom the child table. There are also a number of addional omited tables which needto be joined to either the Parent table, or the Child Table, to get the actual desired value. ParentTable.IssueID, Subject, data2, data3 ... data10 ChildTableIssueID ParticipantName.what I want do do is get a result set which shows data from both tables. Note that in practice I will have something like 8 - 12 items for parent table and 4 items from the child table.1) do a join gettingissue ID, Subject ParticipantName011 subject1 name1011 subject1 name2011 subject1 name3012 subject2 name1 etc2) Use a union, where the first query returns Parent data and the 2nd returns childdata. As Noted earlier this is same appeoach which FOR XML EXPLICIT queries table. issueID Subject participantName011 Subject1 NULL011 NULL Name1011 NULL Name2011 NULL Name3012 Subject2 NULL012 NULL Name1The calling code will use the above record set to produce the following output:<issue-list> <Issue id="011"> <subject>Subject1</subject> <participants> <participant>Name1</participant> <participant>Name2</participant> <participant>Name3</participant> <participants> </issue> <issue id="012"> <subject>Subject2</subject> <participants> <participant>Name1</participant> ... </participants> </issue> ...</issue-list> |
 |
|
|
|
|
|
|
|