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)
Related
Hello friendly internet wizards.
I am attempting to extract a levelled bill of materials (BOM) from a dataset, running in DB2 on an AS400 server.
I have constructed most of the query (with a lot of help from online resources), and this is what I have so far;
#set item = '10984'
WITH BOM (origin, PMPRNO, PMMTNO, BOM_Level, BOM_Path, IsCycle, IsLeaf) AS
(SELECT CONNECT_BY_ROOT PMPRNO AS origin, PMPRNO, PMMTNO,
LEVEL AS BOM_Level,
SYS_CONNECT_BY_PATH(TRIM(PMMTNO), ' : ') BOM_Path,
CONNECT_BY_ISCYCLE IsCycle,
CONNECT_BY_ISLEAF IsLeaf
FROM MPDMAT
WHERE PMCONO = 405 AND PMFACI = 'M01' AND PMSTRT = 'STD'
START WITH PMPRNO = :item
CONNECT BY NOCYCLE PRIOR PMMTNO = PMPRNO)
SELECT 0 AS BOM_Level, '' AS BOM_Path, MMITNO AS Part_Number, MMITDS AS Part_Name,
IFSUNO AS Supplier_Number, IDSUNM AS Supplier_Name, IFSITE AS Supplier_Part_Number
FROM MITMAS
LEFT OUTER JOIN MITVEN ON MMCONO = IFCONO AND MMITNO = IFITNO AND IFSUNO <> 'ZGA'
LEFT OUTER JOIN CIDMAS ON MMCONO = IDCONO AND IDSUNO = IFSUNO
WHERE MMCONO = 405
AND MMITNO = :item
UNION ALL
SELECT BOM.BOM_Level, BOM_Path, BOM.PMMTNO AS Part_Number, MMITDS AS Part_Name,
IFSUNO AS Supplier_Number, IDSUNM AS Supplier_Name, IFSITE AS Supplier_Part_Number
FROM BOM
LEFT OUTER JOIN MITMAS ON MMCONO = 405 AND MMITNO = BOM.PMMTNO
LEFT OUTER JOIN MITVEN ON IFCONO = MMCONO AND IFITNO = MMITNO AND IFSUNO <> 'ZGA' AND MMMABU = '2'
LEFT OUTER JOIN CIDMAS ON MMCONO = IDCONO AND IDSUNO = IFSUNO
;
This is correctly extracting the components for a given item, as well as the sub-components (etc).
Current data looks like this (I have stripped out some columns that aren't relevant to the issue);
https://pastebin.com/LUnGKRqH
My issue is the order that the data is being presented in.
As you can see in the pastebin above, the first column is the 'level' of the component. This starts with the parent item at level 0, and can theoretically go down as far as 99 levels.
The path is also show there, so for example the second component 853021 tells us that it's a 2nd level component, the paths up to INST363 (shown later in the list as a 1st level), then up to the parent at level 0.
I would like for the output to show in path order (for lack of a better term).
Therefore, after level 0, it should be showing the first level 1 component, and then immediately be going into it's level 2 components and so on, until no further level is found. Then at that point, it returns back up the path to the next valid record.
I hope I have explained that adequately, but essentially the data should come out as;
Level
Path
Item
0
10984
1
: INST363
INST363
2
: INST363 : 853021
853021
1
: 21907
21907
Any help that can be provided would be very much appreciated!
Thanks,
This is an interesting query. Frankly I am surprised it works as well as it does since it is not structured the way I usually structure queries with a recursive CTE. The main issue is that while you have the Union in there, it does not appear to be within the CTE portion of the query.
When I write a recursive CTE, it is generally structured like this:
with cte as (
priming select
union all
secondary select)
select * from cte
So to get a BOM from an Item Master that looks something like:
CREATE TABLE item (
ItemNo Char(10) PRIMARY KEY,
Description Char(50));
INSERT INTO item
VALUES ('Item0', 'Root Item'),
('Item1a', 'Second Level Item'),
('Item1b', 'Another Second Level Item'),
('Item2a', 'Third Level Item');
and a linkage table like this:
CREATE TABLE linkage (
PItem Char(10),
CItem Char(10),
Quantity Dec(5,0),
PRIMARY KEY (PItem, CItem));
INSERT INTO linkage
VALUES ('Item0', 'Item1a', 2),
('Item0', 'Item1b', 3),
('Item1b', 'Item2a', 5)
The recursive CTE to list a BOM for 'Item0' looks like this:
WITH bom (Level, ItemNo, Description, Quantity)
AS (
-- Load BOM with root item
SELECT 0,
ItemNo,
Description,
1
FROM Item
WHERE ItemNo = 'Item0'
UNION ALL
-- Retrieve all child items
SELECT a.Level + 1,
b.CItem,
c.Description,
a.Quantity * b.Quantity
FROM bom a
join linkage b ON b.pitem = a.itemno
join item c ON c.itemno = b.citem)
-- Set the list order
SEARCH DEPTH FIRST BY itemno SET seq
-- List BOM
SELECT * FROM bom
ORDER BY seq
Here are my results:
LEVEL
ITEMNO
DESCRIPTION
QUANTITY
0
Item0
Root Item
1
1
Item1a
Second Level Item
2
1
Item1b
Another Second Level Item
3
2
Item2a
Third Level Item
15
Notice the search clause, that generates a column named seq which you can use to sort the output either depth first or breadth first. Depth first is what you want here.
NOTE: This isn't necessarily an optimum query since the description is in the CTE, and that increases the size of the CTE result set without really adding anything to it that couldn't be added in the final select. But it does make things a bit simpler since the 'priming query' retrieves the description.
Note also: the column list on the with clause following BOM. This is there to remove the confusion that DB2 had with the expected column list when the explicit column list was omitted. It is not always necessary, but if DB2 complains about an invalid column list, this will fix it.
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.
I am new to oracle and oracle ebs and I need some help.
I am doing a report in oracle ebs r12 and I need to list flex values from fnd_flex_values_vl view in a hierarchical way using a SQL query. It doesn`t necessary has to be a hierarchial query. Any query will do. I just need a SQL statement that will return me flex values in their hierarchial way.
There are two objects, that store information about flex values hierarchy. It is FND_FLEX_VALUE_NORM_HIERARCHY (table) and fnd_flex_value_children_v (view). I assume one of these is enough, since fnd_flex_value_children_v is made using FND_FLEX_VALUE_NORM_HIERARCHY and some other objects.
However, the problem I faced is, that several parents may be listed for one flex value and that I need to find all top parents or leaves in order to do an up-bottom or bottom-up hierarchy. As far as I understand fnd_flex_value_children_v doesn`t necessary store top parents (stores only children).
Also it seems that there is probably not one, but there may be multiple hierarchies (if so, I need to list them all in one query).
Your help will be really appreciated. I`ve been struggling with this one quite a while.
Thank you very much for your attention.
Best regards, new user. =)
You should use the table, APPLSYS.FND_FLEX_VALUE_SETS. The objects you identify are metadata objects about the FND_FLEX_VALUE_SETS table.
I like to start with the root record.
Here is my method to find root records (no parent).
SELECT DISTINCT
FVS.PARENT_FLEX_VALUE_SET_ID
FROM
APPLSYS.FND_FLEX_VALUE_SETS FVS
WHERE
FVS.PARENT_FLEX_VALUE_SET_ID IS NOT NULL
ORDER BY 1 ;
Once I find the root record, I develop my start by clause:
START WITH
(
FVS.FLEX_VALUE_SET_ID IN
(SELECT DISTINCT FVS.PARENT_FLEX_VALUE_SET_ID
FROM APPLSYS.FND_FLEX_VALUE_SETS FVS
WHERE FVS.PARENT_FLEX_VALUE_SET_ID IS NOT NULL
)
This clause does capture all root records (you could select just one).
Next, I develop my connect by clause. Since I want my hierarchy to start at the root, I would take this approach:
level 1 flex_value_set_id ....prior level
level 2 parent_flex_value_set_id
CONNECT BY fvs.parent_flex_value_set_id = prior fvs.flex_value_set_id ;
This results in this statement:
SELECT LEVEL,
FVS.*
FROM APPLSYS.FND_FLEX_VALUE_SETS FVS
START WITH
(
FVS.FLEX_VALUE_SET_ID IN
(SELECT DISTINCT FVS.PARENT_FLEX_VALUE_SET_ID
FROM APPLSYS.FND_FLEX_VALUE_SETS FVS
WHERE FVS.parent_flex_value_set_id IS NOT NULL
)
)
CONNECT BY FVS.PARENT_FLEX_VALUE_SET_ID = PRIOR FVS.FLEX_VALUE_SET_ID ;
One then can add the flex values as follows:
SELECT
LEVEL,
FVS.*
FROM
(SELECT
FLEX.FLEX_VALUE_SET_ID,
FLEX.PARENT_FLEX_VALUE_SET_ID,
FLEX.FLEX_VALUE_SET_NAME,
FVAL.FLEX_VALUE
FROM
APPLSYS.FND_FLEX_VALUE_SETS FLEX,
APPLSYS.FND_FLEX_VALUES FVAL
WHERE
FLEX.FLEX_VALUE_SET_ID = FVAL.FLEX_VALUE_SET_ID(+)) FVS
START WITH
(FVS.FLEX_VALUE_SET_ID IN
(SELECT DISTINCT
FVS.PARENT_FLEX_VALUE_SET_ID
FROM APPLSYS.FND_FLEX_VALUE_SETS FVS
WHERE FVS.parent_flex_value_set_id IS NOT NULL ) )
CONNECT BY
FVS.PARENT_FLEX_VALUE_SET_ID = PRIOR FVS.FLEX_VALUE_SET_ID;
May be this can help u
SELECT fvc.PARENT_FLEX_VALUE RUBRO_N0 ,FVT.description
DESC_RUBRO_N0,FVC.FLEX_VALUE RUBRO_N1 , fvc.DESCRIPTION
DESC_RUBRO_N1,FVC2.FLEX_VALUE RUBRO_N2 , FVC2.DESCRIPTION
DESC_RUBRO_N2,FVC3.FLEX_VALUE RUBRO_N3 , FVC3.DESCRIPTION
DESC_RUBRO_N3,FVC4.FLEX_VALUE RUBRO_N4 , FVC4.DESCRIPTION
DESC_RUBRO_N4,NVL(FVC4.FLEX_VALUE,NVL(FVC3.FLEX_VALUE,NVL(FVC2.FLEX_VALUE,FVC.FLEX_VALUE))) RUBROFIN
FROM FND_FLEX_VALUE_CHILDREN_V fvc
,FND_FLEX_VALUE_CHILDREN_V FVC2
,FND_FLEX_VALUE_CHILDREN_V FVC3
,FND_FLEX_VALUE_CHILDREN_V FVC4
,FND_FLEX_VALUES_TL FVT
WHERE fvc.FLEX_VALUE_SET_ID = 1016176
AND fvc.PARENT_FLEX_VALUE not in(SELECT FLEX_VALUE FROM FND_FLEX_VALUE_CHILDREN_V WHERE FLEX_VALUE_SET_ID = --YOUR FLEX_VALUE_SET_ID)
AND fvc.FLEX_VALUE = FVC2.PARENT_FLEX_VALUE (+)
AND fvc2.FLEX_VALUE = FVC3.PARENT_FLEX_VALUE (+)
AND fvc3.FLEX_VALUE = FVC4.PARENT_FLEX_VALUE (+)
AND fvc.PARENT_FLEX_VALUE = FVT.FLEX_VALUE_MEANING
AND FVT.SOURCE_LANG = 'ESA'
AND FVT.LANGUAGE = 'ESA' AND FVT.LAST_UPDATE_LOGIN NOT IN (0)
ORDER BY 1,2,3,5,7
;
Best Regards
This SQL from the Blitz Report library returns the hierarchy structure based on fnd_flex_value_norm_hierarchy with all containing child flex values: https://www.enginatics.com/reports/fnd-flex-value-hierarchy/
select
lpad(' ',2*(level-1))||level level_,
lpad(' ',2*(level-1))||ffvnh.parent_flex_value value,
ffvv.description,
decode(ffvnh.range_attribute,'P','Parent','C','Child') range_attribute,
ffvnh.child_flex_value_low,
ffvnh.child_flex_value_high,
decode(connect_by_isleaf,1,'Yes') is_leaf,
connect_by_root ffvnh.parent_flex_value root_value,
substr(sys_connect_by_path(ffvnh.parent_flex_value,'-> '),4) path,
ffvnh.parent_flex_value value_flat
from
(
select
ffvnh.parent_flex_value,
ffvnh.child_flex_value_low,
ffvnh.child_flex_value_high,
ffvnh.range_attribute,
ffvnh.flex_value_set_id
from
fnd_flex_value_norm_hierarchy ffvnh
where
ffvnh.flex_value_set_id=(select ffvs.flex_value_set_id from fnd_flex_value_sets ffvs where ffvs.flex_value_set_name=:flex_value_set_name)
union all
select
ffv2.flex_value parent_flex_value,
null child_flex_value_low,
null child_flex_value_high,
'x' range_attribute,
ffv2.flex_value_set_id
from
fnd_flex_values ffv2
where
2=2 and
ffv2.summary_flag='N' and
ffv2.flex_value_set_id=(select ffvs.flex_value_set_id from fnd_flex_value_sets ffvs where ffvs.flex_value_set_name=:flex_value_set_name)
) ffvnh,
fnd_flex_values_vl ffvv
where
3=3 and
ffvnh.parent_flex_value=ffvv.flex_value and
ffvnh.flex_value_set_id=ffvv.flex_value_set_id
connect by nocycle
ffvnh.parent_flex_value between prior ffvnh.child_flex_value_low and prior ffvnh.child_flex_value_high and
decode(nvl(prior ffvnh.range_attribute,'P'),'P','Y','N')=ffvv.summary_flag
start with
ffvnh.parent_flex_value=:parent_flex_value and
1=1 and
(:parent_flex_value is not null or
not exists (select null from
fnd_flex_value_norm_hierarchy ffvnh0
where
ffvnh0.flex_value_set_id=(select ffvs.flex_value_set_id from fnd_flex_value_sets ffvs where ffvs.flex_value_set_name=:flex_value_set_name) and
ffvnh.parent_flex_value between ffvnh0.child_flex_value_low and ffvnh0.child_flex_value_high and
ffvv.summary_flag=decode(ffvnh0.range_attribute,'P','Y','N')
)
)
To see all hierarchies (different parent top values), remove the 'start with' restriction to the parent flex value:
ffvnh.parent_flex_value=:parent_flex_value
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..
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.