Solved

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

Posted on 2013-05-19
5
448 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
  • 2
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
MattSQL earned 500 total points
Comment Utility
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
Comment Utility
Thanks!
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
:)
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now