Teradata: Recursive Query: Dbmstutorials
Teradata: Recursive Query: Dbmstutorials
UNION ALL
--Recursive Join / Repeated iteration of the logic in the entire table
) Back to Top ↑
-- Termination Query
©SELECT
Copyright 2016-2021 dbmstutorials.com
temp_col,query_level,character_length(temp_col) AS wordSize
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
FROM [any_recursive_tablename]
QUALIFY RANK() OVER (ORDER BY wordSize desc) = 1;
Sample Table: Creating a sample volatile table rec_example with 2 columns for testing.
CREATE MULTISET VOLATILE TABLE rec_example
(
id INTEGER,
name VARCHAR(50)
)
PRIMARY INDEX(id)
ON COMMIT PRESERVE ROWS;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
name,
name_len,
qry_level+1,
SUBSTRING(name FROM qry_level FOR 1) || reverse_string,
tm
FROM RecReverseStringTable
WHERE qry_level <= name_len
)
-- Termination Query
SELECT id,
name,
reverse_string
FROM RecReverseStringTable
WHERE name_len+1 = qry_level ORDER BY 1;
Output:
id name reverse_string
---------- ------------------------------ ------------------------------
1 Pradeep Khatri irtahK peedarP
2 Teradata Database esabataD atadareT
Note: Starting TD15, Teradata provides direct function to reverse a string, visit Teradata String Functions to learn more about string functions.
Sample Table: Creating a sample volatile table rec_example with 2 columns for testing.
CREATE MULTISET VOLATILE TABLE rec_example
(
id INTEGER,
code CHAR(1)
)
PRIMARY INDEX(id)
ON COMMIT PRESERVE ROWS;
Back to Top ↑
© Data Population:
Copyright Inserting
2016-2021 sample data for Concatenating string from different rows.
dbmstutorials.com
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
INSERT INTO rec_example(1,'A');
INSERT INTO rec_example(2,'B');
INSERT INTO rec_example(3,'C');
INSERT INTO rec_example(4,'D');
Output:
code qry_level concat_string_len
---------- ---------- -------------------
D,C,B,A 4 7
Note: Teradata also provides other function to achieve similar result, visit Teradata Misc Functions to learn more about it.
Back to Top ↑
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
➠ Example 3: Finding a parent recursively for all the nodes(rows).
Sample Table: Creating a sample volatile table rec_example with 2 columns for testing.
CREATE VOLATILE TABLE Searchlist_tbl(
search_Id INTEGER,
value_txt VARCHAR(50),
parent_search_id INTEGER
)
PRIMARY INDEX(search_Id)
ON COMMIT PRESERVE ROWS;
Data Population: Inserting sample data for finding a parent recursively for all the nodes(rows).
INSERT INTO Searchlist_tbl VALUES(1,'search',null);
INSERT INTO Searchlist_tbl VALUES(2,'search1',1);
INSERT INTO Searchlist_tbl VALUES(3,'search2',2);
INSERT INTO Searchlist_tbl VALUES(4,'search3',3);
INSERT INTO Searchlist_tbl VALUES(5,'search4',4);
INSERT INTO Searchlist_tbl VALUES(6,'search5',5);
Recursive Query: Query to finding a parent recursively for all the nodes(rows).
WITH RECURSIVE RecursiveParent(search_Id,parent_search_Id,LVL)
AS
(
--Seed Query
SELECT search_Id ,search_id as parent_search_Id,1
FROM Searchlist_tbl WHERE parent_search_id IS NULL
UNION ALL
--RECURSIVE Join
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
SELECT search_Id,parent_search_Id,case when search_Id=parent_search_Id THEN null ELSE parent_search_Id END orig_parent,lvl
FROM RecursiveParent
order by 1;
Output:
search_Id parent_search_Id orig_parent LVL
---------- ---------------- ----------- ----
1 1 ? 1
2 1 1 2
3 1 1 3
4 1 1 4
5 1 1 5
6 1 1 6
7 7 ? 1
8 7 7 2
9 7 7 3
Back to Top ↑
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD