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.
Related
I have the Following Output:
Sno
Value Stream
Duration
Inspection
1
Test1
3
1
2
ON
14
0
3
Start
5
0
4
Test1
5
1
5
OFF
0
1
6
Start
0
1
7
Test2
0
1
8
ON
3
1
9
START
0
1
10
Test2
2
2
I want to merge the same value after that before START values charge to after ON. For example S.no 4 will merge to s.no4.
1 | Test1 | 8 | 2 |
If the combination is not equal then don't allow it to merge. For Example, we have to consider only On/Start. If the condition is OFF/Start then don't allow to merge. E.g. S.no 5 and 6 OFF/Start then don't allow to merge s.no 4 & 7.
I think you are talking about summarization not merging:
select [Value Stream],
min(Sno) as First_Sno,
sum(Duration) as total_Duration,
sum(Inspection) as Inspection
from yourtable
group by [Value Stream]
Will give you the result
This question already has answers here:
one to one distinct restriction on selection
(2 answers)
Closed 8 years ago.
I have encountered a problem like that. There is a Table A, I want to aggregate it using a `group by x order by diff(which is abs(x-y)) incrementally
x and y goes always incrementally. And x with smaller value will have the priority when two different x can paired with same y
x y diff
1 2 1
1 4 3
1 6 5
3 2 1
3 4 1
3 6 3
4.5 2 3.5
4.5 4 0.5
4.5 6 1.5
The aggregate function I want is:
take the y in each group which has the smallest difference with x(smallest diff value).
BUT that y which is taken can not be reused.(for example y=2 will be taken in (x=1) group so that can not be reused in (x=3) group)
Expected result:
x y diff
1 2 1
3 4 1
4.5 4 0.5
seems to be very tricky in plain SQL. I am using PostgreSQL. The real data will be much
complicated and longer than this idea-shooting example
If properly understood your question
test=# select * from A;
x | y | diff
---+---+------
1 | 2 | 1
1 | 4 | 3
1 | 6 | 5
3 | 2 | 1
3 | 4 | 1
3 | 6 | 3
5 | 2 | 3
5 | 4 | 1
5 | 6 | 1
(9 rows)
test=# SELECT MIN(x) AS x, y FROM A WHERE diff = 1 GROUP BY y ORDER BY x;
x | y
---+---
1 | 2
3 | 4
5 | 6
(3 rows)
SELECT MIN(x) AS x, y, MIN(diff) FROM A WHERE diff = 1 GROUP BY y ORDER BY x;
x | y | min
---+---+-----
1 | 2 | 1
3 | 4 | 1
5 | 6 | 1
(3 rows)
added MIN(diff) if not needed can be removed.
Try like this
t1 as table name
d as diff
with cte as (
select x, y,d from t1 where d=(select min(d) from t1) order by x )
select t1.x, min(t1.y), min(t1.d) from t1 inner join cte on
t1.x=cte.x and not t1.y in (select y from cte where cte.x<t1.x)
group by t1.x
This is more of a comment.
This problem essentially a graph problem, of finding the shortest set pairs between two discrete sets (x and y in this case). Technically, this is a maximum matching of a weighted bipartite graph (see [here][1]). I don't think this problem is NP-complete. But that still can make it hard to solve particularly in SQL.
Regardless of whether or not it is hard in the theoretical sense (NP-complete is considered "hard theoretically"), it is hard to do in SQL. One issue is that greedy algorithms don't work. The same "y" value might be closest to all the X values. Which one to choose? Well, the algorithm has to look further.
The only way that I can think to do this accurate in SQL is an exhaustive approach. That is, generate all possible combinations and then check for the one that meets your conditions. Finding all possible combinations requires generating N-factorial combinations of the X's (or Y's). That, in turn, requires a lot of computation. My first thought would be to use recursive CTEs for this. However, that would only work on small problems.
How do you check if the content of a field is a multiple of 2 in oracle sql
n_id | n_content
-------------------------
1 | Balloon
2 | Drill
3 | Cup
4 | Bottle
5 | Pencil
6 | Ball
I have tried:
select*from num_w where (n_id%2>0);
and also
select*from num_w where n_id%2=0;
Neither of these worked
Wasn't this multiple of 5 a few seconds ago? :)
Anyway, in Oracle I believe the query would be:
select * from num_w where MOD(n_id,2) = 0;
Does there exist a Postgres Aggregator such that, when used on the following table:
id | value
----+-----------
1 | 1
2 | 1
3 | 2
4 | 2
5 | 3
6 | 3
7 | 3
8 | 4
9 | 4
10 | 5
in a query such as:
select agg_function(4,value) from mytable where id>5
will return
agg_function
--------------
t
(a boolean true result) because a row or rows with value=4 were selected?
In other words, one argument specifies the value you are looking for, the other argument takes the column specifier, and it returns true if the column value was equal to the specified value for one or more rows?
I have successfully created an aggregate to do just that, but I'm wondering if I have just re-created the wheel...
select sum(case when value = 4 then 1 else 0 end) > 0
from mytable
where id > 5
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.