Link to home
Start Free TrialLog in
Avatar of Addie Baker
Addie BakerFlag for United States of America

asked on

Need to bring back another column from a SQL Query I got help with the other day

I asked this question the other day and got my answer and worked great.
https://www.experts-exchange.com/questions/28131755/Need-help-with-SQL-Query-comparing-a-matched-row.html

Now i want to bring back another column to IMPORTS tables

This is the working query

UPDATE imports
SET imports.AMIPartNumber = coalesce(apn.Item,asn.Item,aqn.Item,arn.Item)
FROM imports as I 
LEFT JOIN jdsubs as PN ON I.OEMPartNumber = PN.OEMPartNumber 
LEFT JOIN amipartnumbers as APN ON PN.OEMPartNumber = APN.OEMItem 
LEFT JOIN jdsubs as SN ON I.OEMPartNumber = SN.OEMSubNumber 
LEFT JOIN amipartnumbers as ASN ON SN.OEMPartNumber = ASN.OEMItem
LEFT JOIN ihsubs as QN ON I.OEMPartNumber = QN.OEMPartNumber 
LEFT JOIN amipartnumbers as AQN ON QN.OEMPartNumber = AQN.OEMItem 
LEFT JOIN ihsubs as RN ON I.OEMPartNumber = RN.OEMSubNumber 
LEFT JOIN amipartnumbers as ARN ON RN.OEMPartNumber = ARN.OEMItem
;

select
*
from imports

Open in new window


Here are the table layouts
imports
----------------
OEMPartNumber  | AMIPartNumber
AR77530        |
AR12345        |

JDSubs
---------------------------
OEMPartNumer    | OEMSubNumber
AR65123         | AR77530
AR12345         | AR56242

AMI
---------------------------
Item            | OEMItem   | Description
AMAR65123       | AR65123   | Axle
AMAR56242       | AR12345   | Spindle

Open in new window


Now i want to bring back AMI.Description and put in Imports.AMIDescrtiption
ASKER CERTIFIED SOLUTION
Avatar of Matt Bowler
Matt Bowler
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Addie Baker

ASKER

Thanks!
:)
just like adding ihsubs into the query (which you did), all one has to do is mimic the coalesce on item for the description... trying is learing
I thought about trying that. I was afraid that was too easy and wouldn't work. So the joins are setting the relationships and the coalesces are telling what info to bring back from those relationships?
yes, correct

coalesce simply says,
if something is null, then do the next thing
if that is also null do the next thing
if that is also null do the next thing
... etc

because there are many left joins, it is possible to have nulls
and that is why coalesce is so helpful here

make sense?

if you thought about doing it - do it - it won't bite :)

it would be much clearer is you look again at this query if you run it as a query, you will see many nulls in columns, but for each row there will be some value.

satisfy the curiosity by looking :)