473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Join a table to itself

82 64KB
Hi,

I have a table that stores varies entities. An entity can own one or more entieis. I also have a table for relationships that list the type of relationship and parent ID and child ID. Do I use a self join with entity table and the relationship table in my query to get results or is there a better approach? thanks.
Jan 3 '14 #1

✓ answered by Rabbit

You would join the entity table to the relationship table twice.

15 1584
Rabbit
12,516 Expert Mod 8TB
You would join the entity table to the relationship table twice.
Jan 3 '14 #2
barbarao
82 64KB
@Rabbit
That is what I thought. Just wanted to see if someone agreed with my approach. Thanks as usual and happy new year.
Jan 3 '14 #3
Rabbit
12,516 Expert Mod 8TB
No problem. Happy new year to you too!
Jan 3 '14 #4
barbarao
82 64KB
Hi. Sorry for the delay (death in family). I did what you suggested but having a problem. I have Entities named Caesar, Kathy, susan, Michael, and Blake. Caesar and Kathy are both at the top of the food chain. They are ultimate owners if you will. Caesar owns Susan who owns Blake and Blake owns Michael. Kathy owns Pam. Caesar also guides Button but does not own Button. The only way that I can tell all that is by carefully studying the query results which would take forever with the amount of records in the Entity Table. Any thoughts on what I could do? I know I am stressed out so maybe I am just overlooking something. Thanks in advance.
Jan 10 '14 #5
Rabbit
12,516 Expert Mod 8TB
If you are looking to store a hierarchical structure and want to be able to retrieve the hierarchical tree, then you need to use a different structure.

If there can ever only be one parent node, i.e. one owner or one guide and never more than one owner/guide in any combination, then I suggest the modified preorder tree traversal structure.

You can read the particular details about it from this article: http://www.sitepoint.com/hierarchical-data-database-2/
Jan 10 '14 #6
barbarao
82 64KB
@Rabbit
Thanks for the link. I will read it now. There can be only one ultimate owner, a next lever owner, one level after that, and probably one more. There are some entities related to things that are not owners and these may or may not have an ultimate ownder.
Jan 10 '14 #7
Rabbit
12,516 Expert Mod 8TB
That's an issue then, the strucure I linked only works where each node only has one parent node. It sounds like one node may have one owner parent node and another non-owner parent node.

If this is true, then I can't think of an easy structure that you can use. You would have to do some sort of recursion to be able to pull the entire structure and that's not something you would be able to do easily in SQL if at all.
Jan 10 '14 #8
barbarao
82 64KB
Thanks again. The only thing I can think of would be in the relationship table have an OWN1, OWN, OWN3 assigned to each entity then add a new field for Ultimate and enter the ultimate owner there. I'll keep research. Thank again.
Jan 10 '14 #9
barbarao
82 64KB
Hi. Just wanted to close the loop in case anyone finds themselves in this situation. Found a model, forgot what it is called, that offered the idea of a "depth" field to enter the level of who own what. For example, entity Caesar has an ID of 10. He is the highest. Susan reports to him and her ID is 2 so her depth is 10 2. Blake reports to Suan and his ID is 7 so his depth is 10 2 7. Gives me the ability to see an entire "family" for lack of a better word just by sorting by ID field. thanks again.
Jan 15 '14 #10
Rabbit
12,516 Expert Mod 8TB
How does the depth field represent a node that has an owner node and a guide node?
Jan 15 '14 #11
barbarao
82 64KB
@Rabbit
I thought that the 10 2 7 for the company "Blake" would indicated that he is 7, his immediate owner is 2, and the owner of 2 is 10. You don't think that would work? Guess I don't understand the node concept. Thanks in advance.
Jan 16 '14 #12
Rabbit
12,516 Expert Mod 8TB
That shows one person reporting to one higher person. From your earlier statements, it's not always one to one. You said that they can have both an owner and a guide, which means one person reporting to two people one level higher.

Your 10.2.7 would be represented graphically like this:
Expand|Select|Wrap|Line Numbers
  1. 10
  2.  |
  3.  2
  4.  |
  5.  7
  6.  
