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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:21:45+00:00 2026-05-31T02:21:45+00:00

Sample table: N ProductName Cost Type TOTAL 1 ProductX 3 Checker 2 Product0 5

  • 0

Sample table:

 N  ProductName          Cost          Type            TOTAL

 1  ProductX             3             Checker  
 2  Product0             5             TOY
 3  Product1             5             TOY
 4  Product2             8             TOY

Rows 2,3,4 Need Cost Of Type = Checker + current row cost into the TOTAL column. So row 2 would be 8, 3 would be 8 and 4 would be 11

 5  ProductZ             10            Checker         
 6  Product3             5             TOY
 7  Product4             9             TOY
 8  Product5             18            TOY
 9  Product6             25            TOY

Likewise, these need Cost of ProductZ added to their totals. So Row 6 would be 15, 7 would be 19, and so on. They cannot go back and reference ProductY at row 1.

The datasets are not always the same, and there may be more “Checker” type items in the dataset.

When there’s only one, it works really well, obviously. But when there’s more than one I have issues where the products total is wrong because it’s using the wrong “Checker” value.

  • 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-31T02:21:46+00:00Added an answer on May 31, 2026 at 2:21 am

    I think this query works.

    WITH toysAndCheckers AS
    (
    SELECT n AS toy, (SELECT top 1 lowerCheckers.n From sampleTable AS lowerCheckers
            WHERE lowerCheckers.N < theToys.N
            AND lowerCheckers.[type] = 'Checker'
            ORDER by n desc) AS matchingChecker
    
    FROM sampleTable AS theToys
    WHERE theToys.[type] = 'TOY'
    ),
    toyCostAdjustments AS
    (SELECT toy, cost AS checkerAdjustment
    FROM toysAndCheckers 
    JOIN sampleTable
        ON MatchingChecker = n
    )       
    MERGE sampleTable
    USING
    toyCostAdjustments
    ON n = toy
    WHEN MATCHED THEN UPDATE
    SET total = cost + CheckerAdjustment
    ;
    

    The first CTE, “toysAndCheckers”, gets for each toy the N of the CHECKER row that is highest while still being lower than the TOY in question. The second one just replaces the N of the checker with the cheker’s cost. Then you have a straightforward merge. Assuming the N column is unique, you’ll never have multiple matches.

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

Sidebar

Related Questions

For my question lets consider the following sample table data: ProductID    ProductName    Price   Category 1                Apple                 5.00       Fruits
## Sample Table ## Here are some particulars concerning proposed problem. ROWID = _id;
My SAMPLE table has the following five columns: sample_id (PK) (NUMBER) sampled_on (DATE) received_on
Data: Here is sample table(TableA) data ID StartTime EndTime 1 2012-03-22 06:00:00.000 2012-03-22 06:30:00.000
I'm using Hibernate 3.3.1 and am following along in modelling this sample table structure
I have a table Sample and another SampleLog With these structure I want to
How can I concatenate two char columns within a perform screen? example: table sample
Here is the sample data for test table I have [childId] [parentId] [present] 1
I have a sample file like the following: CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 (
Here is sample data <table class=sparql border=1> <tr> <th>abstract</th></tr> <tr> <td> Cologne is Germany&#39;s

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.