common-table-expression

Insert into from CTE

百般思念 提交于 2019-12-17 18:37:56
问题 WITH DTL AS (SELECT CMPI_CODE, CMN_CDTY_MTRL, CMI_WT_FACTOR, CMI_CNTRCT_RATE, 'PL', PRESENT_PRICE, TRM_CODE, ROUND(((NVL(PRESENT_PRICE,1)*CMI_WT_FACTOR) / CMI_CNTRCT_RATE),2) AS PL_FACTOR FROM VW_CMD_MATERIAL WHERE TRM_CODE = 41) INSERT iNTO IPA_PRCADJ_HDR(TRM_CODE,IPAPH_ADJ_FACTOR,IPAPH_AMT_CUR,IPAPH_REMARKS) SELECT TRM_CODE,SUM(PL_FACTOR) AS PL_FACTOR,((SUM(PL_FACTOR)*10)) AS AMT_CUR,'asdf' FROM DTL GROUP BY (TRM_CODE); showing an error ORA-00928: missing SELECT keyword 回答1: This is the

SQL Server: How to limit CTE recursion to rows just recursivly added?

馋奶兔 提交于 2019-12-17 18:21:54
问题 Simpler Example Let's try a simpler example, so people can wrap their heads around the concepts, and have a practical example that you can copy&paste into SQL Query Analizer: Imagine a Nodes table, with a heirarchy: A - B - C We can start testing in Query Analizer: CREATE TABLE ##Nodes ( NodeID varchar(50) PRIMARY KEY NOT NULL, ParentNodeID varchar(50) NULL ) INSERT INTO ##Nodes (NodeID, ParentNodeID) VALUES ('A', null) INSERT INTO ##Nodes (NodeID, ParentNodeID) VALUES ('B', 'A') INSERT INTO

How to use multiple WITH statements in one PostgreSQL query?

时光怂恿深爱的人放手 提交于 2019-12-17 18:00:16
问题 I would like to "declare" what are effectively multiple TEMP tables using the WITH statement. The query I am trying to execute is along the lines of: WITH table_1 AS ( SELECT GENERATE_SERIES('2012-06-29', '2012-07-03', '1 day'::INTERVAL) AS date ) WITH table_2 AS ( SELECT GENERATE_SERIES('2012-06-30', '2012-07-13', '1 day'::INTERVAL) AS date ) SELECT * FROM table_1 WHERE date IN table_2 I've read PostgreSQL documentation and researched into using multiple WITH statements and was unable to

How to create Temp table with SELECT * INTO tempTable FROM CTE Query

余生长醉 提交于 2019-12-17 17:21:46
问题 I have a MS SQL CTE query from which I want to create a temporary table. I am not sure how to do it as it gives an Invalid Object name error. Below is the whole query for reference SELECT * INTO TEMPBLOCKEDDATES FROM ;with Calendar as ( select EventID, EventTitle, EventStartDate, EventEndDate, EventEnumDays,EventStartTime,EventEndTime, EventRecurring, EventStartDate as PlannedDate ,EventType from EventCalender where EventActive = 1 AND LanguageID =1 AND EventBlockDate = 1 union all select

Keeping it simple and how to do multiple CTE in a query

雨燕双飞 提交于 2019-12-17 15:18:55
问题 I have this simple T-SQL query, it emits a bunch of columns from a table and also joins information from other related tables. My data model is simple. I have a scheduled event, with participants. I need to know how many participants participate in each event. My solution to this is to add a CTE that groups scheduled events and counts the number of participants. This will allow me to join in that information per scheduled event. Keeping the query simple. I like to keep my queries simple,

Window Functions or Common Table Expressions: count previous rows within range

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 13:26:47
问题 I'd like to use a window function to determine, for each row, the total number of preceding records meeting a certain criteria. A specific example: clone=# \d test Table "pg_temp_2.test" Column | Type | Modifiers --------+-----------------------------+----------- id | bigint | date | timestamp without time zone | I'd like to know for each date the count of rows within '1 hour previous' to that date . Can I do this with a window function? Or do I need to investigate CTE's? I really want to be

Are SELECT type queries the only type that can be nested?

橙三吉。 提交于 2019-12-17 07:54:49
问题 Is it possible to have an non-select query (update, insert, delete) embedded into another query? Something like (an insert inside a select) A single query: select such,and,such from .... where .... insert into .... ; 回答1: Basic answer There are CTEs (Common Table Expressions) in Postgres (like in any major modern RDBMS except MySQL). Since version 9.1 that includes data-modifying CTEs. Those can be "nested". Update: MySQL 8.0 finally adds CTEs. Unlike subqueries CTEs pose as optimization

Common Table Expression (CTE) in linq-to-sql?

痞子三分冷 提交于 2019-12-17 07:41:48
问题 Is it possible to do common table expressions (CTE) (like shown below) in Linq to SQL. I'm pretty new to CTE's as well as Linq to SQL. I'm currently Stored Proc free (but not against them by any means) so i don't want to take the leap to stored procs just for one query unless it's totally necessary. Here's an example of what I'm doing in SQL that I'm wondering if I can do in Linq to SQL: WITH TaskHierarchy (TaskID, [Subject], ParentID, HierarchyLevel, HierarchyPath) AS ( -- Base case SELECT

Common Table Expression, why semicolon?

梦想的初衷 提交于 2019-12-17 05:06:08
问题 Usually in SQL Server Common Table Expression clause there is semicolon in front of the statement, like this: ;WITH OrderedOrders AS --semicolon here ( SELECT SalesOrderID, OrderDate, ROW_NUMBER() OVER (ORDER BY OrderDate) AS 'RowNumber' FROM Sales.SalesOrderHeader ) SELECT * FROM OrderedOrders WHERE RowNumber BETWEEN 50 AND 60 Why? 回答1: To avoid ambiguity because WITH can be used elsewhere ..FROM..WITH (NOLOCK).. RESTORE..WITH MOVE.. It's optional to terminate statements with ; in SQL Server

How to transform a MSSQL CTE query to MySQL?

本秂侑毒 提交于 2019-12-17 03:40:24
问题 in my MySQL schema, I have the category(id, parentid, name) table In the MSSQL, I have that CTE query (to build a category tree from the bottom up for a supplied category ID: with CTE (id, pid, name) as ( select id, parentid as pid,name from category where id = 197 union all select CTE.pid as id , category.parentid as pid, category.name from CTE inner join category on category.id = CTE.pid ) select * from CTE How to 'transform' that query to MySQL ? 回答1: Unfortunately MySQL doesn't support