[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Querys

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

873 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