Solved

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

Posted on 2013-05-19
5
486 Views
Last Modified: 2013-05-20
I asked this question the other day and got my answer and worked great.
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_28131755.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
0
Comment
Question by:bignadad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
Matt Bowler earned 500 total points
ID: 39179787
Try:

UPDATE imports
SET imports.AMIPartNumber = coalesce(apn.Item,asn.Item,aqn.Item,arn.Item),
Imports.AMIDescrtiption = coalesce(apn.Description,asn.Description,aqn.Description,arn.Description)
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
0
 
LVL 2

Author Closing Comment

by:bignadad
ID: 39179828
Thanks!
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39179970
:)
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
0
 
LVL 2

Author Comment

by:bignadad
ID: 39180712
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?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39180736
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 :)
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question