Solved

Joining on a SubQuery?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql join/ assign small # first 10 83
Oracle query output question 4 36
the whoisactive update 12 39
Make query more efficient 1 16
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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 this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

25 Experts available now in Live!

Get 1:1 Help Now