recursive-query

Calculating the Weighted Average Cost of products stock

那年仲夏 提交于 2019-12-18 11:36:56
问题 I have to calculate my products stock cost, so for every product after each buy, i have to recalculate the Weighted Average Cost . I got a view thats bring me the current product's stock after each in/out: document_type document_date product_id qty_out qty_in price row_num stock_balance SI 01/01/2014 52 0 600 1037.28 1 600 SI 01/01/2014 53 0 300 1357.38 2 300 LC 03/02/2014 53 100 0 1354.16 3 200 LC 03/02/2014 53 150 0 1355.25 4 50 LC 03/02/2014 52 100 0 1035.26 5 500 LC 03/02/2014 52 200 0

Simple recursive query in Oracle

孤者浪人 提交于 2019-12-18 06:57:41
问题 I'm currently having some trouble understanding and writing recursive queries. I understand that recursive queries are used to search through hierarchies of information, but I haven't found a simple solution online that can travel up a hierarchy. For example, let's say that I have a relation that models a family tree: create table family_tree ( child varchar(10) parent varchar(10) ); If I wanted to write a recursive query that travelled up this family tree, collecting all parents until origin

Error - “UNION operator must have an equal number of expressions” when using CTE for recursive selection

∥☆過路亽.° 提交于 2019-12-18 04:33:24
问题 At this moment I have a table tblLocation with columns ID, Location, PartOfID . The table is recursively connected to itself: PartOfID -> ID My goal is to have a select output as followed: > France > Paris > AnyCity > Explanation: AnyCity is located in Paris, Paris is located in France. My solution that I found until now was this: ; with q as ( select ID,Location,PartOf_LOC_id from tblLocatie t where t.ID = 1 -- 1 represents an example union all select t.Location + '>' from tblLocation t

Find Parent Recursively using Query

故事扮演 提交于 2019-12-17 22:06:29
问题 I am using postgresql. I have the table as like below parent_id child_id ---------------------- 101 102 103 104 104 105 105 106 I want to write a sql query which will give the final parent of input. i.e suppose i pass 106 as input then , its output will be 103. (106 --> 105 --> 104 --> 103) 回答1: Here's a complete example. First the DDL : test=> CREATE TABLE node ( test(> id SERIAL, test(> label TEXT NOT NULL, -- name of the node test(> parent_id INT, test(> PRIMARY KEY(id) test(> ); NOTICE:

MySQL Recursive get all child from parent

天大地大妈咪最大 提交于 2019-12-17 18:57:42
问题 i have this case using recursive query on Mysql to find lv 2 and lv3 child on one table... database structure i'm using: id name parent 1 A 0 2 B 0 3 C 0 4 D 1 5 E 1 6 F 2 7 G 2 8 H 3 9 I 3 10 J 4 11 K 4 The result i was expecting, when filtering the data, where id=1, it will generate the result i'm expecting. id name parent 4 D 1 5 E 1 10 J 4 11 K 4 or this is the illustration. i've been looking everywhere, and reading this http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

Recursive JPA query?

丶灬走出姿态 提交于 2019-12-17 18:33:42
问题 Does JPA 2 have any mechanism for running recursive queries? Here's my situation: I have an entity E, which contains an integer field x. It also may have children of type E, mapped via @OneToMany. What I'd like to do is find an E by primary key, and get its value of x, along with the x values of all its descendants. Is there any way to do this in a single query? I'm using Hibernate 3.5.3, but I'd prefer not to have any explicit dependencies on Hibernate APIs. EDIT: According to this item,

SQL Select Query for organization tree (hierarchy)

好久不见. 提交于 2019-12-14 03:17:50
问题 I have a table like this; CREATE TABLE [dbo].[TH_ORGANIZATION] ( [ID_CORGANIZATION] [decimal](18, 0) IDENTITY(1,1) NOT NULL, [ID_CCOMPANY] [nvarchar](10) NOT NULL, [CORGANIZATION_NAME_tr] [nvarchar](100) NULL, [CORGANIZATION_NAME_en] [nvarchar](100) NULL, [CORGANIZATION_MAN_ID_CEMP] [decimal](10, 0) NULL, [CORGANIZATION_UPLINK_ID] [decimal](18, 0) NULL, [CACTIVE] [bit] NOT NULL CONSTRAINT [DF_TH_ORGANIZATION_CACTIVE] DEFAULT ((1)), CONSTRAINT [PK_TH_ORGANIZATION] PRIMARY KEY CLUSTERED ([ID

postgresql - building a tree with recursive and export that as json

雨燕双飞 提交于 2019-12-14 03:06:50
问题 I'm having issues on building a tree starting from a table. I'm following the tutorial from D.Fontaine but something is wrong in my query. here is a subset of rows from this table: abs=# select * from myproj_loparentrelation where root_id = '57b2e67b-5862-499a-a471-0f2f6b23440e' order by level ASC; id | root_id | level | display_sequence | lo_id | parent_id --------+--------------------------------------+-------+------------------+--------------------------------------+-----------------------

How to get all levels data using single SQL query for bill of material

混江龙づ霸主 提交于 2019-12-13 12:53:31
问题 I have a lot of bill of material Items and it contains their raw material and semi-finished goods. The semi-finished goods have their own bill of material with raw material and further semi-finished goods and so on. I want to create a SQL query by which I want to get the details of the bill of material up to the last level. I will put a WHERE clause for the parent bill of material and should get the details up to the last level. It is not limited to 7 levels, some items may even goto 10 or 15

Recursive CTE don't work when recursion predicate uses a bind variable

血红的双手。 提交于 2019-12-13 08:51:04
问题 When using recursive CTE in H2 (I know, experimental feature), the following query doesn't work: Connection con = getConnection(); System.out.println("Wrong result:"); PreparedStatement stmt = con.prepareStatement( "WITH recursive t(f) AS ( "+ " SELECT 1 "+ " UNION ALL "+ " SELECT t.f + 1 "+ " FROM t "+ " WHERE t.f < ? "+ ") "+ "SELECT t.f "+ "FROM t " ); stmt.setInt(1, 10); ResultSet rs = stmt.executeQuery(); while (rs.next()) System.out.println(rs.getInt(1)); The produced output is: 1 The