Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9270619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:24:56+00:00 2026-06-18T15:24:56+00:00

I have something like this: CREATE TABLE categories ( id varchar(250) PRIMARY KEY, name

  • 0

I have something like this:

CREATE TABLE categories (
id varchar(250) PRIMARY KEY,
name varchar(250) NOT NULL,
parentid varchar(250)
);

CREATE TABLE products (
id varchar(250) PRIMARY KEY,
name varchar(250) NOT NULL,
price double precision,
category varchar(250) NOT NULL
);

INSERT INTO categories VALUES ('1', 'Rack', '');
INSERT INTO categories VALUES ('2', 'Women', '1');
INSERT INTO categories VALUES ('3', 'Shorts', '2');

INSERT INTO products VALUES ('1', 'Jean', 2.99, '3');
INSERT INTO products VALUES ('2', 'Inflatable Boat', 5.99, '1');

Now, if I wanted to see the total price of products for each category, I could do something like this:

SELECT
categories.name,
SUM(products.price) AS CATPRICE
FROM
categories,
products
WHERE products.category = categories.id
GROUP BY categories.name
;

Which produces output:

  name  | catprice
--------+----------
 Rack   |     5.99
 Shorts |     2.99
(2 rows)

But notice that “Shorts” is an ancestor of “Rack”. I want a query that will produce output like this:

  name  | catprice
--------+----------
 Rack   |     8.98
(1 row)

So that all product prices are added together under the root category. There are multiple root categories in the category table; only one has been shown for simplicity.

This is what I have thus far:

-- "nodes_cte" is the virtual table that is being created as the recursion continues
-- The contents of the ()s are the columns that are being built
WITH RECURSIVE nodes_cte(name, id, parentid, depth, path) AS (
-- Base case?
 SELECT tn.name, tn.id, tn.parentid, 1::INT AS depth, tn.id::TEXT AS path FROM categories AS tn, products AS tn2 
 LEFT OUTER JOIN categories ON tn2.CATEGORY = categories.ID
 WHERE tn.parentid IS NULL
UNION ALL
-- nth case
 SELECT c.name, c.id, c.parentid, p.depth + 1 AS depth, (p.path || '->' || c.id::TEXT) FROM nodes_cte AS p, categories AS c, products AS c2 
 LEFT OUTER JOIN categories ON c2.CATEGORY = categories.ID
 WHERE c.parentid = p.id
)
SELECT * FROM nodes_cte AS n ORDER BY n.id ASC;

I have no clue what I’ve done wrong. The above query returns zero results.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:24:57+00:00Added an answer on June 18, 2026 at 3:24 pm

    Your recursive query is off by a little. Give this a try:

    EDIT — To make this work with the SUM, use this:

    WITH RECURSIVE nodes_cte(name, id, id2, parentid, price) AS (
    -- Base case?
     SELECT c.name, 
      c.id, 
      c.id id2,
      c.parentid, 
      p.price
     FROM categories c
        LEFT JOIN products p on c.id = p.category
     WHERE c.parentid = ''
    UNION ALL
    -- nth case
     SELECT n.name, 
      n.id, 
      c.id id2,
      c.parentid, 
      p.price
     FROM nodes_cte n
      JOIN categories c on n.id2 = c.parentid
      LEFT JOIN products p on c.id = p.category
    )
    SELECT id, name, SUM(price) FROM nodes_cte GROUP BY id, name
    

    And here is the Fiddle: http://sqlfiddle.com/#!1/7ac6d/19

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like this: create table account ( id int identity(1,1) primary key,
I have a table with a composite Primary Key, arranged something like this: CREATE
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
using a Oracle 10g db I have a table something like this: create table
I have a fairly complex query that looks something like this: create table Items(SomeOtherTableID
I have a table that looks something like this: CREATE TABLE A ( A_UNIQUE_ID
I have a MySQL table like this CREATE TABLE IF NOT EXISTS `vals` (
I have a table which looks like this: CREATE TABLE `library_mix` ( `lib_mix_id` VARCHAR(64)
Given a structure like this: CREATE TABLE reference_table ( reference_table_key numeric NOT NULL, reference_value
I have a table with a couple foreign keys, something like this: CREATE TABLE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.