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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:53:32+00:00 2026-06-08T10:53:32+00:00

I need to update some records here are the table structures Table A is

  • 0

I need to update some records here are the table structures

Table A is the main one we need to update some records
A(Id,DataYear0,DataYear1,DataYear3)

Table B has some other than data that we require
B(Id,Year,DataYear)

Example data for Table B:
B(abc,1950,1.25)
B(abc,1951,1.29)
..
..
B(abc,2008,1.67)
B(abc,2009,1.23)
B(abc,2010,1.52)
B(abc,2011,1.78)
B(abc,2012,NULL)

With 2012 as the current Year, I need to update the data values of the previous three years(i.e, 2011,2010,2009) in Table A

So, table A needs to be updated as A(abc,1.78,1.52,1.23) for the above example. Similarly other records need to be updated.

Id is the primary key on both tables.

The query I have so far is

UPDATE A SET
    DataYear0 = CASE WHEN Year+1 = YEAR(GETDATE()) THEN DataYear END,
    DataYear1 = CASE WHEN Year+2 = YEAR(GETDATE()) THEN DataYear END,
    DataYear2 = CASE WHEN Year+3 = YEAR(GETDATE()) THEN DataYear END
FROM A
LEFT JOIN B
    ON A.Id=B.Id

This did not work, because I think it is returning extra year records apart from the years that we want i.e., just the last three years

UPDATE A SET
   DataYear0 = CASE WHEN Year+1 = YEAR(GETDATE()) THEN DataYear END,
   DataYear1 = CASE WHEN Year+2 = YEAR(GETDATE()) THEN DataYear END,
DataYear2 = CASE WHEN Year+3 = YEAR(GETDATE()) THEN DataYear END
FROM A
LEFT JOIN B
    ON A.Id=B.Id
GROUP BY Id,Year,DataYear
HAVING ((YEAR(GETDATE())-3) <= Year) AND (Year <> YEAR(GETDATE()))

But still the data is not being updated.

Tried using CTE

;WITH cte AS
(
 SELECT 
    A.Id
    ,B.Year
    ,B.DataYear 
    ,ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Year DESC) as row
 FROM A
 LEFT JOIN B
    ON A.Id=B.Id
 GROUP BY Id,Year,DataYear
 HAVING ((YEAR(GETDATE())-3) <= Year) AND (Year <> YEAR(GETDATE()))
 )


UPDATE
    A.DataYear0 = CASE WHEN row=1 THEN DataYear END
    ,A.DataYear1 = CASE WHEN row=2 THEN DataYear END
    ,A.DataYear2 = CASE WHEN row=3 THEN DataYear END
FROM A
LEFT JOIN cte
    ON A.Id = cte.Id

The data is still not updated. I would appreciate any suggestions that anyone might have. Many thanks in advance.

  • 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-08T10:53:34+00:00Added an answer on June 8, 2026 at 10:53 am

    You could do something like this:

    UPDATE a
        SET DataYear0 = COALESCE(d0.DataYear,DataYear0),
            DataYear1 = COALESCE(d1.DataYear,DataYear1),
            DataYear2 = COALESCE(d2.DataYear,DataYear2)
    FROM A a
    LEFT JOIN B d0 ON d0.Id = a.Id and d0.[Year] = YEAR(GETDATE())-1
    LEFT JOIN B d1 ON d1.Id = a.Id and d1.[Year] = YEAR(GETDATE())-2
    LEFT JOIN B d2 ON d2.Id = a.Id and d2.[Year] = YEAR(GETDATE())-3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to update some entry in my data base using jdo in java
I am using MySQL as database. I need to update some data. However the
I need to use scp update some directory at another server. It is similar
i have ios application with xcdatamodeld, i want update some data so need a
I have a case where I need to update a jqgrid based on some
I need some advice on how to successfully update mutiple rows in my database
Here's some background info. I have three MySQL tables (all InnoDB). The first table
My index page have some records where each record have one or more images
I need solution of auto hide/remove table-rows of deleted records. Suppose a table-body element
I have some records that were imported to SQL. One of the fields 'FieldToChage'

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.