Merging Two Tables in SQL Server - sql-server

I have two tables, each with some columns that are the same. However, each table also contains data that is unique. (Similar data includes a row name).
What I need to do is tack on the data from table two to it's matching row in table one (matching the name column).
Is there any way to do this?
I need stuff from table two to go into table 1 where the names match:

The following query should return all matching rows with columns from both tables. Note that any unique rows (that only exist in table one or two) will be excluded.
SELECT
one.matchingColum,
one.oddColum,
two.evenColumn
FROM one
JOIN two on one.matchingColumn = two.matchingColumn

If the data types are the same, then you can do a union
SELECT *
FROM table1
UNION
SELECT *
FROM table2
If the datatypes are not the same and you have a field that you can JOIN on, then you can do a JOIN
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id

Related

Query for comparing dynamic columns from similar tables

I have two "LINKED SERVER" databases OLDDUMP and LATESTDUMP that have multiple tables. Each given table has the same name with same columns e.g both OLDDUMP and LATESTDUMP have table SIB, and both tables have same columns like (angle, tilt,param1,param2,param3.....param200 etc..). I need to compare each column in these tables (e.g. compare t1.tilt with t2.tilt..etc) and find unmatched records. The code i have works for few columns. But for 200+ columns i do not want to write the condition (where t1.angle<> t2.angle...etc)
SELECT * FROM (
SELECT *
FROM LATESTDUMP...SIB$) t1
FULL OUTER JOIN (
SELECT *
FROM OLDDUMP...SIB$) t2
ON t1.id = t2.id
WHERE
t1.id IS NULL OR
t2.id IS NULL OR
t1.angle<>t2.angle OR
t1.tilt<>t2.tilt...
t1.param1<>t2.param1
t1.param2<>t2.param2
.
t1.param200<>t2.param200
.
etc
The other reason i dont want to write a static query is the columns in table SIB can change in future

Compare rows two tables

I want to compare rows value from one table with rows value from another table. Structure of the first table is :
The other table is more complex than first table and have the same structure but number of rows is million rows, by the way first have 60.000 rows.
I need to compare these two tables by biling_profiles so I have an insight into the price but that billing profile turn in the column. It's easily done using the left outer join because approximately 6 or 7 billing_profiles . Next thing I need is that if the other table has no value (for example, the first table has row with (destination 201425, cost 2,624) and the other does not , then I have trimmed one character from right to left and then search (destination - 20142). If no again result i repeat again trim from right to left (destination - 2014), when i found same destination then show in table)
So, how to solve that ?
try
SELECT DISTINCT A.Id
FROM
table1 A
INNER JOIN table 2B
ON A.Id = B.Id AND A.biling_profiles = B.biling_profiles
or
select t1.biling_profiles,t2.biling_profiles
from table1 t1
inner join table2 t2 on
t1.id= t2.id
where t1.biling_profiles= #biling_profiles

Create a table from full outer join query

I have two tables namely :- TDM & AccountMaster. Both are having three equal columns and I have to create a table retrieving all the rows from TDM-table joining the three columns,i.e. FD_BRANCH,FD_CUSTCODE & PRODUCTID.
while creating table through select into clause I get an error
Column names in each table must be unique. Column name 'FD_BRANCH' in table 'acty' is specified more than once.
Please find the following query from which I want to create a table which as per my requirement :-
SELECT * FROM (SELECT FD_BRANCH,FD_CUSTCODE,PRODUCTID FROM TDM
GROUP BY FD_BRANCH,FD_CUSTCODE,PRODUCTID) A full OUTER JOIN AccountMaster B
ON( A.FD_BRANCH=B.FD_BRANCH AND A.FD_CUSTCODE=B.FD_CUSTCODE AND
A.PRODUCTID=B.PRODUCTID)
Change your select to get only the fields you need from one of the 2 tables.
Select A.*
FROM (
or
Select B.FD_BRANCH,
B.FD_CUSTCODE,
B.PRODUCTID
FROM (
FULL OUTER JOIN combines the 2 sets of columns from both queries so you end up with at least 6 columns. Even though they're from different tables or aliases the columns names are the same.

Select value from different tables based on the column value in SQL Server

I have a main table A with the following fields:
Then I have three separate tables, each for Buildings, Classrooms and Offices. All these tables have two columns; ID and Name.
I want to query the table A to get the following result:
How can I do this?
Your data isn't really normalized. Having three separate tables all serving the same lookup is causing you some headache... so I unioned the 3 tables together and created a 'src' column so you could join table A's type and Id back to table B's ID and src. You'd have been better off having one table and non-repeating IDs and a type ID to specify if it's a building classroom or office.
Select *
from A
LEFT JOIN (SELECT 'Building' as src, ID, Name FROM Buildings UNION ALL
SELECT 'Classroom' as src, ID, Name FROM Classrooms UNION ALL
SELECT 'Office' as src, ID, Name FROM Offices) B
on A.Location_Type = B.Src
and A.LocationID = B.ID
I used a left join here in case not all records in A have an associated record in B. However, an inner join should work as well.
Should be doable with the use of a join.
Something along those lines should work:
SELECT tabelA.ID, tabelA.Subject, tabelA.Date, tabelA.locationType, tabelB.location
FROM tabelA INNER JOIN tabelB on tabelA.locationID = tabelB.locationID

2 nvarchar fields are not matching though the data is same?

I want to join 2 tables using an Inner Join on 2 columns, both are of (nvarchar, null) type. The 2 columns have the same data in them, but the join condition is failing. I think it is due to the spaces contained in the column values.
I have tried LTRIM, RTRIM also
My query:
select
T1.Name1, T2.Name2
from
Table1 T1
Inner Join
Table2 on T1.Name1 = T2.Name2
I have also tried like this:
on LTRIM(RTRIM(T1.Name1)) = LTRIM(RTRIM(T2.Name2))
My data:
Table1 Table2
------ ------
Name1(Column) Name2(Column)
----- ------
Employee Data Employee Data
Customer Data Customer Data
When I check My data in 2 tables with
select T1.Name1,len(T1.Name1)as Length1,Datalength(T1.Name1)as DataLenght1 from Table1 T1
select T2.Name2,len(T2.Name2)as Length2,Datalength(T2.Name2)as DataLenght2 from Table2 T2
The result is different Length and DataLength Values for the 2 tables,They are not same for 2 tables.
I can't change the original data in the 2 tables. How can I fix this issue.
Thank You
Joins do not have special rules for equality. The equality operator always works the same way. So if a = b then the join on a = b would work. Therefore, a <> b.
Check the contents of those fields. They will not be the same although you think they are:
select convert(varbinary(max), myCol) from T
Unicode has invisible characters (that only ever seem to cause trouble).
declare #t table (name varchar(20))
insert into #t(name)values ('Employee Data'),('Customer Data')
declare #tt table (name varchar(20))
insert into #tt(name)values ('EmployeeData'),('CustomerData')
select t.name,tt.name from #t t
INNER JOIN #tt tt
ON RTRIM(LTRIM(REPLACE(t.name,' ',''))) = RTRIM(LTRIM(REPLACE(tt.name,' ','')))
I would follow the following schema
Create a new table to store all the possible names
Add needed keys and indexes
Populate with existing names
Add columns to your existing tables to store the index of the name
Create relative foreign keys
Populate the new columns with correct indexes
Create procedure to perform an insert in new tables names only in case the value is not existing
Perform the join using the new indexes

Resources