recursive-query

show only categories that have products in them

旧巷老猫 提交于 2020-01-06 03:19:27
问题 excuse the bad title but I couldn't find a good way to express what I want in abstract terms. Anyway I have 3 tables tbl_product: PID | productname 1 | product 1 2 | product 2 3 | product 3 4 | product 4 .. tbl_categories, motherCategory allows me to nest categories: CID | categoriename | motherCategory 1 | electronics | NULL 2 | clothing | NULL 3 | Arduino | 1 4 | Casings, extra's | 3 .. tbl_productInCategory PID and CID are foreign keys to PID and CID in tbl_product and tbl_categories

how to write Recursive CTE in SQL server 2012 for hour and min

强颜欢笑 提交于 2020-01-05 08:02:12
问题 I would like to generate a list of half an hour interval. Any suggestion would be very helpful. I tried this and did not work. Thank you WITH cte AS (select convert(varchar, DATEADD(Day, 0, DATEDIFF(Day, 0, GetDate())), 108) AS Today UNION ALL SELECT dateadd(MINUTE, 30, Today) AS Today FROM cte WHERE dateadd(MINUTE, 30,Today) < (select convert(varchar, DATEADD(Day, 1, DATEDIFF(Day, 0, GetDate())), 108)) ) SELECT* FROM cte To get: 0:00 0:30 1:00 1:30 2:00 2:30 3:00 3:30 4:00 4:30 5:00 5:30 6

mysql how to find the total number of child rows with respect to a parent

混江龙づ霸主 提交于 2020-01-05 07:18:05
问题 I have a table which having parent child relatiionship like this, Employee_ID | Employee_Manager_ID | Employee_Name -------------------------------------------------------- 1 | 1 | AAAA 2 | 1 | BBBB 3 | 2 | CCCC 4 | 3 | DDDD 5 | 3 | EEEEE Is it possible to get the count of all the employees come under a particular employee(Not only direct child,count of all the childs of child ) using a single query ? Eg if the input = 1 output should be 4 if input = 2 ,output should be 3 thanks in advance

Reverse aggregation inside of common table expression

旧巷老猫 提交于 2020-01-03 21:07:00
问题 I would have expected that the following query returns all people with their respective children. WITH RECURSIVE nested_people (id, name, children) AS ( SELECT id, name, NULL::JSON AS children FROM people WHERE parent_id IS NULL UNION ALL SELECT people.id, people.name, ROW_TO_JSON(nested_people.*) AS children FROM people JOIN nested_people ON people.parent_id = nested_people.id ) SELECT * FROM nested_people; But actually it does the exact reverse. I can't think of a way to do correct nesting

Reverse aggregation inside of common table expression

自作多情 提交于 2020-01-03 21:02:14
问题 I would have expected that the following query returns all people with their respective children. WITH RECURSIVE nested_people (id, name, children) AS ( SELECT id, name, NULL::JSON AS children FROM people WHERE parent_id IS NULL UNION ALL SELECT people.id, people.name, ROW_TO_JSON(nested_people.*) AS children FROM people JOIN nested_people ON people.parent_id = nested_people.id ) SELECT * FROM nested_people; But actually it does the exact reverse. I can't think of a way to do correct nesting

Joining two Hierarchical queries to form larger Hierarchy

我们两清 提交于 2020-01-03 16:52:06
问题 I have researched this and know I'm not the first to ask but I can't seem to get my head around it. I have created a simple example that I think will help me crack it if someone can provide the missing link! I have a table of areas that contains continents and countries in a hierarchy. I also have a table of places that contains cities and landmarks in a hierarchy. This table contains an area id column to join to the areas table. create table areas ( id NUMBER not null, name VARCHAR2(200) not

Fetching all parents in simple way with PostgreSQL

旧街凉风 提交于 2020-01-03 04:30:07
问题 I have a table with a hierarchical structure: _________ |Plans |_____________________________________________ |-------------------------------------------------------| | id | parent | plan_name | description | |-------------------------------------------------------| | 1 0 Painting bla..bla | | 2 1 Shopping bla..bla | | 3 1 Scheduling bla..bla | | 4 2 Costumes bla..bla | | 5 2 Tools bla..bla | | 6 2 Paints bla..bla | |_______________________________________________________| I want to list all

SQL select descendants of a row

我与影子孤独终老i 提交于 2020-01-02 05:50:35
问题 Suppose a tree structure is implemented in SQL like this: CREATE TABLE nodes ( id INTEGER PRIMARY KEY, parent INTEGER -- references nodes(id) ); Although cycles can be created in this representation, let's assume we never let that happen. The table will only store a collection of roots (records where parent is null) and their descendants. The goal is to, given an id of a node on the table, find all nodes that are descendants of it. A is a descendant of B if either A 's parent is B or A 's

Recursive query for parent child hierarchy. Get descendants from a top node

依然范特西╮ 提交于 2019-12-31 04:26:04
问题 I have a table that stores hierarchy data in parent child format with one top node. Multiple levels, and each parent having multiple children. How can I write a recursive query to select only the parent child rows from a particular node down to the last child? Example table Parent|child 1 |2 1 |3 2 |4 2 |5 3 |6 3 |7 6 |8 How can I retrieve only rows from node 3 and all its descendants? 回答1: If your DBMS is SQL Server you can accomplish this through Common Table Expressions (CTE) using

Get the paths from a database of points in sql

不打扰是莪最后的温柔 提交于 2019-12-31 03:54:14
问题 Consider that I have a table of points, each point has 2 coordinates. For example: Source | Destination 1 | 2 2 | 3 3 | 7 5 | 7 9 | 12 I'd like to write a query in SQL that gives me the following: All the paths that these points make. Each path must have connected points, as in the next point's source coordinate is the same as the previous point's destination point. a path can't be cyclic. For example, in the table above, running the query should return 3*paths: (1,2) (2,3) (3,7) (5,7) (9,12)