?
Solved

SQL Querys

Posted on 2012-03-28
3
Medium Priority
?
275 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

752 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