Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-05-19
5
Medium Priority
?
502 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 2000 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 49

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 49

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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
Suggested Courses

636 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