Whereas what you described earlier is more like this:
Expand|Select|Wrap|Line Numbers
  1. 11  3
  2.  \ /
  3.   8
  4.  
Jan 16 '14 #13
barbarao
82 64KB
yes but each guide would be a would be a differnt depth path and they would not have OWN aa a relationship type so i think this is the best I can come up with. Buidling a solution while mourning my only brother and supporting an 87 years old mother who lives 900 miles away isn't easy. This request came at an awful time for clear thinking so your help has been more than appreciated.
Jan 16 '14 #14
Rabbit
12,516 Expert Mod 8TB
I'm sorry for your loss.

The relationship type doesn't matter to the preorder tree structure, only that there is one and only one parent node at the next level up. If this is true, then I hold that my original suggestion would be the best structure to use. It allows you to insert nodes at any point with less work than it would take to do in any other paradigm, including the depth field you want to use. It also allows for easy retrieval, deletion of nodes, and movement of nodes. Each would only require a couple of queries that can be automated where as the depth field would require extensive manual editing to do additions, deletions, and movements.

If, however, your structure is static and you never have to move, delete, or add additional nodes, then the depth field is workable.
Jan 16 '14 #15
barbarao
82 64KB
Thanks. It is a hard loss for me as he was also my best friend throughout our lives.

Entity Susan, for example, who is owned by Entity Caesar, may have 4 guides as well. Entity Blake who is owned by Entity Susan, may have the same guides, different guides, or no guides at all. Entity Caesar, and all the other Entities like that one who are at the top of their family tree are not owned by another Entity nor do they have guides.

Entity Trevor may be self owned and have no or many guides.

I need to report not only on the full family tree but also generate reports on each type of guide.

Ownerships do not change often if at all.

I read through your article many times and sketched it out and just can't see how it will save time in building the solution or running any reports but I will read it again. Perhaps this time it will dawn on me but till then, I am going to keep the depth field in mind as a potential answer.

As usual, thanks for your help and suggestions. You have always come through for me on this site.
Jan 17 '14 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Xenophobe | last post by:
I have a query with a simple JOIN. For example: SELECT * FROM Companies INNER JOIN CompanyTypes ON Companies.CompanyID = CompanyTypes.CompanyID The query returns all fields from both...
1
by: Keith Simeon | last post by:
Hello all, I've recently started playing with Hibernate, and have a question regarding many-to-many relationships using a join or cross table (I've also heard these referred to as a transition...
2
by: bodleyhenley | last post by:
Please help me with this! :) I have a database of servers, one of the things I am recording information about is what software is on each server. Obviously this is a many to many so I have a...
6
by: davegb | last post by:
I'm trying to create a self-join table to show the relationship between employee and supervisor. In another thread, I was advised to create a SupervisorID in the employee table, a separate...
1
by: LL | last post by:
Hi, My sql join two table, how to call the fill method? thanks. string sqlStr = "select * from table1, table2"; SqlDataAdapter myCommand = new SqlDataAdapter(sqlStr, myConnection);DataSet...
3
by: CanFlightSim | last post by:
I am not very strong in databases, so you must forgive any obvious stupidity. After having a good look at our Sales Managemend database, we decided there is something basially wrong. Our Agents...
3
by: ssims | last post by:
I've been thinking about this problem for awhile, and can't seem to come up with a valid answer. I've got two tables, Surveys and Instructors. When a survey is completed there are one or two...
9
by: koehlerc14 | last post by:
I'm trying to create a form that imports a text file and join zip code fields to our "Master" database x2. I import the txt file and name it based on a "strTableName" which includes the date, a...
6
by: BD | last post by:
Hi, all. I need to enforce a one-to-many relationship on 2 tables, with a join table. Say the join table contains account information. It has cust_no and acct_no. Both cust_no and acct_no are...
0
by: Todd Nichols | last post by:
I have the main form for the "one" side of the relationship and a subform for the "many" side, linked by the primary key of the main table to the foreign key of the join table (the many-side table). ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.