Using union on two tables - sql

It gives me error when i try to join two tables.Both tables has same number of columns
Here is the error
failed to find conversion function from unknown to text
select * from table1
union
select * from table 2;

When you use UNION columns need the same type as the same position.
use clear column name instead of *, Because we can't predict the number of columns after table1 or table2 is different.
select col1,col2...
from table1
union
select col1,col2...
from table2;

Related

I need help combining a few select statements

I would like to pull all of the columns from my second select statement and put it to the right of all of my columns from my first statement.
I have tried Union and the join commands with no luck.
When I use these they just have what I wanted from my first select statement.
Here is basic code I have.
Select * from MTG_TREND where LINEID='A2' end;
Select * from MTG_TREND where LINEID='B2'
All of the other columns are the same.
Select t1.*,t2.* from MTG_TREND t1
left join (
Select * from MTG_TREND where LINEID='B2'
) as t2
on t1.primarykey=t2.primarykey
where t1.LINEID='A2'
NOTE:
- Make sure you are matching the same number of columns in both SELECT Statement.
- Another thing to remember is you have to have the same matched column (datatype).
- If there are some columns that are not present is the table then just defined the column with null so that you can get it in output from another select statement.
Select col1, col2, col3,...... from MTG_TREND where LINEID='A2'
UNION ALL
Select col1, col2, col3,...... from MTG_TREND where LINEID='B2'

How to find a record from more than one table in SQL

I have two table containing some information as below
ENRNO, PROGRAM, NAME, ADDRESS, AGE
I want to find data referencing ENRNO which is containing from one of the given table but I don't know which table have the information.
Please suggest.
Malay Barik
If ENRNO is unique in tables u may try use UNION
select * from t1
where ENRNO ='ENRNO1'
UNION select * from t2
where ENRNO ='ENRNO1'
else use DISTINCT and subquery
select DISTINCT * from (
select * from t1
where ENRNO ='ENRNO1'
UNION select * from t2
where ENRNO ='ENRNO1')
But goodest way for solving this is redesign(normalize) you DB.

How to return unique records between two tables without using distinct and union?

I need to return the unique records between two tables. Ideally, an UNION would solve my problem but both tables contain an object field which gives me an error(cannot ORDER objects without MAP or ORDER method) when I do UNION/distinct.
So, I was wondering if I can do a UNION ALL(to avoid the error) to get all the records first then do something to return only the unique records from there. I tried analytic function combined with the UNION ALL query but no luck so far.
Select * from Table1
union all
Select * from table2
Any help? Note:I need to return all fields.
I actually solved the problem using analytic function+row_num. The query will choose the first record for each set of duplicates hence returning only the unique records.
select * from
(
select ua.*,row_number() over (partition by p_id order by p_id ) row_num from
(
select * from table1
union all
select * from table2
)ua
) inner
where inner.row_num=1
How about this :
SELECT DISTINCT A.* FROM
(
Select * from Table1
union all
Select * from table2
) A;
(or)
SELECT col1,col2,col3...coln FROM
(
Select col1,col2,col3...coln from Table1
union all
Select col1,col2,col3...coln from table2
) A
GROUP BY A.col1,col2,col3...coln;
UNION ALL will give duplicate values as well .. instead use UNION and see if you are facing the error

Join two tables with geometry column into one

I have two tables that are exactly the same. I want to join them together into one large dataset. I tried simply SELECT-INTO query but got an error...
SELECT * INTO dbo.ParkingBay
FROM (SELECT * FROM dbo.ParkingBay_Old
UNION
SELECT * FROM dbo.ParkingBay_New) AS PARKING_BAY;
The error is:
The geometry data type cannot be selected as DISTINCT because it is
not comparable.
The UNION performs a DISTINCT on the combined result set.
UNION ALL eliminates this DISTINCT step, but would create the possibility of dupes in the result.
If you are OK with dupe possibility, then try this
SELECT * INTO dbo.ParkingBay
FROM (SELECT * FROM dbo.ParkingBay_Old
UNION ALL
SELECT * FROM dbo.ParkingBay_New) AS PARKING_BAY;
It looks like ALL solves everything:
SELECT * INTO dbo.ParkingBay
FROM (SELECT * FROM dbo.ParkingBay_Old
UNION ALL
SELECT * FROM dbo.ParkingBay_New) AS PARKING_BAY;

UNION ALL query: "Too Many Fields Defined"

I'm trying to get a UNION of 3 tables, each of which have 97 fields. I've tried the following:
select * from table1
union all
select * from table2
union all
select * from table3
This gives me an error message:
Too many fields defined.
I also tried explicitly selecting all the field names from the first table (ellipses added for brevity):
select [field1],[field2]...[field97] from table1
union all
select * from table2
union all
select * from table3
It works fine when I only UNION two tables like this:
select * from table1
union all
select * from table2
I shouldn't end up with more than 97 fields as a result of this query; the two-table UNION only has 97. So why am I getting Too many fields with 3 tables?
EDIT: As RichardTheKiwi notes below, Access is summing up the field count of each SELECT query in the UNION chain, which means that my 3 tables exceed the 255 field maximum. So instead, I need to write the query like this:
select * from table1
union all
select * from
(select * from table2
union all
select * from table3)
which works fine.
It appears that the number of fields being tracked (limit 255) is counted against ALL parts of the UNION ALL. So 3 x 97 = 291, which is in excess. You could probably create a query as a UNION all of 2 parts, then another query with that and the 3rd part.
I had two tables with 173 fields each (2 x 173 > 255!). So I had to resort to splitting the tables in half (keeping the primary key in both), before using the UNION statement and reassembling the resulting output tables using a JOIN.
select u1.*, u2.*
from (
select [field1_PKID],[field2],...,[field110]
from table1
union all
select [field1_PKID],[field2],...,[field110]
from table2
) as u1
inner join (
select [field1_PKID],[field111],...,[field173]
from table1
union all
select [field1_PKID],[field111],...,[field173]
from table2
) as u2
on [u1].[field1_PKID] = [u2].[field2_PKID]
Perhaps if your 3 tables have duplicate records you can go with UNION instead of UNION ALL which may reduce the number of fields to be tracked. Because UNION will always serve the business purpose which removes duplicates. In that case your query will be like following,
select * from table1
union
select * from table2
union
select * from table3;