recursive-query

Recursive function in sql server 2005?

百般思念 提交于 2019-11-28 14:19:10
Can anybody suggest programming examples that illustrate recursive functions? For example fibonacci series or factorial.. bytebender Here are a few articles that I found using google.com ;) Recursion in T–SQL Using recursion in stored procedures A Recursive User-Defined Function (SQL Server 2000) Search for "common table expressions." See also this link Update Adding example from the above-referenced link: ;WITH Fibonacci(n, f, f1) AS ( -- This is the anchor part -- Initialize level to 1 and set the first two values as per definition SELECT CAST(1 AS BIGINT), CAST(0 AS BIGINT), CAST(1 AS

Recursive query used for transitive closure

烂漫一生 提交于 2019-11-28 12:08:37
I've created a simple example to illustrate transitive closure using recursive queries in PostgreSQL. However, something is off with my recursive query. I'm not familiar with the syntax yet so this request may be entirely noobish of me, and for that I apologize in advance. If you run the query, you will see that node 1 repeats itself in the path results. Can someone please help me figure out how to tweak the SQL? /* 1 / \ 2 3 / \ / 4 5 6 / 7 / \ 8 9 */ create table account( acct_id INT, parent_id INT REFERENCES account(acct_id), acct_name VARCHAR(100), PRIMARY KEY(acct_id) ); insert into

Recursive CTE in presence of circular references

做~自己de王妃 提交于 2019-11-28 10:38:58
问题 I have a hierarchical structure in a SQL Server database. I'm trying to write a query to get all elements in the structure under a given element. So, given a DB table with the columns id and parent_id, this is what I do: WITH recursive_cte (root_id, id) AS ( SELECT parent_id, id FROM test_cte UNION ALL SELECT t.parent_id, r.id FROM test_cte t INNER JOIN recursive_cte r ON (r.root_id=t.id) ) SELECT * FROM recursive_cte WHERE root_id=0 Now, if there is a circular reference in the structure

MySQL Recursive get all child from parent

拜拜、爱过 提交于 2019-11-28 10:15:37
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/ , but i didn't find the result i was looking for.. any help would be appreciated, thanks SELECT * FROM

Create nested json object using php mysql

牧云@^-^@ 提交于 2019-11-28 10:09:08
I have two tables, table 1 has 2 fields (question_pk, question_name) and table 2 has 4 fields(ans_pk, options, question_fk and right_answer). I want to create json like the following structure { "type": "quiz", "name": "Brand Colors", "description": "Can you identify these brands by the background color?", "questions": [ { "name": "Can you identify this color?", "description": "#ea4c89", "answers": [ { "name": "Dribbble", "description": "dribbble.png", "weight": 1 }, { "name": "Amazon", "description": "amazon.png", "weight": 0 }, { "name": "Apple", "description": "apple.png", "weight": 0 } ] }

Recursive stored functions in MySQL

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 09:23:53
I'm trying to make a function that recursively builds a path for a specific category CREATE FUNCTION getPath(inId INT) RETURNS TEXT DETERMINISTIC BEGIN DECLARE return_path TEXT; DECLARE return_parent_id INT; SELECT CONCAT('/', name) INTO return_path FROM article_categories WHERE id = inId; SELECT parent_id INTO return_parent_id FROM article_categories WHERE id = inId; IF return_parent_id > 0 THEN SELECT CONCAT(getPath(return_parent_id), return_path) INTO return_path; END IF; RETURN return_path; END When I try to run this function with a category that has no parents (parent_id = 0) it works

What is the equivalent PostgreSQL syntax to Oracle's CONNECT BY … START WITH?

北城余情 提交于 2019-11-28 09:08:17
In Oracle , if I have a table defined as … CREATE TABLE taxonomy ( key NUMBER(11) NOT NULL CONSTRAINT taxPkey PRIMARY KEY, value VARCHAR2(255), taxHier NUMBER(11) ); ALTER TABLE taxonomy ADD CONSTRAINT taxTaxFkey FOREIGN KEY (taxHier) REFERENCES tax(key); With these values … key value taxHier 0 zero null 1 one 0 2 two 0 3 three 0 4 four 1 5 five 2 6 six 2 This query syntax … SELECT value FROM taxonomy CONNECT BY PRIOR key = taxHier START WITH key = 0; Will yield … zero one four two five six three How is this done in PostgreSQL ? Erwin Brandstetter Use a RECURSIVE CTE in Postgres: WITH

Recursive JPA query?

北慕城南 提交于 2019-11-28 07:35:55
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, Hibernate does not have this feature, or at least it didn't in March. So it seems unlikely that JPA would

SQL Server Equivalent of Oracle 'CONNECT BY PRIOR', and 'ORDER SIBLINGS BY'

强颜欢笑 提交于 2019-11-28 07:04:42
I've got this Oracle code structure I'm trying to convert to SQL Server 2008 ( Note: I have used generic names, enclosed column names and table names within square brackets '[]', and done some formatting to make the code more readable) : SELECT [col#1], [col#2], [col#3], ..., [col#n], [LEVEL] FROM (SELECT [col#1], [col#2], [col#3], ..., [col#n] FROM [TABLE_1] WHERE ... ) CONNECT BY PRIOR [col#1] = [col#2] START WITH [col#2] IS NULL ORDER SIBLINGS BY [col#3] What is the SQL Server equivalent template of the above code? Specifically, I'm struggling with the LEVEL , and 'ORDER SIBLINGS BY' Oracle

Tree Structure and Recursion

天涯浪子 提交于 2019-11-28 06:59:16
Using a PostgreSQL 8.4.14 database, I have a table representing a tree structure like the following example: CREATE TABLE unit ( id bigint NOT NULL PRIMARY KEY, name varchar(64) NOT NULL, parent_id bigint, FOREIGN KEY (parent_id) REFERENCES unit (id) ); INSERT INTO unit VALUES (1, 'parent', NULL), (2, 'child', 1) , (3, 'grandchild A', 2), (4, 'grandchild B', 2); id | name | parent_id ----+--------------+----------- 1 | parent | 2 | child | 1 3 | grandchild A | 2 4 | grandchild B | 2 I want to create an Access Control List for those units, where each unit may have it's own ACL, or is inheriting