Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

union 2 datasets to fill in empty fields

Posted on 2011-02-15
6
Medium Priority
?
348 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

636 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