common-table-expression

Parsing error in in CTE query SQL server

怎甘沉沦 提交于 2019-12-12 17:16:24
问题 I have written a CTE query and I am executing the query in Microsoft SQL Server 2008 R2 Management Studio: WITH DependencyHierarchy(processName, dependProcessName) AS ( SELECT processName, dependProcessName, 1 as HierarchyLevel FROM processDependency UNION ALL SELECT e.processName, e.dependProcessName, eh.HierarchyLevel + 1 AS HierarchyLevel FROM processDependency e INNER JOIN DependencyHierarchy eh ON e.dependProcessName = eh.processName ) SELECT * FROM DependencyHierarchy ORDER BY

T-SQL query update null values

不打扰是莪最后的温柔 提交于 2019-12-12 15:13:42
问题 I have a very specific problem in T-SQL. If I can solve this example case I give you I think I will be able to solve my original case. Having this data in a table: DECLARE @Test TABLE ( Value INT ,Date DATETIME2(7) ); INSERT INTO @Test VALUES (NULL, '2011-01-01 10:00'), (NULL, '2011-01-01 11:00'), (2, '2011-01-01 12:00'), (NULL, '2011-01-01 13:00'), (3, '2011-01-01 14:00'), (NULL, '2011-01-01 15:00'), (NULL, '2011-01-01 16:00'), (4, '2011-01-01 17:00'), (NULL, '2011-01-01 18:00'), (5, '2011

Generate range of dates using CTE Oracle

孤街醉人 提交于 2019-12-12 15:08:30
问题 I want to generate a range of days between two different dates using recursive WITH clause in Oracle. WITH CTE_Dates (cte_date) AS ( SELECT CAST(TO_DATE('10-02-2017', 'DD-MM-YYYY') AS DATE) cte_date FROM dual UNION ALL SELECT CAST( (cte_date + 1) AS DATE) cte_date FROM CTE_Dates WHERE TRUNC(cte_date) + 1 <= TO_DATE('20-02-2017', 'DD-MM-YYYY') ) SELECT * FROM CTE_Dates The returned results are completely other than expected: 10-02-2017 09-02-2017 08-02-2017 07-02-2017 06-02-2017 ... (unlimited

How to make ORDER SIBLINGS BY?

家住魔仙堡 提交于 2019-12-12 14:27:06
问题 I'm using PostgreSQL 9.1.6 and trying to build recursive SQL. I want to sort like ORDER SIBLINGS BY in SQL-Server does it. Editor's note: This is probably supposed to refer to Oracle , where ORDER SIBLINGS BY actually exists. Test table: create table RECURSIVE_TEST( EMP_ID int, MANAGER_ID int, EMP_NAME varchar(30) ); insert into recursive_test values (1 ,0 ,'MANAGER1'), (2 ,0 ,'MANAGER2'), (3 ,0 ,'MANAGER3'), (4 ,0 ,'MANAGER4'), (5 ,1 ,'emp1'), (6 ,3 ,'emp2'), (7 ,4 ,'emp3'), (8 ,2 ,'emp4'),

Updating record using CTE?

女生的网名这么多〃 提交于 2019-12-12 11:23:08
问题 ALTER PROCEDURE SP_PriceUpdate -- Add the parameters for the stored procedure here @PriceRuleID INT = NULL, @CategoryID INT = NULL, @SiteID INT = NULL AS SET NOCOUNT ON; BEGIN WITH PriceCTE(ProductID, CategoryID, SalePrice) AS ( SELECT ProductID, CategoryID, SalePrice FROM CAT_Products WHERE CategoryID = @CategoryID ) SELECT ProductID,categoryID,SalePrice FROM PriceCTE DECLARE @Value DECIMAL(32,2), @Type NVARCHAR(5), @Inc_Dec NVARCHAR(5) SELECT @Value = value FROM VB_PriceRule WHERE ID =

PostgreSQL:how to update rows in CTE

不羁岁月 提交于 2019-12-12 10:49:51
问题 am running PostgreSQL 9.2 . below given is a sample of my huge and ugly query with cte as( select ....... from aTable ),cte1 as ( select ..... from bTable inner join cte using(anID) ),update_cte as( update cte set aField=(select somthing from cte1) ) select * from cte i need to create a view with the final result . while executing the above am getting an error which is below. ERROR: relation "cte" does not exist I know am doing something bad. hope you can understand what am trying to achieve

PostgreSql -> CTE + UPDATE + DELETE -> not expected result, why?

自作多情 提交于 2019-12-12 10:32:37
问题 Just interested, why below ( simplified ) example works this way. CREATE TABLE test (id SERIAL, val INT NOT NULL, PRIMARY KEY(id)); INSERT INTO test (val) VALUES (1); WITH t AS ( UPDATE test SET val = 1 RETURNING id ) DELETE FROM test WHERE id IN ( SELECT id FROM t); Result: DELETE 0 Question: Why DELETE did not find any rows to delete? PostgreSql version 9.2.1 Transaction isolation = read commited Thanks! 回答1: I suspect it has something to do with this line in the docs - The primary 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)

How To Get All children and itself from Hierarchical data with CTE In SQL Server 2005 using stored procedure

半城伤御伤魂 提交于 2019-12-12 09:11:13
问题 I have many similar structure tables like this: CREATE TABLE [dbo].[tbl_Hierarchy]( [ID] [int] NOT NULL, [ParentID] [int] NOT NULL, [Text] [nvarchar](100) NOT NULL, --other field irrelevant to my question ) INSERT INTO dbo.tbl_Hierarchy VALUES(1,0,'parent1') INSERT INTO dbo.tbl_Hierarchy VALUES(2,0,'parent2') INSERT INTO tbl_Hierarchy VALUES(3,1,'child1') INSERT INTO tbl_Hierarchy VALUES(4,3,'grandchild1') INSERT INTO tbl_Hierarchy VALUES(5,2,'child2') Can you help me writing such as a stored

CTE Query: How to make them in order

与世无争的帅哥 提交于 2019-12-12 09:01:08
问题 I have a table which stores company information and their parent company in a regular hierarchical manner, with companyid, parentid and name. I just learn CTE query in Sql Server and write this query WITH tableR (ParentCompanyID, CompanyID, Levels) AS ( -- Anchor member definition SELECT e.ParentCompanyID, e.CompanyID, 0 As Levels FROM tblCompany AS e WHERE ParentCompanyID in (9) UNION ALL -- Recursive member definition SELECT e.ParentCompanyID, e.CompanyID, Levels + 1 FROM tblCompany AS e