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 8986467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:33:26+00:00 2026-06-15T21:33:26+00:00

I have a table like this: table item ( id int, quantity float, father

  • 0

I have a table like this:

table item
(
   id    int,
   quantity float,
   father int, -- refer to item itself in case of subitem
)

I need to sum al quantity plus sons quantity like this way:

select i.id, max(i.quantity)+sum(ft.quantity) as quantity
from item i
left join item ft on ft.id=i.id
group by i.id

My trouble is because relationship between father-son is recursive so I would like to sum also his grandfather quantity and so on… and i don’t know the maximum deepness, than I can not join many times.

What can i do?
Thank you.

  • 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-15T21:33:27+00:00Added an answer on June 15, 2026 at 9:33 pm

    You have to use a recursive CTE. Somthing like this:

    ;WITH FathersSonsTree
    AS
    (
      SELECT Id, quantity, 0 AS Level
      FROM Items WHERE fatherid IS NULL
      UNION ALL
      SELECT c.id, c.quantity, p.level+1
      FROM FathersSonsTree p
      INNER JOIN items c ON c.fatherid = p.id
     ), ItemsWithMaxQuantities
    AS
    (
      SELECT *,
      ROW_NUMBER() OVER(PARTITION BY level 
                        ORDER BY quantity DESC) rownum
      FROM FathersSonsTree
      )
    SELECT 
      ID,  
      (SELECT MAX(Quantity) 
       FROM FathersSonsTree t3 
       WHERE t3.level = t1.level
      ) + 
      ISNULL((SELECT SUM(t2.Quantity) 
       FROM FathersSonsTree t2
       WHERE t1.level - t2.level = 1), 0)
    FROM FathersSonsTree t1
    ORDER BY ID;
    

    SQL Fiddle Demo

    This will give you something like:

    | ID | QUANTITY |
    -----------------
    |  1 |       10 |
    |  2 |       20 |
    |  3 |       20 |
    |  4 |       20 |
    |  5 |       32 |
    |  6 |       32 |
    |  7 |       32 |
    |  8 |       32 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this: Item { int ItemID int ParentID string Name
I have a bill of materials table that is set up like this: item
I have a table that looks like this: ------------------------------------------------------------------- CUSTNUM (INT), ITEMNUM (INT), MONTH
I have a table that looks like this CREATE TABLE `purchases` ( `id` INT(10)
I have a db-table that looks like this: CREATE TABLE ItemPrice ( [Identity] INT
I have a table that looks like this: Id (PK, int, not null) ReviewedBy
I have a tablestructure like this: Table entity ( otherEntity_id int // primarykey id
I currently have a table like this: Stuff ---------- StuffId identity int not null
I have a table like this: create table t1 { person_id int, item_name varchar(30),
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives

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.