Quantcast
Viewing all articles
Browse latest Browse all 207

Recursive CTE get

I have the following tables:

Table1

identity_idvalue

Table2 - Same structure as Table1

identity_idvalue

Table3

source_iddestination_identity_id

Table1 and Table2 are both partitions from the same table and Table3 contains the connections between ids on that partition's tables.

One simple example

identity_idvaluetable
111table1
222table2
source_iddestination_identity_id
121

In this example the two records are connected directly but it's possible for them to be connected indirectly for instance:

identity_idvaluetable
111table1
222table2
333tablex
source_iddestination_identity_id
131
323

In this example, the values on Table1 and Table2 are connected via TableX and this can go any level deep.

Now I'm trying to do a query that would return in this case the rows from Table1 and Table2.

I did the following:

WITH RECURSIVE cte AS (    SELECT t1.id, t1.entity_id, t1.value    FROM table1 t1    JOIN table3 t3     ON t1.id = t3.source_id    UNION     SELECT j.id, j.entity_id, j.value    FROM cte j    JOIN table3 t3    ON j.id = t3.source_id    JOIN table2 t2     ON t2.id = t3.destination_id )SELECT * FROM cte;

But it's only returning:

1   1   1

instead of or similar (I just need the table1 and table2 data)

1   1   12   2   2

https://dbfiddle.uk/yNLG9-ON

Any help is welcome.

Thanks


Viewing all articles
Browse latest Browse all 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>