Recursive SQL and information on different level - sql

Is it possible to display, in the same query, information concerning different level of recursivity ?
select LEVEL, ae2.CAB, ae2.NIVEAU, ae2.ENTITE, ae2.ENTITE_PARENT, ae2.libelle
from my_table ae2
where ae2.NIVEAU = 2
start with ae2.cab = 'XXX'
connect by prior ae2.entite_parent = ae2.entite
With this query I have (let's say) 4 levels of information about the entity above root 'XXX'
Is it possible to display root information at the same time?

Yes, it's possible to use the CONNECT_BY_ROOT operator. For instance, if you wanted the cab of the parent your query would be:
select connect_by_root cab
, level, cab, niveau, entite, entite_parent, libelle
from my_table
where niveau = 2
start with cab = 'XXX'
connect by prior entite_parent = entite
You have to use a new operator for each column you want to select. You won't get information from a "different" level of recursivity using this operator, only from the root. If you want more you'll have to use recursive subquery factoring.

Related

Hierarchical Connect By Pass Value to children

I have a hierarchical SQL Statement, which show me a hierarchical list of components of a product. For example: Part 1101400004 contains Part 1012444. And Part 1012444 contains B30048. For each component i have a Qty.
Now my question is: is it possible, to pass a value to the children?
So when part 1101400004 has QTY 0, no matter what QTY Part 1012444 has, it should be 0 because the parent part has QTY zero. And this logic to the bottom of the tree.
select part_no, component_part, qty_per_assembly
FROM STRUCTURE MS
CONNECT BY PRIOR MS.COMPONENT_PART = MS.PART_NO
START WITH MS.PART_NO = '1101400004'
Result
Thx for help
From Oracle version 10g you can use CONNECT_BY_ROOT pseudocolumn like this:
select part_no, component_part, connect_by_root qty_per_assembly
FROM STRUCTURE MS
CONNECT BY PRIOR MS.COMPONENT_PART = MS.PART_NO
START WITH MS.PART_NO = '1101400004'
See https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm#i2069380
As I understand you need to get quantity from qty_per_assembly if all prents not equal "0" and "0" if at least one predecessor is equal "0". For solve it you may use combintion of connect_by_root(component_part) and analytic MIN. Please correct me if I wrong
select part_no, component_part, DECODE(MIN(qty_per_assembly) OVER (PARTITION BY connect_by_root(component_part) ORDER BY level)
, 0
, 0
, qty_per_assembly)
FROM STRUCTURE MS
CONNECT BY PRIOR MS.COMPONENT_PART = MS.PART_NO
START WITH MS.PART_NO = '1101400004'
EDIT: connect_by_root(qty_per_assembly) change to connect_by_root(component_part)

SQL Modeling Pyramid or Binary Tree

I'm building a sql server project that have a pyramid or binary tree concept...
I gonna try to explain using some tables!
The first table is
TB_USER(ID, ID_FATHER, LEFT/RIGHT TREE POSITION)
User can sell producs! So when they sell they earn points. Then, the second table is
TB_SELL (ID_USER, ID_PRODUCT, POINT)
As a result I'd like to see in the report format of points of each client below me in the binary model tree. How can I design these tables to make my life easier in this kind of search ? I will always get my soons up to 9 levels down.
I know that with procedure I can solve this problem , however I would like to know an elegant and simple solution.
Thank you
I solve this using a with a recursive query:
with with_user_earns as (
-- get father information (start)
select father.id, father.str_name, father.id_father, father.ind_father_side_type, 1 as int_user_level from tb_user father where id = 9
union all
-- get all soons (stop condition)
select son.id, son.str_name, son.id_father, son.ind_father_side_type, WUE.int_user_level + 1 from tb_user as son inner join with_user_earns as WUE on son.id_father = WUE.id where son.id_father is not null /*and WUE.int_user_level < 9*/
)
-- show result
select with_user_earns.id, with_user_earns.str_name, with_user_earns.id_father, with_user_earns.ind_father_side_type, with_user_earns.int_user_level from with_user_earns order by with_user_earns.int_user_level, with_user_earns.id

Undirected graph in Oracle SQL

I'm trying to represent undirected graph in Oracle SQL, for example, I have stations graph:
CREATE TABLE station (
station_id INTEGER NOT NULL
);
CREATE TABLE station_link (
from_station INTEGER NOT NULL,
to_station INTEGER NOT NULL
);
This is obviously directed graph, but I have no idea, how to make it undirected.
Point: I need to get all vertices, which have path to current vertex and information about their level (how many vertices on this path).
For directed graph it is pretty easy:
SELECT sl.to_station, LEVEL
FROM station_link sl
START WITH sl.from_station = :curVertex
CONNECT BY NOCYCLE PRIOR sl.to_station = sl.from_station
But so we will get only one-way verticies.
Question: Do this problem have solution, except adding additional links (2 -> 1 for 1 -> 2)?
There is sql fiddle for tests: http://sqlfiddle.com/#!4/6c09e/24
For a "fast win" you can use your structure "as is", but for every edge you shold have two records in station_link table.
If you want "not_so_fast_but_without_doble_edge_records_please win", you can use this weirdo-trick:
SELECT
x.TO_STATION,
x.LVL
FROM (
SELECT sl.to_station, LEVEL as lvl
FROM station_link sl
START WITH sl.from_station = :curVertex
CONNECT BY NOCYCLE PRIOR sl.to_station = sl.from_station
UNION ALL
SELECT sl.from_station as to_station , LEVEL as lvl
FROM station_link sl
START WITH sl.to_station = :curVertex
CONNECT BY NOCYCLE PRIOR sl.from_station = sl.to_station
) x
It will do the work. Actually, it just combines two traversal directions.
But if I'll want to implement some serious algorithms on graphs in PLSQL, I would look to SDO_GEOMETRY data type and Oracle Spatial And Graphs Datasheets.

Recursive cte sql with for hierarchy level

I have a little problem with this recursive CTE, it works fine except when I have a user without root readable rights means no entry for this element. So if I run this query on a user with rights just on the leaves inside the tree the level part of this query won't work correctly.
It will show the real level hierarchy for example 6 but its the top first readable element for him so it should be 1.
WITH Tree
AS (
SELECT
id,
parent,
0 AS Level,
id AS Root,
CAST(id AS VARCHAR(MAX)) AS Sort,
user_id
FROM SourceTable
WHERE parent IS NULL
UNION ALL
SELECT
st.id,
st.parent,
Level + 1 AS Level,
st.parent AS Root,
uh.sort + '/' + CAST(st.id AS VARCHAR(20)) AS Sort,
st.user_id
FROM SourceTable AS st
JOIN Tree uh ON uh.id = st.parent
)
SELECT * FROM Tree AS t
JOIN UserTable AS ut ON ut.id = t.user_id AND ut.user_id = '141F-4BC6-8934'
ORDER BY Sort
the level is as follows
id level
5 0
2 1
7 2
4 2
1 2
6 1
3 2
8 2
9 3
When a user now just have read rights to id 8 and 9 the level from CTE stays at 2 for id 8 and 3 for id 9 but I need for id 8 level 1 if there is no one before
You haven't told us how you know whether a user has rights to a given id. That is a necessary piece of information. I'm going to put some code below that assumes you add a column to your query called hasRights and that this column will have a zero value if the user does not have rights and a value of one if they do. You may need to tweak this, since I have no data to test with but hopefully it will get you close.
Basically, the query is altered to only add 1 to the level if the user has rights. It also only adds to the sort path if the user has rights, otherwise an empty string is appended. So, if ids 8 and 9 are the only items the user has access to, you should see levels of 1 and 2 and sort paths similar to '5/8/9' rather than '5/6/8/9'. If you still aren't able to get it working, it would help us tremendously if you posted a sample schema on SqlFiddle.
WITH Tree
AS (
SELECT
id,
parent,
0 AS Level,
id AS Root,
hasRights AS HasRights,
CAST(id AS VARCHAR(MAX)) AS Sort,
user_id
FROM SourceTable
WHERE parent IS NULL
UNION ALL
SELECT
st.id,
st.parent,
Level + st.hasRights AS Level,
st.parent AS Root,
st.hasRights AS HasRights,
uh.sort + CASE st.hasRights WHEN 0 THEN '' ELSE '/' + CAST(st.id AS VARCHAR(20)) END AS Sort,
st.user_id
FROM SourceTable AS st
JOIN Tree uh ON uh.id = st.parent
)
SELECT * FROM Tree AS t
JOIN UserTable AS ut ON ut.id = t.user_id AND ut.user_id = '141F-4BC6-8934'
ORDER BY Sort
You requite something like if the higher level(0 or 1 ) is not in existence, then the next level become the higher level..
If yes, then you have to do this when the final result
insert all the results in temp table lets say #info (with same characteristics of data)
Now after all final data ready in the table,
Please check from the top.
Select * from #info where level= 0
if this returns 0 rows then you have to update each records level. to (level = level -1)
Now again same for Level=0, then level 1, then level 2 , then level 3 in recursion. this will be easy but not easy to code. So try without recursion then try final update.
I hope this will help :)
Please reply if you are looking for something else.
Try to perform the following select and let me know if it is your desired result:
SELECT *,
DENSE_RANK() OVER (PARTITION BY t.user_id ORDER BY t.LEVEL ASC) -1 as RelativeUserLevel
FROM Tree AS t
JOIN UserTable AS ut ON ut.id = t.user_id AND ut.user_id = '141F-4BC6-8934'
ORDER BY Sort
Is conversion of your table into hierarchical types an option:
hierarchyid data type (http://technet.microsoft.com/en-us/library/bb677290(v=sql.105).aspx)
or
xml data type
SOME TIMES option (maxrecursion 10000); is very useful
I don't have time to read your problem but here snipped
declare cursorSplit Cursor for
select String from dbo.SplitN(#OpenText,'~')
where String not in (SELECT [tagCloudStopWordText]
FROM [tagCloudStopList] where [langID]=#_langID)
option (maxrecursion 10000);
open cursorSplit
I'm sorry to be a party pooper and spoil the fun of creating such an interesting piece of SQL, but perhaps you should load all relevant access data into your application and determine the user levels in the application?
I bet it would result in more maintainable code..

T-SQL Count child node in Binary Tree?

I made a table to store a Binary Tree like below:
- NodeID
- NodeLeft
- NodeRight
NodeLeft store the ID of the left node. And Node right store the ID of the right node.
I need to write a Procedure that if i pass a NodeID, it'll count how many child node on the left and how many child node on the right. Can separate to 2 Procedure.
Try this:
WITH CTE_Node(
NodeID,
NodeRigth,
NodeLeft,
Level,
RigthOrLeft
)
AS
(
SELECT
NodeID,
NodeRigth,
NodeLeft,
0 AS Level,
'P'
FROM Node
WHERE NodeID = 1
UNION ALL
SELECT
Node.NodeID,
Node.NodeRigth,
Node.NodeLeft,
Level + 1,
CASE WHEN CTE_Node.NodeLeft = Node.NodeID THEN 'R' ELSE 'L' END
FROM Node
INNER JOIN CTE_Node ON CTE_Node.NodeLeft = Node.NodeID
OR CTE_Node.NodeRigth = Node.NodeID
)
SELECT DISTINCT RigthOrLeft,
COUNT(NodeID) OVER(PARTITION BY RigthOrLeft)
FROM CTE_Node
Here is an SQL Fiddle.
The Level is just there to see how is it working. May you can use it later.
I found this topic.
http://www.sqlservercentral.com/Forums/Topic1152543-392-1.aspx
The table structure is different from my designed table. But it is an Binary Tree so i can use it.
And the SQL Fiddle is very helpful.