Solved

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

Posted on 2013-05-19
5
472 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

832 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