Solved

Get the table name in Union Select too?

Posted on 2014-04-28
4
528 Views
Last Modified: 2014-04-28
Hello,

I am trying to query several tables that have the same 3 columns and combine the results like this.

Select col1,col2,col3 from tblA
union
Select col1,col2,col3 from tblB
union
Select col1,col2,col3, from tblC

From
The end result who not only show me the union of three columns form all the tables but also - which is there source table as well like this

Col1 | col2 | col3 | myTableName
Er | ty | rt } tblA
Er | ty | rt } tblB
0
Comment
Question by:Rayne
[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
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
plusone3055 earned 400 total points
ID: 40027579
yes
Select col1,col2,col3, Tbla as tablename  from tblA
union all
Select col1,col2,col3, tblb as tablename from tblB
union all
Select col1,col2,col3, tblc as tablename from tblC
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 100 total points
ID: 40027580
Select col1,col2,col3, 'tblA' AS table_name from tblA
union all
Select col1,col2,col3, 'tblB' from tblB
union all
Select col1,col2,col3, 'tblC' from tblC
order by col1, col2, col3
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40027583
hehehe I won by 11 seconds LOL
0
 

Author Closing Comment

by:Rayne
ID: 40027828
thank you both :)
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

729 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