Joining columns from different tables without duplicating - sql

I have the table TableA with 100 rows in it. It has a column loanid, which may have duplicates.
I need to join with the other table TableB, which has a column loanid, to join with.
But, the loanid in tableB may or may not be in tableA.
So if I take a right join or left join, I want the result to be same as 100.
Since there is a matching and unmatching loanid in both table, there is a chance of result to be not 100 rows, if I do a right join or left join.

From what you describe you want a left join:
select . . .
from tablea a left join
tableb b
on a.loanid = b.loanid;
This keeps every row in tablea along with all matching rows in tableb. From your description, tableb doesn't have duplicates, so this will keep everything in tablea with no duplicates.
If tableb had duplicates and you wanted one arbitrary row, then you can use outer apply:
select . . .
from tablea a outer apply
(select top 1 b.*
from tableb b
where a.loanid = b.loanid
) b;

Related

duplicate query result when join table

I face issue about duplicate data when join table, here my sample data table I have
-- Table A
I want to join with
-- Table B
this my query notation for join both table,
select a.trans_id, name
from tableA a
inner join tableB b
on a.ID_Trans = b.trans_id
and this the result, why I get the duplicating data which should show only two lines of data, please help me to solve this case.
Firstly, as you have been told multiple times in the comments, this is working exactly as you have written, and (more importantly) as intended. You have 2 rows in tableA and those 2 rows match 2 rows in your table tableB according to the ON clause. This means that each join operation, for the each of the rows in tableA, results in 2 rows as well; thus 4 rows (2 * 2 = 4).
Considering that your table, TableA only has one column then it seems that you should be cleaning up that data and deleting the duplicates. There are plenty of examples on how to do that already (example).
Perhaps the column you show us in TableA is one many, and thus instead you have a denormalisation issue, and instead there should be another table with the details of Id_trans and a PRIMARY KEY or UNIQUE CONSTRAINT/INDEX on it. Then you would join fron that table to TableB.
Finally, what you might be after is an EXISTS, which would look like this:
SELECT B.trans_id, B.[name]
FROM dbo.TableB B
WHERE EXISTS(SELECT 1
FROM dbo.TableA A
WHERE A.ID_Trans = B.trans_id); --Odd that it's called ID_Trans in one table, and Trans_ID in another
As the comments mentioned your query does exactly what you asked it to do but I think you wanted something like:
select a.trans_id, a.name, b.name
from tableA a
inner join tableB b on a.trans_id = b.trans_id
group by a.trans_id, a.name, b.name
Since there are two rows in both table with same ID join will make them four. You can use distinct to remove duplicates:
select distinct a.trans_id, name
from tableA a
inner join tableB b
on a.id_trans = b.trans_id
But I would suggest to use exists:
select trans_id, name
from tableB b
exists (select 1 from tableA a where a.trans_id=b.trans_id)

SQL query with Left Join to return results when the top 1 column of joined table matches column of main table

I have a main table (TableA) and I'm Left Joining TableB, but I only want to include the first record of TableB where ColumnB of the two tables match. I've gotten this to work in the query below, except I also need to include the records of TableA where no matching record exists for TableB (i.e. in the query below b.ColumnA would be null). I understand why my line below "OR b.ColumnA = null" does not work, but I'm struggling to find a solution that does.
Any reasonable way to ALSO include records of TableA where no matching records exist in TableB
SELECT b.ColumnA, a.ColumnA, a.ColumnB
FROM TableA a
LEFT JOIN TableB b ON b.ColumnB = a.ColumnB
WHERE b.ColumnA = (SELECT TOP 1 bb.ColumnA FROM TableA aa LEFT JOIN TableB bb ON bb.ColumnB = a.ColumnB)
OR b.ColumnA = null
Use OUTER APPLY:
SELECT b.ColumnA, a.ColumnA, a.ColumnB
FROM TableA a OUTER APPLY
(SELECT TOP (1) b.*
FROM TableB b
WHERE b.ColumnB = a.ColumnB
ORDER BY ? -- however you are defining the ordering for "first"
) b;
You can leave out the ORDER BY if you are content with an arbitrary matching record from b. However, the question specifies "first" without defining it.

SQL - Need to conditionally join based on value in TableB column

I need to know the rows in TABLE A that have join records in TABLE B based a column value in TABLE B, but I also need to return rows in which a row in TABLE A has no match in TABLE B.
It seems like I need a LEFT JOIN and a LEFT OUTER JOIN, so I'm not sure what to do there. I understand how to do each, but don't understand how to do them together.
The schema looks like:
TABLE_A
pk
TABLE_B
pk
a_fk
some_value
I need the joined rows where Table_A has no join record in Table_B OR Table_A has a join record row in Table_B (it can have many) in which some_value does not equal "thisValue"
Thanks.
A Left join is a left outer join. Outer joins preserve one of the tables which is what you are after so good guess.
SELECT *
FROM Table A
LEFT JOIN Table B
ON TableA.Column = TableB.Column
AND B.SomeValue <> 'ThisValue'
All of the rows with a match will have the B information populated all of those without will have nulls in the B data

My attempt at LEFT JOIN isn't producing desired result. Any ideas?

I am seriously suffering from brain dead as I have done this successfully several times in the past.
This time, it isn't working.
I have 2 tables, tableA and tableB
TableA has all the surmons records
TableB has some but not all surmons.
The common key between them is surmonId.
The requirement is to display the surmons from tableB where there is a match between tableA and tableB but at the same time, display ALL the surmons from tableA.
In other words, give me from tableB any records that exist and all the records on tableA.
The lef join query below is only giving me records that exist in tableB.
Select distinct l.surmons from tableB b left join tableA a on b.surmonId = a.surmonId.
There are only 10 surmons on tableB and that's all I am getting.
Where am I messing up?
Thanks a lot in advance
Either switch order of your tables:
SELECT DISTINCT a.surmons
FROM tableA a
LEFT JOIN tableB b
ON a.surmonId = b.surmonId
Or use my favorite, the RIGHT JOIN:
SELECT DISTINCT a.surmons
FROM tableB b
RIGHT JOIN tableA a
ON b.surmonId = a.surmonId
If you want everything from tableA, you need to make the left join from tableA to tableB.
Select distinct a.surmons from tableA a left join tableB b on a.surmonId = b.surmonId

Cartesian join two tables with no records

I have to join Table A (tax related) to Table B (customer related)
I pull at most 1 record but sometimes no record.
Now I need to return the combined record to the user
I though doing a simple Cartesian product would have work
SELECT * FROM TableA, TableB
but that does not work if TableA or TableB is empty
I would do a full outer join but right now do not have anything to join on. I could create temp tables with identity columns and then join on them (since 1 = 1)
But I was looking for a different way?
Thank you
Per your own suggestion, you could use a full outer join to guarantee a row:
select *
TableA a
full outer join
TableB b
on      1=1
To always return at least one row, even if TableA and TableB are emtpy, you could use a fake table:
select *
from (
select 1 as col1
) fake
left join
TableA a
on 1=1
left join
TableB b
on 1=1