?
Solved

union 2 datasets to fill in empty fields

Posted on 2011-02-15
6
Medium Priority
?
347 Views
Last Modified: 2012-05-11
Can I union 2 select statements so that similar records will combine fields, such as
select names.id, names.first, names.last, '' as address
from tblnames
union
select addrs.nameid, '' as first, '' as last, addrs.address
from addrs
and the result would be like
id   first    last     addr
1     Tom  Jones   123 Maple
2     Jon   Smith     342 1st
etc
I'm trying something similar, but, I get (i've attached my code, but, it's much different...just trying to show what's happening)
id     name     email         phone
1      Tom     1@2.com
1      Tom                         123-345
2      Jon       3@2.com
2      Jon                          121-545
Select	DPid1.WebID, Contact1.Name as DP, Contact1.Email as DPEmail, '' as GM, '' as GMEmail
From	tblsfContact as Contact1 Inner Join
		(
		SELECT	WebIDs.WebID, MIN(contact.sfContactID) as ContactID
		FROM	tblsfInstalledBase AS Prod INNER JOIN
				tblHistWebIDDetails AS WebIDs ON Prod.sfWebID = WebIDs.sfWebID LEFT OUTER JOIN
				tblsfContact AS Contact ON WebIDs.sfAccntID = Contact.sfAccntID
		WHERE	(WebIDs.Period = DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)) AND (Prod.ProductCode = '14.285.00') AND (WebIDs.HasProCare = 0) AND 
				(WebIDs.HasSearch = 0) AND (Contact.Status = 'Active') AND (Contact.Role LIKE '%Dealer Principal%') AND (Contact.Email IS NOT NULL) 
		GROUP BY WebIDs.WebID
		) as DPid1 on DPid1.ContactID = Contact1.sfContactID
UNION
Select	DPid.WebID, '' as DP, '' as DPEmail, Contact.Name as GM, Contact.Email as GMEmail
From	tblsfContact as Contact Inner Join
		(
		SELECT	WebIDs.WebID, MIN(contact.sfContactID) as ContactID
		FROM	tblsfInstalledBase AS Prod INNER JOIN
				tblHistWebIDDetails AS WebIDs ON Prod.sfWebID = WebIDs.sfWebID LEFT OUTER JOIN
				tblsfContact AS Contact ON WebIDs.sfAccntID = Contact.sfAccntID
		WHERE	(WebIDs.Period = DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)) AND (Prod.ProductCode = '14.285.00') AND (WebIDs.HasProCare = 0) AND 
				(WebIDs.HasSearch = 0) AND (Contact.Status = 'Active') AND (Contact.Role LIKE '%General Manager%') AND (Contact.Email IS NOT NULL) 
		GROUP BY WebIDs.WebID
		) as DPid on DPid.ContactID = Contact.sfContactID

Open in new window

0
Comment
Question by:avoorheis
[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
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34901093


dont use UNION, use inner join and isnull
example

select ISNULL(A.FirstName, B.FirstName) FirstName, ISNULL(A.LastName, B.LastName) LastName
from People A
inner join People B on B.ID = A.ID
0
 

Author Comment

by:avoorheis
ID: 34901121
thanks, I'll try....so, union doesn't do that, am I confused with my Access days (or just totally confused)?
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34901174
Union will just leave null spaces in between
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 50

Accepted Solution

by:
Lowfatspread earned 2000 total points
ID: 34901201
not UNION... you may need a FULL OUTER JOIN  (if data is not guaranteed to be on both tables..


select coalesce(a.id,b.nameid) as id
     , a.first, a.last, b.address
from tblnames as a
full outer join addrs as b
 on a.id=b.nameid
select addrs.nameid, '' as first, '' as last, addrs.address
from addrs

Open in new window

0
 

Author Comment

by:avoorheis
ID: 34901228
hmmmm....
I think I have something different. I'm trying to add fields to the first dataset. So,
select names.id, names.first, names.last, '' as address
from tblnames
union
select addrs.nameid, '' as first, '' as last, addrs.address
from addrs
tblnames does not have a field addrs, so, I want to "append" it on to the results, so, that the list from tblnames will also include the addresses.
0
 

Author Comment

by:avoorheis
ID: 34901311
thanks lowfat, full outer is working
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

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