Solved

Joining on a SubQuery?

Posted on 2008-10-10
2
245 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
2 Comments
 
LVL 142

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

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