Joining two tables where the join condition requires a substring - sql

I am trying to join two oracle database tables where the columns to join on contain slightly different data.
For example Table A has a column 'ref' and Table B has a column 'id'.
A.ref contains data like A1234567890B and B.id contains data of the form 1234567890
I have tried joining the two based on the following query;
SELECT * FROM A INNER JOIN B
ON SUBSTR(A.ref, 2,10) = B.id;
But this has returned no results when I know that there is matching data from this substring.
Any ideas?

you could try something like this:
SELECT * FROM A INNER JOIN B
ON regexp_substr(A.ref, '^[[:alpha:]]+([[:digit:]]+)[[:alpha:]]+$',1,1,'c',1) = B.id

I was able to solve this in the end by padding out SUBSTR(A.ref, 2,10) to 12 characters.

Related

Values after join are incorrect

I have 2 database tables. Table A has to fetch some records based on parameter passed there may or may not be an entry in table B with that key.
What I want to do is:
select a.col1,a.col2,a.col3
FROM table WHERE a.id = 123
This would fetch 20 rows. For one of the rows there is an entry in another table B.
select T_level from table b where b.id = 123
only one record appears with right value.
What I want is to get this in a single query. Something like:
select a.col1,a.col2,a.col3,b.T_level
from a,b
where a.id = 123
and a.id = b.id
When I do that, I get 20 rows and the column T_level as '50' for all the rows, whereas it should be '50' for one correct row, for rest it should be null.
I further tried:
select a.col1,a.col2,a.col3,nvl(b.T_level,0) from a,b
but that doesn't fetch the way I expect.
Firstly, please learn to use ansi sql join syntax. The Oracle join syntax you are using hasn't been considered good practice for decades
SQL Join syntax
If you want to get all records from a and any matching records from b then you need to use a LEFT OUTER JOIN

SQL Inner Join w/ Unique Vals

Questions similar to this one about using DISTINCT values in an INNER JOIN have been asked a few times, but I don't see my (simple) use case.
Problem Description:
I have two tables Table A and Table B. They can be joined via a variable ID. Each ID may appear on multiple rows in both Table A and Table B.
I would like to INNER JOIN Table A and Table B on the distinct values of ID which appear in Table B and select all rows of Table A with a Table A.ID which appears matching some condition in Table B.
What I want:
I want to make sure I get only one copy of each row of Table A with a Table A.ID matching a Table B.ID which satisfies [some condition].
What I would like to do:
SELECT * FROM TABLE A
INNER JOIN (
SELECT DISTINCT ID FROM TABLE B WHERE [some condition]
) ON TABLE A.ID=TABLE B.ID
Additionally:
As a further (really dumb) constraint, I can't say anything about the SQL standard in use, since I'm executing the SQL query through Stata's odbc load command on a database I have no information about beyond the variable names and the fact that "it does accept SQL queries," ( <- this is the extent of the information I have).
If you want all rows in a that match an id in b, then use exists:
select a.*
from a
where exists (select 1 from b where b.id = a.id);
Trying to use join just complicates matters, because it both filters and generates duplicates.

Having trouble joing to tables with different Column names. I have a Foreign key relationship between the tables but table A will not Join table B

My table names are Donut_Order and Order_History. Donut_Order has the data I want to JOIN to Order_History. Here's my code:
SELECT DonutOrder_Id
FROM Donut_Order
LEFT JOIN Order_History ON Donut_Order.Donut_ID = Order_History.DO_ID;
It always returns the DonutOrder_ID column with data, not the join on the Order_history table with the date in DO_ID column.
Your SELECT clause is selecting the DonutOrder_Id column from your Donut_Order table. You can SELECT * to grab all of the columns, or change your select to only return the columns you want.
Best practice for SQL JOINS is to alias your tables like so:
SELECT a.ColumnName, b.ColumnName
FROM TableA a
LEFT JOIN TableB b ON b.Id = a.Id
That way you don't have any confusion as to which tables the columns have come from
Try this:
SELECT
do.*,
oh.*
FROM
Donut_Order do
LEFT JOIN Order_History oh
ON do.DonutOrder_ID = oh.DO_ID;
I also changed Donut_ID to DonutOrder_ID. Let me know if that's incorrect. It might be do.ID = oh.DO_ID??? Not sure...
About the name change, this is why you name your columns the same in every table and don't abbreviate, so you can be sure joins are correct by the fact that the join uses the same-named column on both sides. Are you sure that Donut_ID is the key from the donut order table? It seems like it would be what donut was ordered... or is it just named deceptively? Donut_ID = DO.ID? Also, please use aliases as I have done, and it will be SO much easier to understand your queries.

SQL joining two tables via large id number

I have two tables which look like this
Value EntryID
0200 43300008191907010611241000917561326051
Value EntryID
test 43300008191907010611241000917561326051
I want to join them via INNER JOIN over the EntryID number, but even though it is a nvarchar, the join does not work (I get nothing as result, my new table is empty). Why?
SELECT * FROM #T_TableA AS A INNER JOIN #T_TableB AS B ON A.EntryID = B.EntryID
Maybe one field contains extra spaces? Use the TRIM function to remove spaces:
It should be like:
INNER JOIN #T_TableB AS B ON TRIM(A.EntryID) =TRIM(B.EntryID)
Or:
INNER JOIN #T_TableB AS B ON RTRIM(LTRIM(A.EntryID)) =RTRIM(LTRIM(B.EntryID))
Those cannot be combined using an inner join. Try using a FULL JOIN. If you are trying to yield one single line row try a left or right join. If you are trying to just put the rows in a new table as two rows that look the same use a UNION

SQL merge tables with matching columns

How can I merge 2 tables with the same column names into 1 table? Something like this:
The 2nd table should fill in the 1st table.
This is as close as I got
SELECT * FROM
Animals
LEFT JOIN Best
ON Animals.species=Best.species;
http://sqlfiddle.com/#!5/d0a98/3
But it seems to concatenate the 2nd table on there.
Is LEFT JOIN really the correct way to do this?
You should list the columns in the SELECT. Then you would readily see that all you need is COALESCE():
SELECT a.price, a.species, COALESCE(b.name, a.name) as name
FROM Animals a LEFT JOIN
Best b
ON a.species = b.species;