user2134579
user2134579

Reputation: 11

Union two tables with data of table 2 starting from second field

I have two tables having 72 columns each. The first table has data starting from column1 and the second table has data starting from column 30. How can I do an Union on both the tables and get 0's for first 29 columns in the second table value?

Upvotes: 0

Views: 87

Answers (1)

pratik garg
pratik garg

Reputation: 3342

For this you have to explicitly write 29 columns with default value as 0 (Zero).

same you have to do for table A selection for column present in table B also.

assume table B has 20 column named col30,col31,col32 ....... col49

you can use as below query -

select TableA. col1,
       TableA. col2,
       TableA. col3,
       TableA. col4,
       TableA. col5,
       TableA. col6,
       TableA. col7,
       TableA. col8,
       TableA. col9,
       TableA. col10,
       TableA. col11,
       TableA. col12,
       TableA. col13,
       TableA. col14,
       TableA. col15,
       TableA. col16,
       TableA. col17,
       TableA. col18,
       TableA. col19,
       TableA. col20,
       TableA. col21,
       TableA. col22,
       TableA. col23,
       TableA. col24,
       TableA. col25,
       TableA. col26,
       TableA. col27,
       TableA. col28,
       TableA. col29,
       0       col30,
       0       col31,
       0       col32,
       0       col33,
       0       col34,
       0       col35,
       0       col36,
       0       col37,
       0       col38,
       0       col39,
       0       col40,
       0       col41,
       0       col42,
       0       col43,
       0       col44,
       0       col45,
       0       col46,
       0       col47,
       0       col48,
       0       col49,
       0       col50
  from tableA

union all

select 0       col1,
       0       col2,
       0       col3,
       0       col4,
       0       col5,
       0       col6,
       0       col7,
       0       col8,
       0       col9,
       0       col10,
       0       col11,
       0       col12,
       0       col13,
       0       col14,
       0       col15,
       0       col16,
       0       col17,
       0       col18,
       0       col19,
       0       col20,
       0       col21,
       0       col22,
       0       col23,
       0       col24,
       0       col25,
       0       col26,
       0       col27,
       0       col28,
       0       col29,
       TableB. col30,
       TableB. col31,
       TableB. col32,
       TableB. col33,
       TableB. col34,
       TableB. col35,
       TableB. col36,
       TableB. col37,
       TableB. col38,
       TableB. col39,
       TableB. col40,
       TableB. col41,
       TableB. col42,
       TableB. col43,
       TableB. col44,
       TableB. col45,
       TableB. col46,
       TableB. col47,
       TableB. col48,
       TableB. col49,
       TableB. col50
  from TableB

Upvotes: 2

Related Questions