Multiple parents hierarchy recursion - sql

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.

Related

Check constraint for multiple conditions

The teacher gave us a team assignment, and me and my teammate are quite struggling with it (especially since we need to use things like TRIGGERS and PROCEDURES, things we didn't see in class yet …).
We need to implement an arc-relationship, and we fail to understand how …
But before I tell you guys what I need to accomplish, I will give you part of the description of the task, so you guys can understand the situation a bit better …
We basically need to make an ERD for a VLSI CAD-system and we need to implement it. Now, we have our CELL entity, the attributes of which aren't really relevant … The only thing you guys need to know in order to help us is that it has a primary key, CELL_CODE, which is a VARCHAR.
Each CELL has many (I think at least four, I don't think you can have triangular CELLS, but doesn't matter anyways) SIDES. A SIDE can be logically identified by its CELL, and to make matters ridiculously difficult, each SIDE has to be numbered by its CELL, like so:
CELLS:
CELL_CODE
1
2
SIDES:
SEQUENCE_NUMBER CELL_CODE
1 1
2 1
3 1
1 2
2 2
3 2
Now, each SIDE has its CONNECTION_PINS. CONNECTION_PINS is also uniquely identified by SIDES, which are basically numbered in a similar manner:
CELLS:
CELL_CODE
1
2
SIDES:
SEQUENCE_NUMBER CELL_CODE
1 1
2 1
3 1
1 2
2 2
3 2
CONNECTION_PINS:
SEQUENCE_NUMBER SIE_SEQUENCE_NUMBER CELL_CODE
1 1 1
2 1 1
1 2 1
2 2 1
1 3 1
2 3 1
1 1 2
2 1 2
1 2 2
2 2 2
1 3 2
2 3 2
I tried to explain the numbering issue we have here: Data model - PRIMARY KEY numbering issue, but yeah, I didn't really explain it the way it should be explained ...
Now, we have one final entity, which is where the Arc comes in: CONNECTIONS. CONNECTIONS has 2 CONNECTION_PINS: one for START_FROMand one for END_OF. Now, logically seen the start pin can't be the end pin as well, for a given connection. And that's our struggle. Basically, this shouldn't be allowed:
CELLS:
CELL_CODE
1
2
SIDES:
SEQUENCE_NUMBER CELL_CODE
1 1
2 1
3 1
1 2
2 2
3 2
CONNECTION_PINS:
SEQUENCE_NUMBER SIE_SEQUENCE_NUMBER CELL_CODE
1 1 1
2 1 1
1 2 1
2 2 1
1 3 1
2 3 1
1 1 2
2 1 2
1 2 2
2 2 2
1 3 2
2 3 2
CONNECTIONS:
(you shouldn't be able to put this in …)
CPI_SEQNUM_START SIE_SEQNUM_START CELL_CODE_START CPI_SEQNUM_END SIE_SEQNUM_END CELL_CODE_END
1 1 1 1 1 1
Now, this is basically the ERD for this part:
ERD with barred relationships and the arc-relationship in question
and this is the physical model:
Physical model
I basically thought a simple CHECK might do (CHECK (CPI_SEQNUM_START <> CPI_SEQNUM_END AND CELL_CODE_START <> CELL_CODE_END AND SIE_SEQNUM_START <> SIE_SEQNUM_END) ), but that prevented us from inserting anything somehow … Any advice?
Your approach was correct to use a CHECK constraint. Your logic for the constraint was wrong though. You need an OR condition. Only one of the three fields needs to be different.
CPI_SEQNUM_START <> CPI_SEQNUM_END OR
CELL_CODE_START <> CELL_CODE_END OR
SIE_SEQNUM_START <> SIE_SEQNUM
... assuming all three fields are not nullable.

SQL table structure for store value against list of combination

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.

How to fit multiple equal references into table structure?

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.

Returning all children with a recursive select

Good day everyone! I've got a graph. First, I know how to build simple recursive selections. I read some info on msdn.
In this image you can see that (for example) the top node of the graph, which is numbered 0, influences node number 1 (etc (2->4), (3->4), (4->5), (5->6), (1->5))
TASK: for every node show nodes which it influences. For example,
number 1 influences 5 and 6.
The result SQL must return something like this:
who_acts| on_whom_influence
0 | 1
0 | 5
0 | 6
1 | 5
1 | 6
2 | 4
2 | 5
2 | 6
3 | 4
3 | 5
3 | 6
4 | 5
4 | 6
5 | 6
Starting data that I can get using anchor member of CTE are:
who_acts| on_whom_influence
2 | 4
3 | 4
4 | 5
5 | 6
1 | 5
0 | 1
Can I make this selection using SQL syntax and a recursive select? How can I do it?
That sounds like a straightforward CTE. You can pass along the root of the influence in a separate column:
; with Influence as
(
select who_acts
, on_whom_influence
, who_acts as root
from dbo.YourTable
union all
select child.who_acts
, child.on_whom_influence
, parent.root
from Influence parent
join dbo.YourTable child
on parent.on_whom_influence = child.who_acts
)
select root
, on_whom_influence
from Influence
order by
root
, on_whom_influence
Example on SQL Fiddle.

Pairwise testing: How to create the table?

Hello I have doubt regarding how to create the table for the pairwise testing.
For example if I have three parameter which can each attain two different values. How do I create a table of input with all possible combination then? Would it look something like this?
| 1 2 3
-----------
1 | 1 1 1
2 | 1 2 2
3 | 1 1 2
4 | 1 2 1
Does each parameter corresponds to each column?
However since I have 3 parameter, which each can take 2 different value. The number of test cases should be 2^3 isn't it?
There's a good article with links to some useful tools here:
http://blog.josephwilk.net/ruby/pairwise-testing-with-cucumber.html
For the parameters: each column is a parameter, and each row is a possible combination. Here is the table:
| 1 2 3
-----------
1 | 1 1 1
2 | 2 1 1
3 | 1 2 1
4 | 1 1 2
5 | 2 2 1
6 | 2 1 2
7 | 1 2 2
8 | 2 2 2
so 2^3=8 possible combinations as you can see :)
For the values: each column is a value, and each row is a possible combination:
| 1 2
--------
1 | 1 1
2 | 2 1
3 | 1 2
4 | 2 2
They are 2^2=4 possible combinations. Hope it helps.
1) Please note that pair-wise testing is not about scanning exhaustively all possible combination of values of all parameters. Firstly, such a scanning would give you an enormous amount of test cases that almost no existing system could be able to run all of them.
Secondly, pair-wise testing for a software system is based on the hope that the two parameters having the highest number of possible values are the culprit for the highest percentage of faults of that system.
This is of course only a hope and almost no rigorous scientific research has existed so far to prove that.
2) What I often see in the documentations discussing pair wise testing, like this is that the list of all possible values (aka the pair-wise test table) is not constructed in a well thought way. This creates confusions.
In your case, all the parameters have the same number of possible values (2 values), therefore you could choose any two parameters of those three to build the table. What you could pay attention is the ordering of the combination: you iterate first the top-right parameter, then the next parameter to the left, and so on, ...
Say if you have two parameters p1 and p2, p1 has two possible values apple and orange; and p2 has two possible values red and blue, then your pair-wise test table would be:
index| p1 p2
------------------
1 | apple red
2 | apple blue
3 | orange red
4 | orange blue