Solved

Joining on a SubQuery?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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.​
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

12 Experts available now in Live!

Get 1:1 Help Now