Solved

Joining on a SubQuery?

Posted on 2008-10-10
2
247 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 500 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

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. …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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