common-table-expression

Using CTE recursion for STACKING splittable items into bins

自闭症网瘾萝莉.ら 提交于 2019-12-13 04:26:59
问题 I created a minimalistic sample data for the purpose of studying and presenting this interesting task. Bellow is code to setup test tables. The task is to distribute items into pre-set bins , while: we cannot exceed maximum capacity of a bin item can (should) be split, if it exceeds the remaining bin capacity there's a bin (always first), which has "unlimited" (very high) capacity and anything that does not fit into regular bins has to end up here. While it is particular to this example, in

CTE RECURSIVE optimization, how to?

百般思念 提交于 2019-12-13 04:19:01
问题 I need to optimize the performance of a commom WITH RECURSIVE query... We can limit the depth of the tree and decompose in many updates, and can also change representation (use array)... I try some options but perhaps there are a "classic optimization solution" that I'm not realizing. All details There are a t_up table, to be updated, with a composit primary key (pk1,pk2), one attribute attr and an array of references to primary keys... And a unnested representation t_scan , with the

PostgreSQL ERROR: subquery in FROM cannot refer to other relations of same query level

送分小仙女□ 提交于 2019-12-13 04:15:19
问题 I'm having an inordinate amount of trouble using CTEs as arguments to a PostgreSQL function call, and no amount of refactoring into subqueries seems to help; I get either subquery in FROM cannot refer to other relations of same query level or function expression in FROM cannot refer to other relations of same query level . The simplified SQL: create type similarity as ( distance float, explanation text ); create or replace function similarity(my_user_id int) returns table(user_id int,

How do i can show forecast years data from row into column?

回眸只為那壹抹淺笑 提交于 2019-12-13 04:03:49
问题 Suppose if Item-A's Sale in 2013 is 100 Quantity and I'm expecting 10% of sales growth in next year i.e. in 2014 -------------------- ITEM | YEAR | QTY | -------------------- ITM-A| 2013 | 100 | ITM-B| 2013 | 200 | -------------------- if I want to forecast sale data for up to year 2015 ------------------------------ Item | 2013 | 2014 | 2015 | ------------------------------ Item-A | 100 | 110 | 121 |--each year qty incremented by 10% of its Item-B | 200 | 220 | 242 |--previous year qty -----

concatenating single column in TSQL

这一生的挚爱 提交于 2019-12-13 03:48:09
问题 I am using SSMS 2008 and trying to concatenate one of the rows together based on a different field's grouping. I have two columns, people_id and address_desc. They look like this: address_desc people_id ---------- ------------ Murfreesboro, TN 37130 F15D1135-9947-4F66-B778-00E43EC44B9E 11 Mohawk Rd., Burlington, MA 01803 C561918F-C2E9-4507-BD7C-00FB688D2D6E Unknown, UN 00000 C561918F-C2E9-4507-BD7C-00FB688D2D6E Jacksonville, NC 28546 FC7C78CD-8AEA-4C8E-B93D-010BF8E4176D Memphis, TN 38133

ORACLE: Using CTEs (Common Table Expressions) with PL/SQL

陌路散爱 提交于 2019-12-13 03:34:59
问题 First off, my background is in SQL Server. Using CTEs (Common Table Expressions) is a breeze and converting it to a stored procedure with variables doesn't require any changes to the structure of the SQL other than replacing entered values with variable names. In Oracle PL/SQL however, it is a completely different matter. My CTEs work fine as straight SQL, but once I try to wrap them as PL/SQL I run into a host of issues. From my understanding, a SELECT now needs an INTO which will only hold

Problem in counting nulls and then merging them with the existing rows

自闭症网瘾萝莉.ら 提交于 2019-12-13 03:07:56
问题 Input: ID groupId RowID Data 1 1 1 W 2 1 1 NULL 3 1 1 NULL 4 1 1 Z 5 1 2 NULL 6 1 2 NULL 7 1 2 X 8 1 2 NULL 9 1 3 NULL 10 1 3 NULL 11 1 3 Y 12 1 3 NULL Expected Output GroupId NewData 1 2Y1,2X1,W2Z For every Null there will be a numeric count. That is if there are two nulls then the numeric value will be 2. The ddl is as under DECLARE @t TABLE(ID INT IDENTITY(1,1) , GroupId INT, RowID INT, Data VARCHAR(10)) INSERT INTO @t (GroupId, RowID,DATA) SELECT 1,1,'W' UNION ALL SELECT 1,1,NULL UNION

SQL CTE counting childs recursion

久未见 提交于 2019-12-12 18:27:28
问题 I'd like (using cte) to count children in table in that way to have at parent level number of all children including theirs children . Is there any sample available? 回答1: CREATE TABLE t_parent (id INT NOT NULL PRIMARY KEY, parentID INT NOT NULL) INSERT INTO t_parent VALUES (1, 0) INSERT INTO t_parent VALUES (2, 1) INSERT INTO t_parent VALUES (3, 1) INSERT INTO t_parent VALUES (4, 2) INSERT INTO t_parent VALUES (5, 1) INSERT INTO t_parent VALUES (6, 5) INSERT INTO t_parent VALUES (7, 5); WITH

Trying to obtain the accurate information (CTE - recursive)

对着背影说爱祢 提交于 2019-12-12 18:15:11
问题 I have different tables and the goal is to obtain the approval workflow for every customer, displaying that information in this way: > CLIENT | APPROVER1 | APPROVER2 | APPROVER3 | APPROVER4 First of all, i have a table called entities (12, 'Math Andrew', 308, 'CHAIN1-MathAndrew') (13, 'John Connor', 308, 'CHAIN2-JohnConnor') (18, 'ZATCH', 309, null), (19, 'MAX', 309, null), (20, 'Ger',310, null), (21, 'Mar',310, null), (22, 'Maxwell',311, null), (23, 'Ryan',312, null), (24, 'Juy',313, null),

SQL Server - CTE with 2 tables until qty consumed

夙愿已清 提交于 2019-12-12 17:30:43
问题 I am try to perform a recursive join of my 2 tables (SQL Server 2012) like below: Table: Purchase szProductID nQty szSupplierCode 0001 5 A-101 0001 50 A-102 0001 2 A-103 0001 70 A-104 and Table: Sales szProductID nQty szSalesID 0001 10 S-101 0001 20 S-102 0001 20 S-103 0001 50 S-104 And I need my result like this : szProductID nQtySales SupplierCode SalesID 0001 5 A-101 S-101 0001 5 A-102 S-101 0001 20 A-102 S-102 0001 20 A-102 S-103 0001 5 A-102 S-104 0001 2 A-103 S-104 0001 43 A-104 S-104