Avatar of tablaFreak
tablaFreakFlag for United States of America

asked on 

SQL Update with Join

Hi Experts,

Can anyone tell me why this query isn't working? I want to update rows with null 'Effective_Date' with related records with 'Effective_Date' not null.

What would you suggest?

Thanks,
Steve


Update si2
Set si2.Effective_Date = si.Effective_Date
from Specifications_Import2 si
Left join
      (Select Macola_Number, Spec_Version_Num, Effective_Date from Specifications_Import2 si
            where Effective_Date IS NULL) si2
       on si.Macola_Number = si2.Macola_Number and si.Spec_Version_Num = si2.Spec_Version_Num
where si.Effective_Date is not null
Microsoft SQL Server

Avatar of undefined
Last Comment
tablaFreak
SOLUTION
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of arnold
arnold
Flag of United States of America image

I do not think you can update a non-existent table.
si2 is the result of this query:
(Select Macola_Number, Spec_Version_Num, Effective_Date from Specifications_Import2 si
            where Effective_Date IS NULL) si2

What do you get from
select si.Macola_Number, si.Spec_Version_Num, si.Effective_date,
            si2.Macola_Number, si2.Spec_Version_Num, si2.Effective_date
            from Specifications_Import2 si
Left join
     Specifications_Import2 si2
            on si.Macola_Number = si2.Macola_Number and si.Spec_Version_Num = si2.Spec_Version_Num (and si.Effective_date is Null )
where si.Effective_Date is not null

with and without the items in parenthesis

If I understand it, you have duplicates (but not completely as some have the effective_date as null) in your table and you seem to try to actually complete the duplicate that should be there before you go through a process to remove duplicates (you want to be identical...)?
Will this version work for you?
UPDATE s1
SET s1.Effective_Date = (SELECT Effective_Date 
			FROM Specifications_Import2 s2
			WHERE s1.Macola_Number = s2.Macola_Number and s1.Spec_Version_Num = s2.Spec_Version_Num and s2.Effective_Date is not null)
FROM Specifications_Import2 s1
WHERE s1.Effective_Date IS NULL

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of tablaFreak
tablaFreak
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of tablaFreak
tablaFreak
Flag of United States of America image

ASKER

Worked out the solution with help from the guys.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo