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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:31:22+00:00 2026-06-13T18:31:22+00:00

I have some SQL written up to address my problem but it is not

  • 0

I have some SQL written up to address my problem but it is not quite ready. I have been attempting different queries for days now without much luck. FYI I inherited this DB so it may be a bit more advanced than what I am used to.

There is a many-to-many relationship between Formula and Color using the bridge table FormulaColor. FormulaColor has the columns (FormulaId, ColorId, Percentage).

My Business problem, we are converting all the formulas that use color A with the colors B(1%), C(2.8%) and D(96.2%). So if a Formula currently uses the color A as 50% of the formula then I would add a row for that formula with the B color and a percentage of .5%, a row for C color with a percentage of 1.4% and a row for D color with a percentage of 48.1%.

The problem though, is a given formula that contains color A, may also already contain one or more of colors B, C or D. In that case I need to update the percentage by simply adding what was calculated above.

The actual sql that I have so far is as follows. *Note that this is just for handling 1 of the new colors (ColorId=2594)

INSERT INTO FormulaColor (FormulaId, ColorId, Percentage) 
SELECT FormulaId, 2594 as ColorId, round((.01*FormulaColor.Percentage),2) as Percentage
FROM FormulaColor WHERE ColorId=2595;
DELETE FROM FormulaColor WHERE ColorId = 2595;

I was thinking that using the upsert method that uses @@rowscount is what I need? If so, I can’t figure out how to do the update to just add the calculated amount to the percentage.

Any help or resources is appreciated!

UPDATE

I ended up using the solution from Andriy M. In case it helps someone else out I am posting the actual SQL that I used.

DECLARE @DeletedColor int;
DECLARE @Replacement TABLE (ColorId int, Percentage float);

-- old ColorId getting replaced
SET @DeletedColor = 2595

-- new ColorId's and the percentages needed to recalculate
INSERT INTO @Replacement (ColorId, Percentage) VALUES (2594, .01)
INSERT INTO @Replacement (ColorId, Percentage) VALUES (2521, .028)
INSERT INTO @Replacement (ColorId, Percentage) VALUES (2533, .962)

SELECT
  fc.FormulaId,
  r.ColorId,
  Percentage = round(fc.Percentage * r.Percentage, 2)
INTO #FormulaColor
FROM FormulaColor fc
CROSS JOIN @Replacement r
WHERE fc.ColorId = @DeletedColor
;

UPDATE old
SET old.Percentage = old.Percentage + new.Percentage
FROM FormulaColor old
INNER JOIN #FormulaColor new
 ON old.FormulaId = new.FormulaId
AND old.ColorId   = new.ColorId
;

INSERT INTO FormulaColor (FormulaId, ColorId, Percentage)
SELECT new.FormulaId, new.ColorId, new.Percentage
FROM #FormulaColor new
LEFT JOIN FormulaColor old
 ON old.FormulaId = new.FormulaId
AND old.ColorId   = new.ColorId
WHERE old.FormulaId IS NULL
;

DELETE FROM FormulaColor WHERE ColorId = @DeletedColor;
  • 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-13T18:31:24+00:00Added an answer on June 13, 2026 at 6:31 pm

    Let @DeletedColor be the ID of the colour to delete, and @Replacement the table of the replacement colours, declared like this:

    DECLARE @DeletedColor int;
    DECLARE @Replacement TABLE (ColorId int, Percentage float);
    

    One way to approach the problem would be like this:

    1. Prepare the actual colour values to add to every formula:

      SELECT
        fc.FormulaId,
        r.ColorId,
        Percentage = fc.Percentage * r.Percentage
      INTO #FormulaColor
      FROM FormulaColor fc
      CROSS JOIN @Replacement r
      WHERE fc.ColorId = @DeletedColor
      ;
      

      This is creating a list of replacement colours for every formula containing the @DeletedColor. The percentage specified for every replacement colour in the replacement table is factored by the percentage of each formula’s @DeletedColor to form the final percentage for the replacement colour.

    2. Update existing FormulaColor colours from the just created row set:

      UPDATE old
      SET old.Percentage = old.Percentage + new.Percentage
      FROM FormulaColor old
      INNER JOIN #FormulaColor new
       ON old.FormulaId = new.FormulaId
      AND old.ColorId   = new.ColorId
      ;
      
    3. Insert the colours from the new row set to the formulas that do not contain those colours:

      INSERT INTO FormulaColor (FormulaId, ColorId, Percentage)
      SELECT new.FormulaId, new.ColorId, new.Percentage
      FROM #FormulaColor new
      LEFT JOIN FormulaColor old
       ON old.FormulaId = new.FormulaId
      AND old.ColorId   = new.ColorId
      WHERE old.FormulaId IS NULL
      ;
      

    Naturally, since you are modifying the actual table using more than one statement, it would be best to carry out the modifications in a transaction to ensure their atomicity.

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

Sidebar

Related Questions

Hi I have some software written in VB6.0 that uses an SQL Server, but
i have a problem. Some sql in my program could be written by the
I am facing a very annoying problem. I have written (in Notepad++) some SQL
Say we have some poorly written SQL that does not release temp tables when
I'm getting crazy with this problem: I have got some Oracle SQL-Reports to redesign
I have often seen the queries written in SQL in CAPITAL LETTERS . Though
I'm working with some SQL 2005 CLR code written in C#. We have recently
I have written some SQL code to create five tables with several relations(foreign keys).
Some sql is not well written. Sometimes a search costs hours in applications. When
Hai Techies, I have some stored procedure which was written in SQL server.Now i

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.