I want to union two tables and add a flag column(1,0) indicating where they came from. Any help on how to perform this SQL?
Asked
Active
Viewed 405 times
0
-
would you be able to provide what you have tried? or maybe the schema of the tables you are trying to union? – hurnhu Nov 14 '19 at 19:56
2 Answers
0
select 0 my_flag, your_column from your_table_1 union all select 1 my_aflag, your _column from your_table_2

Simon
- 36
- 3
-
Thank you, your code is very useful to create two flag variables. Is there a way to create one common flag variable that has value of 1 or 0 based on the datasource? or a way to combine those two variables into one? – Prajwal Mani Pradhan Nov 14 '19 at 20:03
0
Just add a static column to your SELECT
SELECT
a
,b
,0 AS flag
FROM
t1
UNION ALL
SELECT
a
,b
,1 AS flag
FROM
t2

Randy Slavey
- 544
- 4
- 19