create table test_tab1(
seq_id NUMBER(10),
e_id NUMBER(10),
jira_key VARCHAR2(20),
stage_code NUMBER(10)
);
INSERT INTO test_tab1 VALUES(1,11,'JIRA_A',2);
INSERT INTO test_tab1 VALUES(2,12,'JIRA_B',3);
COMMIT;
create table test_tab2(
seq_id NUMBER(10),
e_id NUMBER(10),
jira_key VARCHAR2(20),
stage_code NUMBER(10)
);
Can anyone tell me why I am getting the n.stage_code invalid identifier error in the below code even though this column exists?
Expected should be that this query should not give this error since this column exist in the WITH sub query clause.
WITH got_new_code AS
(
SELECT m.e_id,m.jira_key
, m.stage_code AS new_code
, c.code
FROM test_tab1 m
CROSS APPLY (
SELECT LEVEL - 1 AS code
FROM dual
CONNECT BY LEVEL <= m.stage_code + 1
) c
WHERE m.stage_code IS NOT NULL
ORDER BY m.e_id,m.stage_code
)
SELECT *
FROM got_new_code n
FULL JOIN test_tab2 t USING (e_id,jira_key,stage_code);
Database version: Oracle 18c.
You have a USING clause which specifies stage_code as a column to join the left and right tables on and there is no stage_code from the got_new_code sub-query factoring clause as it has been aliased to new_code.
Either don't use USING and specify the join conditions manually or ensure that both sides of the join have all the columns in the USING clause:
WITH got_new_code (e_id, jira_key, new_code, code) AS (
SELECT m.e_id,m.jira_key
, m.stage_code
, c.code
FROM test_tab1 m
CROSS APPLY (
SELECT LEVEL - 1 AS code
FROM dual
CONNECT BY LEVEL <= m.stage_code + 1
) c
WHERE m.stage_code IS NOT NULL
ORDER BY m.e_id,m.stage_code
)
SELECT *
FROM got_new_code n
FULL JOIN test_tab2 t
ON ( t.e_id = n.e_id
AND t.jira_key = n.jira_key
AND t.stage_code = n.new_code );
or
WITH got_new_code (e_id, jira_key, stage_code, code) AS (
SELECT m.e_id,m.jira_key
, m.stage_code
, c.code
FROM test_tab1 m
CROSS APPLY (
SELECT LEVEL - 1 AS code
FROM dual
CONNECT BY LEVEL <= m.stage_code + 1
) c
WHERE m.stage_code IS NOT NULL
ORDER BY m.e_id,m.stage_code
)
SELECT *
FROM got_new_code n
FULL JOIN test_tab2 t USING (e_id,jira_key,stage_code);
db<>fiddle here
You can either remove the new_code alias:
WITH got_new_code AS
(
SELECT m.e_id,m.jira_key
, m.stage_code
, c.code
FROM test_tab1 m
CROSS APPLY (
SELECT LEVEL - 1 AS code
FROM dual
CONNECT BY LEVEL <= m.stage_code + 1
) c
WHERE m.stage_code IS NOT NULL
ORDER BY m.e_id,m.stage_code
)
SELECT *
FROM got_new_code n
FULL JOIN test_tab2 t USING (e_id,jira_key,stage_code);
or change the join from using to on:
WITH got_new_code AS
(
SELECT m.e_id,m.jira_key
, m.stage_code AS new_code
, c.code
FROM test_tab1 m
CROSS APPLY (
SELECT LEVEL - 1 AS code
FROM dual
CONNECT BY LEVEL <= m.stage_code + 1
) c
WHERE m.stage_code IS NOT NULL
ORDER BY m.e_id,m.stage_code
)
SELECT *
FROM got_new_code n
FULL JOIN test_tab2 t
ON t.e_id = n.e_id AND t.jira_key = n.jira_key AND t.stage_code = n.new_code;
... which gives the joining columns twice; so you probably want to coalesce those back to a single occurrence.
db<>fiddle
Related
Here is entry's hierarchy.
_________Milky Way (30)________
/ | \
Alpha(10) Beta(20) Delta(null)
/ \ |
Mars(7) Jupiter(3) Delta-child(44)
Parents value is a sum of it's children's values.
Ex.
Alpha = Mars + Jupiter = 7 + 3 = 10
Milky Way = Alpha + Beta + Delta = 10 + 20 + null = 30
The task: recalculate parents up to the root in case any child is updated. Let's even simplify the task: select all entries up to the root with recalculated values.
Imagine that Mars is updated. Now Mars value is 2.
_________Milky Way (?)________
/ | \
Alpha(?) Beta(20) Delta(null)
/ \ |
Mars(2) Jupiter(3) Delta-child(44)
It means that all parents should be updated:
Alpha = Mars + Jupiter = 2 + 3 = 5
Milky Way = Alpha + Beta + Delta = 5 + 20 + null = 25.
Note: Delta -> Delta-child coupling is broken and it's fine. It can happen lets leave it out of scope here. 've added this sample just to be sure that it won't be counted during calculation as hierarchy can be huge enough and tehre is no task to recalculate all children leaf, just parents up to the root.
As a result of some "select .. from hierarchy.."
I'd like to receive recalculated parents' values.
Ex.
id
name
value
1
Milky Way
25
2
Alpha
5
Code samples with already updated Mars (sqlfiddle links are below):
Schema
CREATE TABLE hierarchy
(
id int4,
parent_id int4,
name varchar(255),
value int4
);
Values
insert into hierarchy
values
(1, null, 'Milky Way', 30),
(2, 1, 'Alpha', 10),
(3, 1, 'Beta', 20),
(4, 1, 'Delta', null),
(5, 2, 'Mars', 2),
(6, 2, 'Jupiter', 3),
(7, 4, 'Delta-child', 44);
What I have tried:
I was able to list all leafs which should be used in calculation
sqlfiddle 1
WITH RECURSIVE cte AS (
SELECT h1.id, h1.parent_id, h1.name , h1.value from hierarchy h1
where h1.id = 5
UNION
SELECT h2.id, h2.parent_id, h2.name , h2.value from hierarchy h2
JOIN cte cte ON (cte.parent_id = h2.parent_id or cte.parent_id = h2.id )
where cte.id != h2.id
) select * from cte
order by id
When I tried to sum values, query goes in infinite loop for some reason
sqlfiddle 2
WITH RECURSIVE cte AS (
SELECT h1.id, h1.parent_id, h1.name , h1.value from hierarchy h1
where h1.id = 5
UNION
SELECT h2.id, h2.parent_id, h2.name , (h2.value + cte.value) as value from hierarchy h2
JOIN cte cte ON (cte.parent_id = h2.parent_id or cte.parent_id = h2.id )
where cte.id != h2.id
) select * from cte
order by id
There is one more query that I have tried, unfortunately it doesn't count sibling of parents.
sqlfiddle 3
WITH RECURSIVE cte AS (
SELECT h1.id, h1.parent_id, h1.name , h1.value from hierarchy h1
where h1.parent_id = (select parent_id from hierarchy where id = 5)
UNION
SELECT h2.id, h2.parent_id, h2.name , cte.value as value from hierarchy h2
JOIN cte cte ON (cte.parent_id = h2.parent_id or cte.parent_id = h2.id )
where cte.id != h2.id
) select id, parent_id, name, sum(value) from cte
group by id, parent_id, name
order by id
I'd appreciate any assistance. :-)
It took me a while, but good work on your trials.
What i did is that I split the problem in parts.
go down the hierarchy with the recursive query
with recursive base_qry as (
select id,
parent_id,
value,
ARRAY[id] as id_array
from hierarchy
union all
select h.id,
h.parent_id,
h.value,
id_array || h.id as id_array
from hierarchy h join base_qry b on h.parent_id = b.id and h.value is not null
)
Understand the nodes affected filtering the last id of the array containing all the nodes with id_array[array_length(id_array,1)] = 5
nodes_affected as (
select * from base_qry
where id_array[array_length(id_array,1)] = 5
order by array_length(id_array,1) desc
LIMIT 1)
Find all the tree breanches than contribute to changed nodes (check here the filter for node id=5)
all_combinations as (
select b.id, b.parent_id, b.value, b.id_array from
base_qry b join nodes_affected n
on ARRAY[n.id_array] && ARRAY[b.id_array]
where (b.id_array[array_length(b.id_array,1)] = 5
or b.id_array #> ARRAY[5] = false)
)
aggregating up
select id_array[1]::int id, sum(value)
from all_combinations where id not in (select parent_id from all_combinations where parent_id is not null)
group by id_array[1]::int
order by 1
Whole query
with recursive base_qry as (
select id,
parent_id,
value,
ARRAY[id] as id_array
from hierarchy
union all
select h.id,
h.parent_id,
h.value,
id_array || h.id as id_array
from hierarchy h join base_qry b on h.parent_id = b.id and h.value is not null
),
nodes_affected as (
select * from base_qry
where id_array[array_length(id_array,1)] = 5
order by array_length(id_array,1) desc
LIMIT 1),
all_combinations as (
select b.id, b.parent_id, b.value, b.id_array from
base_qry b join nodes_affected n
on ARRAY[n.id_array] && ARRAY[b.id_array]
where (b.id_array[array_length(b.id_array,1)] = 5
or b.id_array #> ARRAY[5] = false)
)
select id_array[1]::int id, sum(value)
from all_combinations where id not in (select parent_id from all_combinations where parent_id is not null)
group by id_array[1]::int
order by 1
;
Start from the leafs and traverse all the nodes the leaf contributes to up the hierachy. Then just sum all contributions to a node.
WITH RECURSIVE cte AS (
SELECT id, parent_id, value
FROM hierarchy h1
WHERE not exists (select 1 from hierarchy h2 where h2.parent_id = h1.id)
UNION ALL
SELECT h.id, h.parent_id, case when h.value is null then 0 else cte.value end
FROM hierarchy h
JOIN cte ON (cte.parent_id = h.id)
)
select h.id, h.name, v.value
from (
select id, sum(value) as value
from cte
group by id
) v
join hierarchy h on h.id = v.id
order by h.id;
db<>fiddle
You can use a recursive CTE to get all nodes and their paths, and then for every non-leaf node in hierarchy, you can join the CTE back to hierarchy on the condition that the current hierarchy row id exists in the path and sum the values:
with recursive cte(id, p, n, v, t) as (
select h.*, concat('[', h.id::text) from hierarchy h where h.parent_id is null
union all
select h.id, h.parent_id, h.name, h.value, concat(c.t, ', ', h.id)
from cte c join hierarchy h on c.id = h.parent_id
)
select h.id, sum(c.v)
from hierarchy h join cte c on c.id != h.id and exists (select 1 from jsonb_array_elements(concat(c.t, ']')::jsonb) v where v.value::text = h.id::text)
group by h.id order by h.id
Output:
id sum
1 79
2 5
4 44
I have a query which is taking a long time. Is there anyway to write it better and in optimized way:
select 1, my_text from (
select distinct a.my_text||'_'||b.my_text my_text from (
select r_id, my_text
from tmp_v
where r_id in (
select o_id
from tmp_recid
) and v_id in (
select v_id
from o_v
where v_id in (
select o_id from tmp_record_vaid
union
select o_id from tmp_vue_vaid
) and va_nm = 'My V'
)
) a,
(
select r_id, my_text
from tmp_v
where r_id in (
select o_id
from tmp_recid
) and v_id in (
select v_id
from o_v
where v_id in (
select o_id from tmp_record_vaid
union
select o_id from tmp_vue_vaid
) and va_nm = 'My V 2'
)
) b
where a.r_id = b.r_id
except
select e_nm
from myp_ent_id
where p_m_id = 1 and entity_id in (
select entity_id
from o_e_t
where p_m_id = 1 and tag = 'Ample' and tag_category = 'My Type'
)
) a;
please, try with below query except "except query":
select 1, distinct(CONCAT(TV1.my_text, '_'))
from tmp_v TV1
left outer join tmp_recid TR1 on TR1.o_id = TV1.r_id
left outer join o_v OV1 on OV1.v_id = TV1.v_id and OV1.va_nm in ('My V', 'My V 2')
left outer join tmp_record_vaid TRV1 on OV1.v_id = TRV1.o_id
left outer join tmp_vue_vaid TVV1 on OV1.v_id = TVV1.o_id
where TR1.o_id is not null and (TRV1.o_id is not null OR TVV1.o_id is not null)
group by TV1.r_id
First level optimization: I combined two sub queries a and b(tmp_v) into one using CASE as the conditions are common for both.
select 1,my_text(
select distinct string_agg(amytext,'')||'_'||string_agg(bmytext,'') as mytext(
select r_id,case va_nm='My V' then my_text else null end as amy_text,case va_nm='My V 2' then my_text else null end as bmy_text
from tmp_v
where r_id in
(select o_id from tmp_recid)
and v_id in (select v_id from o_v where v_id in (select o_id from tmp_record_vaid union select o_id from tmp_vue_vaid))
)
except
select e_nm
from myp_ent_id
where p_m_id = 1 and entity_id in (
select entity_id
from o_e_t
where p_m_id = 1 and tag = 'Ample' and tag_category = 'My Type'
)
)a;
Further you can replace IN with EXISTS.
Below query is taking a lot of time to return results in two different databases. Is there any way to simplify this query?
WITH tblParent AS
(
SELECT *
FROM REFERENCES
WHERE referenced_id = 208593
UNION ALL
SELECT REFERENCES.*
FROM REFERENCES
JOIN tblParent ON REFERENCES.referenced_id = tblParent.entity_Id
)
SELECT DISTINCT(entity_Id)
FROM tblParent
WHERE entity_Id <> 208593 AND field_type = 'ChildField'
OPTION(MAXRECURSION 5)
This should simplify it:
WITH tblParent AS
(
SELECT entity_Id, 0 c
FROM [REFERENCES]
WHERE referenced_id = 208593
UNION ALL
SELECT r.entity_Id, 1
FROM [REFERENCES] r
JOIN tblParent
ON r.referenced_id = tblParent.entity_Id
)
SELECT DISTINCT entity_Id
FROM tblParent
WHERE c = 1
OPTION(MAXRECURSION 5)
By checking the c value, it becomes apparent that it is a child value. I am assuming that this text has field_type = 'ChildField' for all childs.
(REFERENCES is a reserved word and DISTINCT is not a function)
Since you're only interested in the child entityId's, then just select the fields you need in the recursive CTE.
WITH tblParent AS (
SELECT entity_Id, referenced_id as baseId
FROM [REFERENCES]
WHERE referenced_id = 208593
UNION ALL
SELECT t.entity_Id, cte.baseId
FROM tblParent cte
JOIN [REFERENCES] t
ON (t.referenced_id = cte.entity_Id
AND t.entity_Id <> cte.baseId -- to avoid a circular reference
)
WHERE t.field_type = 'ChildField'
)
SELECT DISTINCT entity_Id
FROM tblParent
WHERE entity_Id <> baseId
OPTION(MAXRECURSION 5)
And you might want to double-check if there's an index on referenced_id.
I have a Oracle query with a NOCYCLE clause which I have to translate to Postgres:
SELECT FG_ID,CONNECT_BY_ROOT FG_ID as Parent_ID
FROM FG t
START WITH t.Parent_filter_group_id is null
CONNECT BY NOCYCLE PRIOR t.FILTER_GROUP_ID = t.PARENT_FILTER_GROUP_ID
I have converted this one with the help of the question and answer in
connect_by_root equivalent in postgres
as
with recursive fg_tree as (
select FG_ID,
FG_ID as fg
from FG
where Parent_filter_group_id is null
union all
select c.FG_ID,
p.fg
from FG c join fg_tree p on p.FG_ID = PARENT_FILTER_GROUP_ID
)
select * from fg_tree
order by FG_ID
but in this there is no clause for NOCYCLE if the parent is also one of the children then this query will return error.
You can collect the IDs for each level and then join on the condition that the "current" id is not contained in the path:
with recursive fg_tree as (
select FG_ID,
FG_ID as fg,
array[fg_id] as path
from FG
where Parent_filter_group_id is null
union all
select c.FG_ID,
p.fg,
p.fg||c.fg_id
from FG c
join fg_tree p on p.FG_ID and c.fg_id <> ALL (p.path)
)
select fg_id, fg
from fg_tree
order by filter_group_id
oracle version:
SELECT o.object_id, p.plugin_id AS plugin_id, LEVEL, CONNECT_BY_ISCYCLE "Cycle"
FROM sst_cycle_obj o LEFT JOIN sst_cycle_devplug p ON p.device_id = o.object_id
WHERE CONNECT_BY_ISCYCLE = 1
CONNECT BY NOCYCLE o.object_id = PRIOR p.plugin_id
START WITH o.object_id = 11
create table sst_cycle_obj
(object_id numeric(10))
create table sst_cycle_devplug
(device_id numeric(10)
,plugin_id numeric(10))
insert into sst_cycle_obj (object_id)
(select 11 from dual
union all
select 12 from dual
union all
select 13 from dual)
insert into sst_cycle_devplug (device_id,plugin_id)
(select 11, 12 from dual
union all
select 12, 13 from dual
union all
select 13,11 from dual)
-->NOCYCLE2 CONNECT_BY_ISCYCLE "Cycle" Postgresql version
WITH recursive ncot
AS
(SELECT a.device_id,a.plugin_id,('{'||a.device_id||'}')::numeric[] as PATH, 0 AS cycle1, 1::int as level
FROM ( select o.object_id device_id, p.plugin_id as plugin_id
from sst_cycle_obj o
left join sst_cycle_devplug p on p.device_id=o.object_id
) a
WHERE a.device_id = 11
UNION ALL
SELECT objt.device_id,objt.plugin_id, ncot.path||objt.device_id::numeric as PATH, CASE WHEN objt.plugin_id = any(ncot.path) THEN 1 else 0 END AS cycle1, ncot.level + 1 as level
FROM ( select o.object_id device_id, p.plugin_id as plugin_id
from sst_cycle_obj o
left join sst_cycle_devplug p on p.device_id=o.object_id
) objt
JOIN ncot ON objt.device_id = ncot.plugin_id and objt.device_id <> ALL (ncot.path)
)
SELECT device_id,plugin_id,PATH,cycle1,level
FROM ncot
WHERE cycle1=1
--<
Create table #tmptble(RuleId, SubjectId, RID, Date)
Insert into #tmptble(RuleId,SubjectId, RID, Date)
Select RuleTable.RuleId, RuleTable.SubjectId, KeyTable.RID, KeyTable.ParentId
FROM RuleTable INNER JOIN KeyTable
ON KeyTable.RID = RuleTable.RID
This query is very slow. I have an clustered index on RID on KeyTable, clustered index on RuleId on RuleTable, Unique non clustered index on RuleId+SubjectId on the RuleTable. (RuleTable is used in various other places)
In the above query if I introduce a where clause like
Insert into #tmptble(RuleId,SubjectId, RID, Date)
Select RuleTable.RuleId, RuleTable.SubjectId, KeyTable.RID, KeyTable.ParentId
FROM RuleTable INNER JOIN KeyTable
ON KeyTable.RID = RuleTable.RID
WHERE KeyTable.RID = #RID -- #RID is passed into the storedproc
the running time reduces by > 50%. But the problem is that I use the original table result without the WHERE clause in the following way
WITH ResourceTree AS
(
SELECT
#tmptble.RuleId AS [RuleId],
#tmptble.SubjectId AS [SubjectRecId],
#tmptble.RId AS [RId],
#tmptble.ParentID AS [ParentID]
FROM #tmptble WHERE #tmptble.SubjectId = #SubjectId
AND #tmptble.RId = #RId
UNION ALL
-- Recursive step
-- Note that the recursive step uses the results from original #tmptable
SELECT
#tmptble.RuleId AS [RuleId],
#tmptble.SubjectId AS [SubjectId],
#tmptble.RId AS [RId],
#tmptble.ParentID AS [ParentID]
FROM #tmptble INNER JOIN ResourceTree RT
ON RT.ParentID = #tmptble.RId
)
SELECT *
FROM ResourceTree
Is there a way to optimize this query? Any kind of suggestions regarding indexes or way recursion is done would be helpful
Possible this be helpful for you -
;WITH temp AS
(
SELECT
r.RuleId
, r.SubjectId
, r.RID
, k.ParentId
FROM (
SELECT r.*
FROM dbo.RuleTable r
WHERE r.RID = #RID
) r
JOIN dbo.KeyTable k ON k.RID = r.RID
)
, ResourceTree AS
(
SELECT
t.RuleId
, [SubjectRecId] = t.SubjectId
, t.RId
, t.ParentID
FROM temp t
WHERE t.SubjectId = #SubjectId
UNION ALL
SELECT
t.RuleId
, t.SubjectId
, t.RId
, t.ParentID
FROM temp t
WHERE EXISTS (
SELECT 1
FROM ResourceTree r
WHERE r.ParentID = t.RId
)
)
SELECT *
FROM ResourceTree