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

  • Home
  • SEARCH
  • 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 7565031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:02:46+00:00 2026-05-30T14:02:46+00:00

I have a table with next structure Id|State|Quan|SellQuan|LastId 3 |2 | 5 | 0

  • 0

I have a table with next structure

Id|State|Quan|SellQuan|LastId
3 |2    | 5  | 0      |2
2 |3    |10  | 5      |1
1 |3    |15  | 5      |NULL

LastId is referenced on Id field, I need calulate value on next step: take Quan from Id=3 then add all sellquan with Id referenced on previous LastId. thats meen
Id=3 – lastId = 2 => take value from Id=2 field sellquan then chek if lastId is not null
.In my ecample LastId = 1 => Take Id=1 value SellQaun
Result: 5 + 5+ 5 = 15

  • 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-05-30T14:02:47+00:00Added an answer on May 30, 2026 at 2:02 pm

    I am not really sure what you want. But I will give it a try.

    First some test data:

    DECLARE @tbl TABLE(Id INT,State INT,Quan INT,SellQuan INT,LastId INT)
    INSERT INTO @tbl
    VALUES
        (3,2,5,0,2),
        (2,3,10,5,1),
        (1,3,15,5,NULL)
    

    Then the recursive function like this:

    DECLARE @Id INT=1
    ;WITH CTE(Id,parentId,SellQuan,topParent)
    AS
    (
        SELECT
            tbl.Id,
            tbl.LastId AS parentId,
            (CASE 
                WHEN EXISTS(SELECT NULL FROM @tbl AS tbl2 WHERE tbl2.LastId=tbl.Id)
                THEN tbl.SellQuan
                ELSE tbl.Quan
            END) AS SellQuan,
            tbl.Id AS topParent
        FROM
            @tbl AS tbl
        WHERE
            tbl.Id=@Id
        UNION ALL
        SELECT
            tbl.Id,
            tbl.LastId AS parentId,
            (CASE 
                WHEN EXISTS(SELECT NULL FROM @tbl AS tbl2 WHERE tbl2.LastId=tbl.Id)
                THEN tbl.SellQuan
                ELSE tbl.Quan
            END) AS SellQuan,
            CTE.topParent AS topParent
        FROM
            @tbl AS tbl
            JOIN CTE
                ON CTE.Id=tbl.LastId
    )
    SELECT
        CTE.topParent AS id,
        SUM(CTE.SellQuan) AS SellQaunResult
    FROM
        CTE
    GROUP BY
        CTE.topParent
    

    Hope this helps

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

Sidebar

Related Questions

I have a MySQL database table with this structure: table id INT NOT NULL
I have the following function. $(function() { $(.sectionHeader:gt(0)).click(function() { $(this).next(.fieldset).slideToggle(fast); }); $(img[alt='minimize']).click(function(e) { $(this).closest(table).next(.fieldset).slideUp(fast);
I have a table story_category in my database with corrupt entries. The next query
I have table with some fields that the value will be 1 0. This
i have table structure with 3 colums (column1, column2, column3) and i want to
I have the following table structure. I want to select distinct CustomerId and CustomerName
I have a hierarchical structure stored in a table. Each element has a pointer
I have a request of translation: (table structure: line_id lang (en or ru) text
I have to implement charts in my application. Suppose i have a table structure
I have a many to many table structure called PropertyPets. It contains a dual

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.