Solved

Include table name in query results

Posted on 2008-10-31
3
434 Views
Last Modified: 2012-05-05
I have several tables that are all filled with basically the same information but each for a different school.  I need to pull a few pieces of information from each so that I can do some comparison analysis.  The problem is that I need to know which table each result was pulled from so that I know which school it pertains to.  Is there any way to include the actual table name in the results of the query?
0
Comment
Question by:Ima Jedi
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 22850395
Like this?

SELECT 'table1' AS Source, Col1, Col2, Col3
FROM table1
UNION ALL
SELECT 'table2' AS Source, Col1, Col2, Col3
FROM table2
UNION ALL
SELECT 'table3' AS Source, Col1, Col2, Col3
FROM table3
0
 
LVL 5

Expert Comment

by:JBart_17
ID: 22850415
select 'School1',fieldA, fieldB from table1
select 'School2',fieldA, fieldB from table2
hardcode it in
0
 

Author Closing Comment

by:Ima Jedi
ID: 31512050
Thank you Patrick!  That works perfectly.  It figures that it would be such a simple solution.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

11 Experts available now in Live!

Get 1:1 Help Now