Solved

SQL Querys

Posted on 2012-03-28
3
268 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
3 Comments
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 150 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 350 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now