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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:41:59+00:00 2026-06-04T09:41:59+00:00

I have this join: UPDATE Table_Name SET Change = isnull(tab2.Value,0) – tab1.Value FROM (SELECT

  • 0

I have this join:

UPDATE Table_Name
SET Change = isnull(tab2.Value,0) - tab1.Value 
FROM 
    (SELECT Date,ID,ID,FileName,Value FROM Table_Name WHERE FileName = 'x' AND Date = '2012-05-17') tab1
LEFT OUTER JOIN 
    (SELECT Date,ID,TradeID,FileName,Value FROM Table_Name WHERE FileName = 'x' AND Date = '2012-05-18')  tab2 
ON tab1.FileName = tab2.FileName AND 
tab1.ID = tab2.ID  AND 
tab1.ID = tab2.ID

As you can see, it is a left outer join. However, when I have data for May 17th and none for May 18th, the value inserted should be -17th.Value (because the general calculation is 18th.Value – 17th.Value and 18th.Value would be zero).

If I put a select statement just below the insert part (for debugging) this shows correctly, however, when I remove the SELECT statement and do the calculation in the SET part, it does not work. I end up with values for Change of null, where a match for the 18th of May could not be found.

EDIT: I should add I am unsure whether I need LEFT JOIN or LEFT OUTER JOIN. I wish to return all rows from tab1 and if it does not exist in tab2 the Change value should be -tab1.Value, as opposed to tab2.Value – tab1.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-06-04T09:42:00+00:00Added an answer on June 4, 2026 at 9:42 am

    DB2 doesn’t allow UPDATEs (or DELETEs) to contain any joins in the main table-reference clause, which to my mind prevents certain types of issues. Yes, I’m aware you’re on SQL Server, which is why you’re attempting this – I just tend to write things with DB2 constraints in mind, is all.

    Assuming that you want to update rows for the 17th, I believe the following should work. Otherwise, please specify which rows you actually want updated.

    UPDATE Table_Name 
    SET change = -value + COALESCE((SELECT nxt.value
                                    FROM Table_Name nxt
                                    WHERE nxt.fileName = table_name.fileName
                                    AND nxt.id = table_name.id
                                    AND nxt.scenarioId = table_name.scenarioId
                                    AND nxt.cobDate = '2012-05-18'), 0)
    WHERE fileName = 'GBP.csv'
    AND cobDate = '2012-05-17'
    

    EDIT:
    Corrected errors for above.

    However… from the sound of things, you may be attempting to update rows that don’t exist; specifically, the ‘result needs to entered for the 18th’. If what you’re doing is attempting to update those rows for the 18th that do exist, or insert them if they do not, you’re going to need two statements (or maybe a MERGE? Except I don’t have them in my version, so I can’t help you with that):

    UPDATE Table_Name
    SET change = value - COALESCE((SELECT prev.value
                                   FROM Table_Name prev
                                   WHERE prev.fileName = table_name.fileName
                                   AND prev.id = table_name.id
                                   AND prev.scenarioId = table_name.scenarioId
                                   AND prev.cobDate = '2012-05-17'), 0)
    WHERE fileName = 'GBP.csv'
    AND cobDate = '2012-05-18';
    
    INSERT INTO Table_Name (id, scenarioId, fileName, cobDate, value, change) 
                            SELECT id, scenarioId, fileName, '2012-05-18', 0, -value
                            FROM Table_Name
                            WHERE fileName = 'GBP.csv'
                            AND cobDate = '2012-05-17'
                            AND NOT EXISTS (SELECT '1'
                                            FROM Table_Name nxt
                                            WHERE nxt.fileName = table_name.filename
                                            AND nxt.id = table_name.id
                                            AND nxt.scenarioId = table_name.scenarioId
                                            AND nxt.cobDate = '2012-05-18');
    

    You MUST run them in that order.
    You MUST ALSO lock the table for the combined run of both statements, have some other method to ensure that no rows containing relevant data are modified (INSERTed, UPDATEd, DELETEd), or be able to tell which rows were not touched.

    If this isn’t the behavior you want, please modify your question to be more clear on what you’re trying to accomplish.

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

Sidebar

Related Questions

I have this query: SELECT * FROM sample INNER JOIN test ON sample.sample_number =
I have this query in Mysql SELECT z.virtuemart_product_id,z.product_name,d.product_price FROM x5lui_virtuemart_products x inner join x5lui_virtuemart_products_en_gb
i have this SELECT COUNT(1) cnt, a.auther_id FROM `posts` a LEFT JOIN users u
I have this query: SELECT DISTINCT IM.EDIFICIOS_ID, TI.TITULAR FROM IMPORTACION IM INNER JOIN I_EDIFICIO
I have this working query: $q = $this->db->query('SELECT u.name FROM users u JOIN user_group
I have this Query: Select DFC_NOMBRE_CAMPO, DFC_TITULO, TDT_DATO_MOTOR, DFC_LONGITUD FROM MEM_DEFINICIONES_CAMPOS JOIN MEM_TIPOS_DATOS ON
I have this expression: var result = from pav in ProductAttributes join id in
I have a MySQL query like this: SELECT cp.plan_name, cp.plan_time FROM courses c INNER
I have written this code to join ArrayList elements: Can it be optimized more?
I have this getopt: GetOptions( GetOptions (library=s => \@libfiles); @libfiles = split(/,/,join(',',@libfiles)); help =>

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.