Solved

SQL Querys

Posted on 2012-03-28
3
265 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 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

12 Experts available now in Live!

Get 1:1 Help Now