I have a table that contains parent child relationships and I need to find all related issues.
Parent
Child
1
3
1
4
2
4
5
6
7
6
I need to be able to identify all related issues, ideally I could return a new table that returns the following.
Issue
Group
1
1
2
1
3
1
4
1
5
2
6
2
7
2
I'm currently using CTEs to try and solve this, but I'm running into circular reference issues and I'm not sure how to create a group number for each. Essentially, it is just tracing up and down different parents and each group does not have one central parent node which I think may be causing the issues.
Related
I have a dataset with two columns of non-unique IDs (ID-A and ID-B respectively).
A single ID-A can have multiple ID-Bs and vice versa. I am trying to generate a third, set identifier using transitivity (calling it ID-C) which is set to the same value for all records with either ID-A or ID-B in common. Two records having neither ID-A nor ID-B in common should only share an ID-C set identifier if there is a transitive chain of records between them.
To visualize, I have something like the first two columns, and want to generate the third column(ID-C)
ID-A ID-B ID-C
1 1 1
1 2 1
1 3 1
2 2 1
2 4 1
3 4 1
4 5 2
5 5 2
5 6 2
6 7 3
I am using Presto SQL inside of AWS Athena, so I cannot use any variables or loops, that I am aware of.
I have a requirement from client where I need to store a value against list of combination.
For example I have following LOBs and against each combination I need to store a value.
Auto
WC
Personal
I purposed multiple solutions he is not satisfied with anyone.
Solution 1: create single table, insert value against all possible combination(string) something like
LOB Value
Auto 1
WC 2
Personal 3
Auto,WC 4
Auto, personal 5
WC, Personal 6
Auto, WC, Personal 7
Solution 2: create lkp_lob, lob_group and lob_group_detail tables. Each group combination represent a group.
Lkp_lob
Lob_key Name
1 Auto
2 WC
3 Person
Lob_group (unique query constrain on lob_group_key and lob_key)
Lob_group_key Lob_key
1 1
2 2
3 3
4 1
4 2
5 1
5 3
6 2
6 3
7 1
7 2
7 3
Lob_group_detail
Lob_group_key Value
1 1
2 2
3 3
4 4
5 5
6 6
7 7
Any suggestion would be highly appreciated.
First of all I did not understood that terms you said.
But from database perspective it is always good to have multiple tables for each module. You will be facing less difficulties when doing CRUD. And will be more faster.
I have a sheet formated somewhat like this
Thing 5 6 7 Person 1 Person 2 Person 3
Thing 1 1 2 7 7 6
Thing 2 5 5
Thing 3 7 6 6
Thing 4 6 6 5
I am trying to find a query formula that I can place in the columns labeled 5,6,7 that will count the number of people who have that amount of Thing 1. For example, I filled out the Thing 1 row, showing that 1 person has 6 of Thing 1 and 2 people have 7 of Thing 1.
You can use this function: "COUNTIF".
The formula to write in the cells will look like this:
=COUNTIF(E2:G2;"=5")
For more information regarding this function, check the documentation: https://support.google.com/docs/answer/3093480?hl=en
How to fit multiple equal references into table structure? How could I do that? For example: I have list of classmates:
1 Peter
2 Jack
3 John
4 Mary
5 Birgit
6 Stella
7 Janus
8 Margo
9 Fred
Now I want to define fellowships. In first place, let's limit that every kid may belong to one fellowship. So we could have 3 fellowships:
[Peter, Jack]
[John, Mary, Birgit]
[Stella, Janus, Margo, Fred]
All members are equal, so they all should reference to other members. Is there better ways to define such relations than just to have table of pairs? Like:
1 2
3 4
3 5
4 5
4 3
5 3
5 4
6 7
6 8
6 9
7 6
7 8
7 9
8 6
8 7
8 9
9 6
9 7
9 8
If using table of pairs, is it better to describe relation both way (like above), or is it enough to have link just from one way to another? What are the benefits of both ways?
Table of pairs does not constrain any member into just one fellowsip, but how would it possible?
I was looking for SQL table solution, but maybe there are better tools for handling such data-structures, so I added nosql-tag too. I am looking for right tools for such data, but I am eager to know, how to fit it in SQL tables too.
Yes, there is another way. If you have "fellowships", then you do not have pair-wise relationships. STart with a Fellowships table that has a FellowshipsId.
Then you would have a FellowshipsKids table. This is called a junction table, and it would have one row for each member of each fellowship. It would have rows like this:
FellowshipId KidId
1 1
1 2
2 3
2 4
2 5
. . .
What you have is an m-n relationship between fellowships and kids -- one fellowship can have multiple kids, one kids can be in multiple fellowships. A junction table is the standard way of represent this in a relational database.
Master Table
===========
ID NAME
1 A
2 B
3 C
4 D
5 E
Hierarchy table with multiple parents (Note that neither can be primary column due to duplicate values):
Relations Table
================
ChildID ParentID
3 1
3 2
4 3
4 2
5 4
Hierarchy becomes like (it might not be this linear always):
1 2
| |
3 3
| |
4 4
| |
5 5
For reporting purpose I need data in recursive hierarchy format so that I can drill down it. I'm not getting if I can get drill down feature from existing data itself (seems not doable as I can not create recursive parent-child relationship due to duplicate values).
Do you have any ideas? My goal is to finally use this structure as a dimension in SSAS which automatically gives drill down if a table has a self primary key-child key relationship.
Using your example data, I actually get a different tree...
Relations Table Tree
================ =======
ChildID ParentID 1 2
3 1 \ /|
3 2 3 |
4 3 \|
4 2 4
5 4 |
5
Do you actually want two independent trees? If that's the case, you could introduce an extra field such as a tree id...
Relations Table Tree1 Tree2
======================= ===== =====
TreeID ParentID ChildID
1 NULL 1 1 2
1 1 3 | |
1 3 4 3 3
1 4 5 | |
2 NULL 2 4 4
2 2 3 | |
2 3 4 5 5
2 4 5
Without some extra piece of information, you'll always have problems of branches splitting and merging without a very well formed set of constraints. For example, if you wanted two linear trees of 1-3-4-5 and 2-3-4-6, your current model would have this...
Relations Table Tree
================ =======
ParentID ChildID 1 2
1 3 \ /
2 3 3
3 4 |
4 5 4
4 6 / \
5 6
The problem you now have though, is that there are FOUR linear paths...
- 1-3-4-5
- 1-3-4-6
- 2-3-4-5
- 2-3-4-6
What may be required is for you to describe a real world situation, exactly what you want from it, and exactly what you don't want from it.
My typical experience is that, for reporting purposes, any node in a tree should only have one parent, but may have many children. This means that when climbing up a tree you only have one route, and when climbing down a tree the data separates into sub-nodes.
Having many parents and many children makes a web rather than a tree. Where you have multiple routes, no matter which direction you traverse the tree.