common-table-expression

SQL insert into using CTE

為{幸葍}努か 提交于 2019-12-12 05:50:00
问题 I am facing a performance issue due to "Insert into" statement in sql. I am using a CTE to select data from multiple tables and insert into other table. It was working just fine until yesterday. Select takes less than a minute to retrieve the data where as insert into taking forever. Can some one please help me in understanding what i am doing wrong. Any help is highly appreciated. Thanks. Here is my code: I am using this query in an SP. I am trying to load 220K records to 1.5M records table.

Expand “From” & “To” date columns to 1 row per day within that range

懵懂的女人 提交于 2019-12-12 04:53:11
问题 Take the following sample data: WITH SampleData AS ( SELECT '8000213' AS EmployeeID, '2014-08-25 00:00:00.000' AS StartDate, '2014-08-31 00:00:00.000' AS EndDate, 28.5 AS HPW UNION ALL SELECT '8000213' AS EmployeeID, '2014-10-01 00:00:00.000' AS StartDate, NULL AS EndDate, 33 AS HPW UNION ALL SELECT '0003289' AS EmployeeID, '2014-04-25 00:00:00.0000' AS StartDate, '2014-04-30 00:00:00.000' AS EndDate, 36 AS HPW UNION ALL SELECT '0003289' AS EmployeeID, '2014-05-01 00:00:00.000' AS StartDate,

Using a CTE to split results across a CROSS APPLY

≡放荡痞女 提交于 2019-12-12 04:47:59
问题 I have some data that I need to output as rows containing markup tags, which I'm doing inside a table valued function. This has been working fine up to a point using code in the format below, using the search query to gather my data, and then inserting into my returned table using the output from results . I now need to take a longer data field and split it up over a number of rows, and I'm at something of a loss as to how to achieve this. I started with the idea that I wanted to use a CTE to

CTE Query throws Recursion Exhausted error

跟風遠走 提交于 2019-12-12 04:40:01
问题 The following CTE Query throws "The statement terminated. The maximum recursion 100 has been exhausted before statement completion." WITH MyCTE AS ( SELECT o.organizationid, organization AS organization FROM organization o INNER JOIN store s ON s.organizationid = o.organizationid UNION ALL SELECT store.storeid, CAST(storeNAme AS NVARCHAR(50)) AS storeNAme FROM store INNER JOIN MyCTE ON store.organizationid = MyCTE.organizationid) SELECT DISTINCT Organization FROM MyCTE when executing the

CTE Index recommendations on multiple keyed table

时光怂恿深爱的人放手 提交于 2019-12-12 04:13:54
问题 I have a View which is keyed on both ChargeID and CustomerID (Charge can be split between multiple customers). The View consists of 2 tables (heavily simplified here, actual tables have ~40 columns each): CREATE TABLE tblCharge (ChargeID int NOT NULL, ParentChargeID int) CREATE TABLE tblChargeShare (ChargeShareID int NOT NULL, ChargeID int NOT NULL, CustomerID int, TotalAmount money, TaxAmount money, DiscountAmount money) The View just joins these both together: CREATE VIEW

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,

CTE For a Complex Query

ぃ、小莉子 提交于 2019-12-12 03:05:01
问题 I have this query: SELECT C.[First Name], C.[Last Name], SUM(Salary.Emp_Salary) AS 'Current Salary' FROM ( SELECT Emp_First_Name AS "First Name", Emp_Last_Name AS "Last Name", MAX(Emp_Salary_Change_Year) AS "Change Year" FROM Employee_Details AS e INNER JOIN Country AS co ON e.Emp_Country_Id = co.Country_Id INNER JOIN State AS s ON e.Emp_State_Id = s.State_Id INNER JOIN Designation AS d ON e.Desig_Id = d.Desig_Id INNER JOIN Salary AS sa ON e.Emp_Id = sa.Emp_Id WHERE Emp_Active = 1 GROUP BY

Eliminate row that has null value using CTE

守給你的承諾、 提交于 2019-12-12 01:54:02
问题 I have some data as RowIdentifier ID RowID Position Data Rn 1 1 1 a1 A1 1 2 1 2 a2 A2 1 3 1 3 a3 NULL 1 4 1 4 a3 A3 2 5 1 1 b1 B1 1 6 1 2 b2 NULL 1 7 1 3 b2 B2 2 8 1 4 b3 B3 1 The desired output being ID RowID Position Data 1 1 a1 A1 1 1 b1 B1 1 2 a2 A2 1 2 b2 B2 1 3 a3 A3 1 3 b3 B3 I need to eliminate those rows where the Positions are duplicate and whose data are null. i.e. in the example, in RowIdentifier 3 and 4, the value in Position column is a3 but the thired RowIdentifier record will

How to use Common Table Expression and check no duplication in SQL Server

China☆狼群 提交于 2019-12-12 01:18:40
问题 I have a table references to itself. User table: id, username, managerid and managerid links back to id Now, I want to get all the managers including direct manager, manager of direct manager, so on and so forth... The problem is that I do not want to have a unstop recursive sql. So, I want to check if an id alreay in a list, I will not include it anymore. Here is my sql for that: with all_managers (id, username, managerid, idlist) as ( select u1.id, u1.username, u1.managerid, ' ' from users

How to set the “Order” of Items in a hierarchy table in an easier and shorter way?

落花浮王杯 提交于 2019-12-12 00:35:06
问题 Refering to this post, here is the overall question and codes: declare @tbl table (MenuItemID uniqueidentifier, PID uniqueidentifier, MenuID uniqueidentifier, SO tinyint, lvl tinyint) ;WITH cte (MenuItemID, PID, MenuID, SO, lvl) AS ( select MenuItemID, PID, MenuID, SO, 0 from MenuItems where del = 0 and Perms = 1 and MenuID = @MenuID and MenuID = PID UNION ALL SELECT MenuItems.MenuItemID, MenuItems.PID, MenuItems.MenuID, MenuItems.SO, cte.lvl + 1 FROM MenuItems JOIN cte ON cte.MenuItemID =