Avatar of phman1275
phman1275

asked on 

SQL Query

I need some help for the SQL Query. I have a loan table (1st lien and 2nd lien). If the loan program is 'Bond' for the 1st lien then I need to change 2nd lien loan program to 'Bond'.

Example:

Guid        LinkGuid     LoanNumber     LienType         ProgramCode
{f1244}  {2077a}       123456789      First Lien         Bond
{2077a} {f1244}        567345673      Second Lien    C30

Result
Guid        LinkGuid     LoanNumber     LienType         ProgramCode
{f1244}  {2077a}       123456789      First Lien         Bond
{2077a} {f1244}        567345673      Second Lien    Bond <-- should change to 'Bond'
DatabasesMicrosoft SQL ServerSQL

Avatar of undefined
Last Comment
phman1275
SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland 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
ASKER CERTIFIED SOLUTION
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 phman1275
phman1275

ASKER

Thank you Slightwv. what about the following situation?

Example:

Guid        LinkGuid     LoanNumber     LienType         ProgramCode
{f1244}  {2077a}       123456789      First Lien         Bond
{2077a} {f1244}        567345673      Second Lien    C30
{4ertw}  {w234r}       342342342     First Lien          C15
{w234r} {4ertw}        567834566     Second Lien     C30

Result
Guid        LinkGuid     LoanNumber     LienType         ProgramCode
{f1244}  {2077a}       123456789      First Lien         Bond
{2077a} {f1244}        567345673      Second Lien    Bond <-- should change to 'Bond'
{4ertw}  {w234r}       342342342     First Lien          C15
{w234r} {4ertw}        567834566     Second Lien     C30  <-- no change
Avatar of phman1275
phman1275

ASKER

Never mind Slightwv.
Avatar of phman1275
phman1275

ASKER

Sorry Thomas, cannot use update.

Thank you Slightwv. You answer work very well.
Hi,

This modified  update should work

UPDATE  t1
SET t1.ProgramCode = t2.ProgramCode
FROM myloantable t1
INNER JOIN myloantable t2 on t2.Guid = t1.LinkGuid
WHERE t2.LienType = 'First Lien'
and t1.LienType = 'Second Lien'
and t1.ProgramCode = 'C30'

Open in new window


Tested on dbfiddle see here

Regards,
    Tomas Helgi
>>and t1.ProgramCode = 'C30'

I believe the intent is to NOT hard-code values.  They can be anything.
SOLUTION
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 phman1275
phman1275

ASKER

Hi Tomas,

Thank you so much. Your code is working. That is exactly what I am looking for.
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