Solved

union 2 datasets to fill in empty fields

Posted on 2011-02-15
6
345 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Conditional WHERE clause 3 38
SSIS Standard Template for Reuse by Business Units 12 52
denied execute as 13 32
Can a Trigger trigger a Trigger? 4 24
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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