Solved

union 2 datasets to fill in empty fields

Posted on 2011-02-15
6
346 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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