recursive-cte

Recursive CTE-Find all Employees Below Manager

落花浮王杯 提交于 2019-12-23 19:04:00
问题 I created a sample fiddle for this SQLFIDDLE CREATE TABLE [dbo].[Users]( [userId] [int] , [userName] [varchar](50) , [managerId] [int] , ) INSERT INTO dbo.Users ([userId], [userName], [managerId]) VALUES (1,'Darry',NULL), (2,'Cono',1), (3,'Abros',2), (4,'Natesh',1), (5,'Ani',3), (6,'Raju',5), (7,'Pinky',5), (8,'Miya',4) My requirement is like displaying all employees hierarchy below that particular manager Here is what i tried WITH UserCTE AS ( SELECT userId, userName, managerId, 0 AS

Does PostgreSQL have a pseudo-column like “LEVEL” in Oracle?

一曲冷凌霜 提交于 2019-12-19 15:04:52
问题 Does PostgreSQL have a pseudo-column like "LEVEL" in Oracle? If not, then how can we create a column similar to "LEVEL"? 回答1: Postgres does not have hierarchical queries. No CONNECT BY , therefore also no LEVEL . The additional module tablefunc provides the function connectby() doing almost the same. See: What is the equivalent PostgreSQL syntax to Oracle's CONNECT BY ... START WITH? Or you can do similar things with a standard recursive CTE and a level column that's incremented with every

Does PostgreSQL have a pseudo-column like “LEVEL” in Oracle?

做~自己de王妃 提交于 2019-12-19 15:00:01
问题 Does PostgreSQL have a pseudo-column like "LEVEL" in Oracle? If not, then how can we create a column similar to "LEVEL"? 回答1: Postgres does not have hierarchical queries. No CONNECT BY , therefore also no LEVEL . The additional module tablefunc provides the function connectby() doing almost the same. See: What is the equivalent PostgreSQL syntax to Oracle's CONNECT BY ... START WITH? Or you can do similar things with a standard recursive CTE and a level column that's incremented with every

PostgreSQL pass data from recursive CTE onto function

点点圈 提交于 2019-12-17 16:40:23
问题 I have the following problem: I am trying to discover all possible paths from source node ( node_s ) to target node ( node_t ). The format of the original table with graph edges is simple: | node_x | node_y | strength | , where "node_x" -> "node_y" is a direct edge with strength of the edge being "weight". The idea is if at any point during the exploration of the paths we discover that a node among its children has target node_t , we record this path and stop exploring paths from this node,

Recursive CTE while Parent Id not in a List

蓝咒 提交于 2019-12-12 19:45:20
问题 I have the following Nested Set That results in this tree 1 - |---- 2 - | |---- 4 - | |---- 7 | |---- 8 |----10 - |---- 9 3 - |---- 5 |---- 6 13- |---- 11 |---- 12 I have a list of produts SELECT Id, Name ... FROM Products A many-to-many relationship with the categories. All Categories can have Promotions. Ok now the problem. Let's say I have a ProductX in the categories 7,8,6. And Promotions in the categories 1,2,3. I need get the closest parent with a promotion per category or until there

SQL replacement for Recursive CTE

蹲街弑〆低调 提交于 2019-12-12 03:25:51
问题 I have a table Test which contains TEST ---- tablename|columnvalue|rankofcolumn A|C1|1 A|C2|2 A|C3|3 A|C4|4 B|CX1|1 B|CX2|2 C|CY1|1 C|CY2|2 C|CY3|3 I want to generate the path along with other columns as follows RESULT ---- tablename|columnvalue|rankofcolumn|path A|C1|1|C1 A|C2|2|C1->C2 A|C3|3|C1->C2->C3 A|C4|4|C1->C2->C3->C4 B|CX1|1|CX1 B|CX2|2|CX1->CX2 C|CY1|1|CY1 C|CY2|2|CY1->CY2 C|CY3|3|CY1->CY2->CY3 As per this question, I can use recursive CTE to achieve this WITH r ( tablename,

How does one Create a Parameterized Recursive CTE to flatten a heirarchy within a Scalar Function?

守給你的承諾、 提交于 2019-12-11 17:07:10
问题 I'm trying to create a scalar function to determine whether a user of a provided ID or any of their subordinates have orders under a collection of provided order IDs. Note I am using my own User-Defined Table Type of IntegerIdTableType to take in the collection of OrderIds. CREATE FUNCTION DoOrdersExistUnderUserOrUsersSubordinates ( @orderIds dbo.IntegerIdTableType READONLY, @userId INT ) RETURNS BIT AS BEGIN RETURN ( WITH GetUserIds(ordinateUserId) AS ( SELECT ordinateUserId UserId UNION ALL

SQL Recursive CTE: Finding objects linked by property

廉价感情. 提交于 2019-12-11 08:56:20
问题 I'm just trying to understand CTE and recursion to solve an issue that I would previously have used a cursor for. create table ##ACC ( AccNo int, Property char ) Insert into ##ACC VALUES (1,'A'),(1,'B'),(2,'A'),(2,'C'),(3,'C'),(4,'D') What I'm trying to achieve is to get a list of all AccNo's, and all AccNo's they're related to via Property. So my expected results are PrimaryAccNo | LinkedAccNo 1 | 1 1 | 2 1 | 3 2 | 1 2 | 2 2 | 3 3 | 1 3 | 2 3 | 3 4 | 4 I've attempted the following code and

Recursive JSONB postgres

假如想象 提交于 2019-12-11 08:43:26
问题 I am trying to build a recursive CTE in Postgres that supports both arrays and objects, to return a list of key-value pairs and don't seem to be able to find a good example. This is my current code. with recursive jsonRecurse as ( select j.key as Path ,j.key ,j.value from jsonb_each(to_jsonb('{ "key1": { "key2": [ { "key3": "test3", "key4": "test4" } ] }, "key5": [ { "key6": [ { "key7": "test7" } ] } ] }'::jsonb)) j union all select jr.path || '.' || jr2.Key ,jr2.key ,jr2.value from

SQL Server 2014 Merging Overlapping Date Ranges

好久不见. 提交于 2019-12-09 03:26:28
问题 I have a table with 200.000 rows in a SQL Server 2014 database looking like this: CREATE TABLE DateRanges ( Contract VARCHAR(8), Sector VARCHAR(8), StartDate DATE, EndDate DATE ); INSERT INTO DateRanges (Contract, Sector, StartDate, Enddate) SELECT '111', '999', '01-01-2014', '03-31-2014' union SELECT '111', '999', '04-01-2014', '06-30-2014' union SELECT '111', '999', '07-01-2014', '09-30-2014' union SELECT '111', '999', '10-01-2014', '12-31-2014' union SELECT '111', '888', '08-01-2014', '08