recursive-query

postgres recursive query on the same table

可紊 提交于 2019-12-08 03:52:33
问题 i spent almost a day on it now and it seems like i am doing something wrong. ok , here is the relation: document_urls( doc_id , url_id) what i want to do is to build a sorte of graph that will show all the children that has been generated from a document through on of his urls. example select * from document_urls where doc_id=1 doc_id url_id 1 2 1 3 if i select all the document with url_id=3 or 2 i will find select * from document_urls where url_id=2 or url_id=3 doc_id url_id 1 2 1 3 2 3 now

Apply a recursive CTE on grouped table rows (SQL server 2005)

百般思念 提交于 2019-12-08 03:12:29
问题 I have a table (ROOMUSAGE) containing the times people check in and out of rooms grouped by PERSONKEY and ROOMKEY. It looks like this: PERSONKEY | ROOMKEY | CHECKIN | CHECKOUT | ROW ---------------------------------------------------------------- 1 | 8 | 13-4-2010 10:00 | 13-4-2010 11:00 | 1 1 | 8 | 13-4-2010 08:00 | 13-4-2010 09:00 | 2 1 | 1 | 13-4-2010 15:00 | 13-4-2010 16:00 | 1 1 | 1 | 13-4-2010 14:00 | 13-4-2010 15:00 | 2 1 | 1 | 13-4-2010 13:00 | 13-4-2010 14:00 | 3 13 | 2 | 13-4-2010

Directed graph SQL

杀马特。学长 韩版系。学妹 提交于 2019-12-08 02:53:34
问题 I have the following data set, which represents nodes in a directed graph. CREATE TABLE nodes (NODE_FROM VARCHAR2(10), NODE_TO VARCHAR2(10)); INSERT INTO nodes VALUES('GT','TG'); INSERT INTO nodes VALUES('GG','GC'); INSERT INTO nodes VALUES('AT','TG'); INSERT INTO nodes VALUES('TG','GC'); INSERT INTO nodes VALUES('GC','CG'); INSERT INTO nodes VALUES('TG','GG'); INSERT INTO nodes VALUES('GC','CA'); INSERT INTO nodes VALUES('CG','GT'); Visual representation: http://esser.hopto.org/temp/image1

Self-join Query

£可爱£侵袭症+ 提交于 2019-12-07 14:55:58
问题 Is it possible to do parent-child query just using join without looping through temporary table? Database sample : menuid name parent url ---------------------------------------------------------- A0000 Master A0000 # A0001 Rekening A0000 /master/rekening.aspx A0002 Master Nominal A0001 /master/nominal.aspx A0003 Master Satuan Other A0001 /master/satuan.aspx A0004 Master Kondisi A0000 /master/kondisi.aspx A0005 Master Tujuan A0003 /master/tujuan.aspx A0006 Master Item A0003 /master/item.aspx

Recursively list concents of Oracle's DBA_DEPENDENCIES view

佐手、 提交于 2019-12-07 13:00:15
问题 I would like a list of dependent tables (ultimately) of a given view. For example: SELECT NAME, TYPE, REFERENCED_NAME, REFERENCED_TYPE FROM DBA_DEPENDENCIES WHERE OWNER='FOO' AND NAME='VIEW_O1' The results: VIEW_O1 VIEW TABLE_01 TABLE VIEW_O1 VIEW TABLE_02 TABLE VIEW_O1 VIEW TABLE_03 TABLE VIEW_O1 VIEW VIEW_02 VIEW VIEW_O1 VIEW VIEW_03 VIEW I would like it to resemble: VIEW_O1 VIEW TABLE_01 TABLE VIEW_O1 VIEW TABLE_02 TABLE VIEW_O1 VIEW TABLE_03 TABLE VIEW_O1 VIEW VIEW_02 VIEW VIEW_O1 VIEW

MySQL/Postgres query 5 minutes interval data

a 夏天 提交于 2019-12-07 10:03:43
问题 I need help with the query, let's say that this is the data in table. timestamp ------------------- 2010-11-16 10:30:00 2010-11-16 10:37:00 2010-11-16 10:40:00 2010-11-16 10:45:00 2010-11-16 10:48:00 2010-11-16 10:55:00 2010-11-16 10:56:00 I want to get every first row (timestamp) that is at least 5 minutes later than the last. In this case the query should return: timestamp ------------------- 2010-11-16 10:30:00 2010-11-16 10:37:00 2010-11-16 10:45:00 2010-11-16 10:55:00 回答1: Recursive CTE

Applying CTE for recursive queries

陌路散爱 提交于 2019-12-07 06:42:40
问题 I am trying to apply CTE and recursive queries. The database is MariaDB 10.2 or greater. Business rules are as follows: An account can either be a holding or a portfolio. A holding consists of a given amount of money. Holdings can be active and inactive. A portfolio contains zero or more accounts, and these accounts can belong to more than one portfolio. The total value of each account is multiplied by a "weight" factor when determining the value of a portfolio. My schema is as follows (note

Recursive QuerySet with django

若如初见. 提交于 2019-12-06 16:46:58
I have this model referencing itself to allow building a tree: class PartCategory(models.Model): parent = models.ForeignKey('PartCategory', on_delete=models.DO_NOTHING, null=True, default=None, blank=True) name = models.TextField() I now have an SQL query to get one element and all of its child in one select (in this example the element with id=64): WITH RECURSIVE under_partcategory(id,name, parent_id,level) AS ( select api_partcategory.id,api_partcategory.name,api_partcategory.parent_id,0 from api_partcategory where api_partcategory.id=64 UNION ALL SELECT api_partcategory.id,api_partcategory

Directed graph SQL

徘徊边缘 提交于 2019-12-06 13:31:04
I have the following data set, which represents nodes in a directed graph. CREATE TABLE nodes (NODE_FROM VARCHAR2(10), NODE_TO VARCHAR2(10)); INSERT INTO nodes VALUES('GT','TG'); INSERT INTO nodes VALUES('GG','GC'); INSERT INTO nodes VALUES('AT','TG'); INSERT INTO nodes VALUES('TG','GC'); INSERT INTO nodes VALUES('GC','CG'); INSERT INTO nodes VALUES('TG','GG'); INSERT INTO nodes VALUES('GC','CA'); INSERT INTO nodes VALUES('CG','GT'); Visual representation: http://esser.hopto.org/temp/image1.JPG Using this data set, I want a user to enter a level (e.g. 2) and this returns all nodes 2 "hops"

Find cluster given node in PostgreSQL

霸气de小男生 提交于 2019-12-06 05:12:05
I am representing a graph in Postgres 9.1 (happens to be bidirectional and cyclic): CREATE TABLE nodes ( id SERIAL PRIMARY KEY, name text ); CREATE TABLE edges ( id SERIAL PRIMARY KEY, node1_id int REFERENCES nodes(id), node2_id int REFERENCES nodes(id) ); Given a particular node ID, want to retrieve all other nodes in that cluster. I started with the "Paths from a single node" example here , and this is where I got: WITH RECURSIVE search_graph(id, path) AS ( SELECT id, ARRAY[id] FROM nodes UNION SELECT e.node2_id, sg.path || e.node2_id FROM search_graph sg JOIN edges e ON e.node1_id = sg.id )