recursive-query

Teradata SQL Syntax - Common Table Expressions

被刻印的时光 ゝ 提交于 2019-12-12 09:50:05
问题 When using multiple CTE's in MSSQL 2008, I normally separate them with a comma. But when I try this in a Teradata environment, I get an error with the syntax. Works in MS SQL: WITH CTE1 AS (SELECT TOP 2 Name FROM Sales.Store) ,CTE2 AS (SELECT TOP 2 ProductNumber, Name FROM Production.Product) ,CTE3 AS (SELECT TOP 2 Name FROM Person.ContactType) SELECT * FROM CTE1,CTE2,CTE3 Now, attempting to put into Teradata syntax: WITH RECURSIVE CTE1 (Name) AS (SELECT TOP 2 Name FROM Sales.Store)

Combining Rows in SQL Viia Recursive Query

泪湿孤枕 提交于 2019-12-12 02:36:53
问题 I have the following table. Animal Vaccine_Date Vaccine Cat 2/1/2016 y Cat 2/1/2016 z Dog 2/1/2016 z Dog 1/1/2016 x Dog 2/1/2016 y I would like to get the results to be as shown below. Animal Vaccine_Date Vaccine Dog 1/1/2016 x Dog 2/1/2016 y,z Cat 2/1/2016 y,z I have the following code which was supplied via my other post at "Combine(concatenate) rows based on dates via SQL" WITH RECURSIVE recCTE AS ( SELECT animal, vaccine_date, CAST(min(vaccine) as VARCHAR(50)) as vaccine, --big enough to

SQL Recursive Query only return the last row

白昼怎懂夜的黑 提交于 2019-12-12 00:27:26
问题 I am trying to get the simple SQL Server 2008 Recursive Query to work. Following these examples: http://msdn.microsoft.com/en-us/library/ms186243.aspx and SQL Server recursive query I have a table, with id and parentID: ID fParent fName 2 NULL root 3 2 Drug_Error 4 2 Incident 5 4 2007 6 4 2009 7 5 2007-1 8 7 2008-2 with the following query with recury as ( Select fs1.ID ,fs1.FParent,fs1.FName from FoldersStructure as fs1 where fs1.FParent =null union all select fs2.id,fs2.FParent,fs2.FName

How to get the full Hierarchy with SQL CTE

隐身守侯 提交于 2019-12-11 16:43:51
问题 Hi I am trying to get the full hierarchy of my category. Here is my sample table ID PARENT_ID NAME DEPTH ------------------------------------------ 1 NULL A 1 2 NULL B 1 3 NULL C 1 4 1 D 2 5 4 E 3 The cte output should be this ID PARENT_ID NAME --------------------------- 1 NULL A 2 NULL B 3 NULL C 4 1 D 5 4 E 5 1 E As you can see id:5 is parent of 4 and 1. How do i query the whole tree 回答1: DECLARE @tmp TABLE(ID INT,ParentID INT,NAME VARCHAR(10),DEPTH INT) INSERT INTO @tmp VALUES (1 ,NULL ,

find relationships in a many-to-many structure with sql

感情迁移 提交于 2019-12-11 12:51:18
问题 my question is near a parent-child problem, and may need some recursive query, but i didn't find any answers by browsing forums. here is my problem: I've got 3 tables: T1 (people) T2 (places) T3 (relationship betwenn A and B) ------- ------ -------- id1 (pk) id2 (pk) id3 (pk) name city id_A id_B I would like to identify groups of places and people that are related. For example, if John visits London and Paris, Mary visits Paris and New York, Peter visits Bangalore and Tokyo, I would like to

postgresql: ordered result

混江龙づ霸主 提交于 2019-12-11 12:25:57
问题 I've a table that looks like: id | user_id | activity_id | activity_type | root_id | is_root | timestamp ----+---------+-------------+---------------+---------+---------+----------- 1 | 1 | 1 | text | 1 | 1 | 5 2 | 2 | 2 | text | 1 | 0 | 6 3 | 3 | 3 | text | 1 | 0 | 10 4 | 2 | 10 | text | 10 | 1 | 50 5 | 1 | 11 | text | 10 | 0 | 90 6 | 3 | 12 | text | 10 | 0 | 100 7 | 3 | 20 | text | 20 | 1 | 190 8 | 2 | 21 | text | 20 | 0 | 130 9 | 3 | 22 | text | 20 | 0 | 150 10 | 3 | 22 | text | 20 | 0 |

“Connect By” to generate rows from multiple delimited string

和自甴很熟 提交于 2019-12-11 11:58:16
问题 we can use "Connect By" to generate rows from a delimited string in oracle. like: SELECT Rn ,Regexp_Substr(data, '[^,]+', 1, LEVEL) Data FROM (SELECT 1 Rn ,'id:a,val:b,desc:c' data FROM Dual) Idata CONNECT BY Regexp_Substr(data, '[^,]+', 1, LEVEL) IS NOT NULL I want to use the inner query as a union all of a few more records. Something like: SELECT Rn ,Regexp_Substr(data, '[^,]+', 1, LEVEL) Data FROM (SELECT 1 Rn ,'id:a,val:b,desc:c' data FROM Dual UNION ALL SELECT 2 Rn ,'id:a2,val:b2,desc:c2

How to replace COALESCE with recursive CTE query?

天涯浪子 提交于 2019-12-11 09:53:25
问题 A follow-up question to Recursive CTE with three tables which helped me with CTE's in SQL Server. What has changed from the initial question? The table MANAGERS no longer includes rows for org.units that have no manager. The goal is to get the first non-null manager for a organizational unit. I've got it working using COALESCE and OUTER JOIN's, my question is if it is possible to use a recursive query instead? Example code follows below. DECLARE @ORG_PARENTS TABLE (ORG_ID INT, ORG_PARENT INT

SQL Tree Structure

╄→гoц情女王★ 提交于 2019-12-11 09:14:56
问题 I am new to this topic (i.e tree structure) in SQL. I have gone through different sources but still not clear. Here in my case, I have a table which I have attached herewith. Now here first, I have to retrieve a Full Tree for “OFFICE”. Also i have to find all the leaf nodes (those with no Children) in the attached hierarchical data. Please provide the answers with detail explanation. Thanks in advance. 回答1: You didn't specify your DBMS but with standard SQL (supported by all modern DBMS) you

EF - multiple includes to eager load hierarchical data. Bad practice?

与世无争的帅哥 提交于 2019-12-11 08:32:09
问题 I am needing to eager load a hierarchy structure so that I can recursively iterate through it. The eager loading is necessary to prevent multiple db queries while traversing the tree. It seems the consensus is that you can't eager load infinite levels of the tree, so I did something like var item= db.ItemHierarchies .Include("Children.Children.Children.Children.Children") .Where(x => x.condition == condition) to load 5 levels of children. This seems to get the job done. I'm wondering what the