?
Solved

Joining on a SubQuery?

Posted on 2008-10-10
2
Medium Priority
?
250 Views
Last Modified: 2012-05-05
I have following query:

select case when parentid like '%-%' then substring(ParentId, 0, charindex('-',ParentId,1)) else parentid end as Parent from assets )

This pulls back all the ParentID's for my assets.

I then have:


select A.ID, A.AccountID, A.SerialNumber,D.ContactName,A.AssetCategory  from Assets as A
left join debitors d on d.accountid = a.parentid  
where
(A.ParentId LIKE '%-l004-%')
OR  (A.ParentId LIKE '%l004-%')
OR  (A.ParentId LIKE '%-l004%')
OR  (A.ParentId LIKE 'l004')
OR  (A.AccountId LIKE 'l004')
GROUP BY A.ID, A.AccountID, A.SerialNumber,A.ParentID,A.AssetCategory,d.ContactName,D.AccountID

But due to how I have built up the ParentID, I am left with alot of NULL going into D.ContactName. What I was hoping to do was use the SubQuery to fill in those NULL's but am not sure how I do it.

0
Comment
Question by:directxBOB
[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 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 22685908
just a guess into the blue:

select A.ID, A.AccountID, A.SerialNumber,D.ContactName,A.AssetCategory  from Assets as A 
left join debitors d on d.accountid = case when parentid like '%-%' then substring(ParentId, 0, charindex('-',ParentId,1)) else parentid end 
where 
(A.ParentId LIKE '%-l004-%') 
OR  (A.ParentId LIKE '%l004-%') 
OR  (A.ParentId LIKE '%-l004%')
OR  (A.ParentId LIKE 'l004') 
OR  (A.AccountId LIKE 'l004') 
GROUP BY A.ID, A.AccountID, A.SerialNumber,A.ParentID,A.AssetCategory,d.ContactName,D.AccountID

Open in new window

0
 

Author Comment

by:directxBOB
ID: 22685930
Excellent Cheers
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 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