164 questions
0 votes
2 answers
140 views
SQL query returns `ERROR: syntax error at or near "union"`
I am a beginner in SQL. What is the syntax issue with the query shown here? I just needed the random rows to be present n number of times. with recursive cte1 as ( select 1 as idx, company_id, ...
0 votes
1 answer
89 views
Hexadecimal concatenation in recursive CTE is truncated to 64 characters
I'm working on Nested Set Hierarchies. I'm using a recursive CTE to build the nested equivalent of adjacency list table. MySQL 8.4 is my platform. -- adjacency list table CREATE TABLE dct_node_adjc ( ...
1 vote
1 answer
97 views
Recursive CTE with cycle detection using path array
I have a recursive CTE definition (graph) in Postgres that involves cycle detection: fiddle CREATE TABLE relationships ( subject_type, subject_id, subject_relation, resource_type, ...
0 votes
1 answer
109 views
Recursive CTE 'Query Error: relation [] does not exist', PostgreSQL v17
Practicing some SQL from Danny Ma's case studies. Working on number 3, found here. The challenge question that I'm requires an iterative table of payments made during 2020. I'm scaffolding up to my ...
1 vote
1 answer
173 views
Recursive CTE in Firebird for calculating aggregate costs in hierarchical data
Consider the following tables: create table WORKS_WITH_BASE_COSTS ( CODE integer not null, BASE_COST decfloat default 0 not null, constraint PK_WORKS_WITH_BASE_COSTS primary key (CODE) ); ...
0 votes
1 answer
127 views
Recursive CTE returns duplicate rows
In SQL Server, I have a Transactions and a TransactionDetails table. Transactions table has the following columns: TransactionId Amount ProcessedDate TransactionDetailsId TransactionDetails table has ...
1 vote
1 answer
29 views
MySQL Recursive CTE to retrieve all Attached Nodes of a Table
I have a recursive structure with the following two tables: tree represents a hierarchy of nodes where the top node has parent = NULL. the items can be attached to any of the tree nodes, anywhere in ...
2 votes
1 answer
106 views
Recursive CTE to remove duplicates
I'm looking to clean up event data that happens to have "duplicate" rows for a given day. I want to remove rows for a day that have more than one status based on the context of the next day'...
0 votes
1 answer
59 views
Understanding inner join in recursive CTE (MySQL)
The code below is from: https://www.freecodecamp.org/news/mysql-common-table-expressions/ Data: CREATE TABLE categories ( id int, cat_name varchar(100), parent_category_id int DEFAULT NULL ...
1 vote
1 answer
680 views
Getting error recursive reference to query must not appear within its non-recursive term in postgres
I am running this query: WITH RECURSIVE base_record AS ( -- Base case: start from the initial defined term SELECT dt.id AS defined_term_id, dt.name AS term_name, 1 AS depth, ARRAY[...
0 votes
2 answers
92 views
Suppress Duplicates in SQL Server Recursive Query
I've got a data structure where a record represents a person. A person can have a relationship to another person. There are different types of relationship so it's possible that there can be multiple ...
0 votes
0 answers
81 views
MySql recursive query using pivot table (many-to-many relashionship)
I'm having a huge performance issue when I run recursive query. Here is my DB Structure: users: id(index) name 1 A 2 B 3 C 4 D 5 E user_user user_id(index) parent_id(index) 2 1 3 2 4 2 5 4 And this is ...
0 votes
1 answer
107 views
Sorting records for hierarchical relationships when there is more than one parent column
I have following table wherein 2 (can be more) foreign key columns define self-relationship between records of the table. [Hierarchical records in my problem dataset] Id Name Parent1__c Parent2__c ...
1 vote
1 answer
85 views
Postgres use recursive CTE to find the root node
I have seen and read many posts on using a recursive CTE to get the hierarchy from a self-related table. I want to get just the root node of an entry in my table. In other words, if I have a suburb ...
0 votes
2 answers
141 views
Querying a whole graph emulated with SQL Server tables
I have an SQL-Server 2019 database with 2 tables NODE and EDGE, where NODE consists of two columns ID (int) and NAME (varchar) and EDGE consists of two columns PARENTELEMENTID (int) and CHILDELEMENTID(...