santro
santro

Reputation: 393

in oracle query, select only few columns with union

I have two tables something like this:

TABLE_1:

COL_A (int), COL_B (float), COL_C (float)

and

TABLE_2:

COL_A (int), COL_B (varchar), COL_C (varchar)

My query is using a UNION to get only COL_A(int) from table 2 like

SELECT COL_A, COL_B, COL_C FROM table1 UNION 
SELECT COL_A               FROM table2

It's throwing an error. How do we get the results?

Upvotes: 0

Views: 45

Answers (1)

The Impaler
The Impaler

Reputation: 48760

All subquery members of a UNION must have the same number and types of columns. In your case the first subquery has three columns, but the second one has only one.

Solution: pad the second subquery with nulls.

For example:

select COL_A, COL_B, COL_C from table1
union 
select COL_A, null, null from table2

Upvotes: 5

Related Questions