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

x
?
Solved

SQL Querys

Posted on 2012-03-28
3
Medium Priority
?
277 Views
Last Modified: 2012-03-28
Hi All

Is it possible in sql to take 3 query and return only one table?

E.g. I want one table return from a query

SELECT [DisplayName],[Email]
  FROM [contacts]
  where [Email] is not null

      SELECT [DisplayName],[Email1]
  FROM [contacts]
  where [Email1] is not null

  SELECT [DisplayName],[Email2]
  FROM .[contacts]
  where [Email2] is not null

At the moment 3 tables are return how do I merge these in to one?
0
Comment
Question by:Matb396
[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 Comments
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 600 total points
ID: 37776351
Use UNION :

SELECT [DisplayName],[Email]
  FROM [contacts]
  where [Email] is not null 
UNION
      SELECT [DisplayName],[Email1]
  FROM [contacts]
  where [Email1] is not null 
UNION
  SELECT [DisplayName],[Email2]
  FROM .[contacts]
  where [Email2] is not null 

Open in new window


http://msdn.microsoft.com/en-us/library/ms180026.aspx
0
 
LVL 11

Accepted Solution

by:
rajvja earned 1400 total points
ID: 37776365
Hi use this way

;WITH cte AS
(
SELECT [DisplayName],[Email]
  FROM [contacts]
  where [Email] is not null
UNION
      SELECT [DisplayName],[Email1]
  FROM [contacts]
  where [Email1] is not null
UNION
  SELECT [DisplayName],[Email2]
  FROM .[contacts]
  where [Email2] is not null
)

select * from cte
0
 

Author Closing Comment

by:Matb396
ID: 37776389
Thanks
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

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