common-table-expression

Insert data in 3 tables at a time using Postgres

拜拜、爱过 提交于 2019-12-17 02:58:29
问题 I want to insert data into 3 tables with a single query. My tables looks like below: CREATE TABLE sample ( id bigserial PRIMARY KEY, lastname varchar(20), firstname varchar(20) ); CREATE TABLE sample1( user_id bigserial PRIMARY KEY, sample_id bigint REFERENCES sample, adddetails varchar(20) ); CREATE TABLE sample2( id bigserial PRIMARY KEY, user_id bigint REFERENCES sample1, value varchar(10) ); I will get a key in return for every insertion and I need to insert that key in the next table. My

Lead() and LAG() functionality in SQL Server 2008

情到浓时终转凉″ 提交于 2019-12-14 03:56:07
问题 Hope all the SQL GURUS out there are doing great :) I am trying to simulate LEAD() and LAG() functionality in SQL Server 2008. This is my scenario: I have a temp table which is populated using the base query with the business logic for mileage. I want to calculate accumulated mileage for each user per day. The temp table is setup using ROW_NUMBER() , so I have all the data needed in the temp table except the accumulated mileage. I have tried using a CTE with the base query and self joining

T-SQL bad performance with CTE

核能气质少年 提交于 2019-12-14 03:43:16
问题 I have a performance question about Common table expressions in SQL Server. In our developer team we use a lot of chaining CTEs when building our queries. I am currently working on a query which had terrible performance. But I found out that if I in the middle of the chain inserted all the records up to that CTE in a temporary table instead and then continued but selecting from that temp table I improved performance significantly. Now I would like to get some help to understand if this type

Using ROW_NUMBER in CTE

这一生的挚爱 提交于 2019-12-14 03:17:59
问题 I am trying for a CTE with a ROW_NUMBER function. Query: with Demo as ( select *, ROW_NUMBER() over (partition by GID, MID order by MMID) as ROWNUMBER from Table1 where DateEntered > '2015-06-13 00:00:00.000' ) select * from Demo Here, the result I get is GID MID ROWNUMBER 1 1 1 1 2 1 1 2 2 1 2 3 2 1 1 2 2 1 2 2 2 2 3 5 2 4 4 Now, I want to get all the rows where combination of GID,MID has max row number value. But a condition here is that for those rows, the combination of GID,MID should

Using CTE as a block loop ?

二次信任 提交于 2019-12-14 03:16:32
问题 Besides while loop and Cursors How can I use CTE table to do n block operations ? for example : WITH cte AS ( SELECT 1 AS n UNION ALL SELECT n+1 FROM cte WHERE n+1<= 10 ) INSERT INTO tbl1 (id) select n from cte --till here it's all ok. INSERT INTO tbl2 (id) select n from cte --{notice tbl2} .fail here ( cause cte usages must come right after the decleration) But is there anything I can do so the second line will be also include ? p.s. ( beside keeping it as a temp table) 回答1: In general you

Can't drop temp table in Postgres function: “being used by active queries in this session”

爱⌒轻易说出口 提交于 2019-12-14 03:05:03
问题 It is expected to now take in a table called waypoints and follow through the function body. drop function if exists everything(waypoints); create function everything(waypoints) RETURNS TABLE(node int, xy text[]) as $$ BEGIN drop table if exists bbox; create temporary table bbox(...); insert into bbox select ... from waypoints; drop table if exists b_spaces; create temporary table b_spaces( ... ); insert into b_spaces select ... drop table if exists b_graph; -- Line the error flags. create

Execute stored procedure programmatically inside recursive CTE instead of Cursor

删除回忆录丶 提交于 2019-12-13 18:20:01
问题 I want to implement recursive CTE instead of following cursor because it takes lot of time to insert data by executing stored procedures programatecally inside cursor especially when there is larger number of records in table. I have tried CTE expression to solve this issue but i was failed to generate same with CTE and cursor really sucks and i totally want to replace it with CTE so is there any way to short out this. Advance thanks. Declare @Emp_Id As Numeric Declare CurEmpWeekOff cursor

CTE Hierachy descending but picking up child nodes not parents from ancestor

不打扰是莪最后的温柔 提交于 2019-12-13 15:50:53
问题 Explanation OK, the title might be a bit much :) I'll paste the scripts at the end. Imagine the following n-ary tree . | ---1 **(25)** | -----1.1 **(13)** | | | ----1.1.1 (1) | | | ----1.1.2 **(7)** | | | | | ----1.1.2.1 (4) | | | | | ----1.1.2.2 (3) | | | ----1.1.3 (5) | -----1.2 (2) | | -----1.3 (10) And so on, where the root branch "." can also have a 2,3,n branch and that branch would also have its own arbitrary tree form with n-branches possible from any give node. The values in brackets

Multi-part identifier could not be bound

谁说胖子不能爱 提交于 2019-12-13 14:09:41
问题 I know that there are several questions around this exception on SO, but nothing seen that helps me. I have following query giving me a "Multi-part identifier 'claim.fiData' could not be bound" -Exception: SELECT claim.idData FROM tabData as claim INNER JOIN dbo._previousClaimsByFiData(claim.fiData) AS prevClaim ON prevClaim.idData=claim.fiData GROUP BY claim.idData HAVING(prevClaim.fiMaxActionCode IN (8, 23, 24) and prevClaim.Repair_Completion_Date >= DATEADD(day,-90,prevClaim.Repair

Combining multiple CTE in TSQL

好久不见. 提交于 2019-12-13 07:14:57
问题 I have two CTEs and I want to combine them together. I tried a lot but I got a syntax errors. First Part: declare @TimeRanges as TABLE (SessionStart datetime, SessionEnd datetime); with TimeRanges as ( select @Start as StartTime, @Start + @TimeRange as EndTime union all select StartTime + @TimeRange, EndTime + @TimeRange from TimeRanges where EndTime < @Finish ) Here is the second part: ;with cte as ( select SessionStartTime as changetime,1 as CC from Calls union all select SessionCloseTime,