I have to join (required to use join) these 2 tables table and table B.
I have table A
CoverID
4!12569
3!18175
1!478931
And Table B
ID
Accountid
4
12569
3
18175
1
478931
Please advise how can I join the table using concat
From TableA A
Left join TableB B
On A.CoverId = (concat(B.ID, ‘!’, B.AccountId))
You need to make sure the data types match. So you’ll have to play around with whether or not you need to cast the data to match within the concatenation besides that it should work.
Related
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
I'll try my best to pose this as a question, and not a "write my script" request- but finding it hard to phrase without using the working example.
To start, here's a relational diagram of what I'm working with;
**TABLE 1 - TABLE 2 are scalable, i.e there is a TABLE 4 - TABLE 5, TABLE -... etc, it's Tables, A/B/C that are constant.
Essentially, what I'm trying to do with this structure is match a certain value that only exists in TABLE 1 or 2 to TABLE C, where I'll pass that value to TABLE 1 or 2 and return the TABLE C value.
SELECT
TABLEC.CUSTOMID
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.TABLE2ENTITYKEY = TABLE2.ENTITYKEY
INNER JOIN TABLEA ON TABLEA.ENTITYKEY = TABLE2.TABLEAENTITYKEY
INNER JOIN TABLEB ON TABLEB.ENTITYKEY = TABLEA.TABLEBENTITYKEY
INNER JOIN TABLEC ON TABLEC.ENTITYKEY = TABLEB.TABLECENTITYKEY
WHERE TABLE1.USERENTITYKEY = /*ENTER_VALUE*/
OR
SELECT
TABLEC.CUSTOMID
FROM TABLE3
INNER JOIN TABLE4 ON TABLE3.TABLE4ENTITYKEY = TABLE4.ENTITYKEY
INNER JOIN TABLEA ON TABLEA.ENTITYKEY = TABLE4.TABLEAENTITYKEY
INNER JOIN TABLEB ON TABLEB.ENTITYKEY = TABLEA.TABLEBENTITYKEY
INNER JOIN TABLEC ON TABLEC.ENTITYKEY = TABLEB.TABLECENTITYKEY
WHERE TABLE3.USERENTITYKEY = /*ENTER_VALUE*/
Both of these work, and will work for my Tables 5/6, 7/8 etc - but that leaves me with multiple queries.
What I'd like to do is 'combine' these queries into one, where I'll pass my value to Tables 1/3/5 etc and return any values from C that link back to any of the source tables - it's not important that I know which source table the value in C links too, it's only important that it links to one of them.
Does anyone have any suggestions or examples I can use to see how this kind of script is formatted? I have played around with some things - but my SQL isn't that strong, so it's futile so far.
Thanks in advance, please comment if more information is required - and sorry in advance if the question is poorly asked!
-L
Not a full solution - but currently tying the two above queries together using UNION.
If I have a table with the following atributes:
A: id, race, key1
B: key1, driving_id
C: driving_id, fines
why would it be possible for us to have the following queries:
select A.id, A.race, B.key1, B.driving_id, C.fines
from A
left join B on A.key1=B.key1
left join C on B.driving_id= C.driving_id
even though there are no common keys for A and C in the last line of the SQL query?
The query that you have written is parsed as:
select A.id, A.race, B.key1, B.driving_id, C.fines
from (A left join
B
on A.key1 = B.key1
) left join
C
on B.driving_id = C.driving_id;
That is, C is -- logically -- being joined to the result of A and B. Any keys from those tables would be valid.
Although your original query is the preferable way to write it, you could also write:
select ab.id, ab.race, ab.key1, ab.driving_id, C.fines
from (select . . . -- whatever columns you need
from A left join
B
on A.key1 = B.key1
) ab left join
C
on ab.driving_id = C.driving_id;
The three versions are all equivalent, but the last one may help you better understand what is going on with joins between multiple tables.
Without seeing sample data from the three tables, we might not know for sure in the query makes any sense or would even run. Assuming it does run, then there should be nothing wrong with the join logic. For example, it is perfectly possible for table B to have a key key1 which relates to the A table, while at the same time having another key driving_id which relates to the C table. Note that either of these keys (but not both) could be a primary key in the B table, and if not then each key would be a foreign key.
The LEFT JOIN keyword returns all records from the left table (tableA), and the matched records from the right table (tableB). Furthermore, Similarly it returns all records from the result of first set, and the matched records from the right table (tableC). The result is NULL from the right side, if there is no match.
So A & C have a link through table B.
I have been working on this issue since 2 days now.
I have two tables created by using SQL Select statements
SELECT (
) Target
INNER JOIN
SELECT (
) Source
ON Join condition 1
AND Join condition 2
AND Join condition 3
AND Join condition 4
AND Join condition 5
The target table has count value of 10,000 records.
The source table has count value of 10,000 records.
but when I do an inner join between the two tables on the 5 join conditions
I get 9573 records.
I am basically trying to find a one to one match between source and target table. I feel every field from target matches every field in source.
Questions:
Why does my inner join give less records even if there are same value of records in both tables?
If it is expected, how can I make sure I get the exact 10,000 records after the join condition?
1) An INNER JOIN only outputs the rows from the JOINING of two tables where their joining columns match. So in your case, Join Condition1 may not exist in rows in both tables and therefore some rows are filtering out.
2) As the other poster mentioned a left join is one way. You need to look which table source or target you want to use as your master i.e. start from and return all those rows. You then left join the remaining table based on your conditions to add all the columns where you join conditions match.
It's probably better if you give us the tables you are working on and the query\results you are trying to achieve.
There's some really good articles about the different joins out there. But it looks like you'd be interested in left joins. So if it exists in Target, but not in Source, it will not drop the record.
So, it would be:
SELECT(...) Target
LEFT OUTER JOIN
SELECT(...) Source
ON cond1 and cond2 and cond3 and cond4 and cond5
Give that a shot and let me know how it goes!
Sometime you need to rely on logical analysis rather than feelings. Use this query to find the fields that do not match and then work out your next steps
SELECT
Target.Col1,Source.Col1,
Target.Col2,Source.Col2,
Target.Col3,Source.Col3
FROM
(
) Target
FULL OUTER JOIN
(
) Source
ON Target.Col1=Source.Col1
AND Target.Col2=Source.Col2
AND Target.Col3=Source.Col3
WHERE (
Target.Col1 IS NULL
OR Source.Col1 IS NULL
OR Target.Col2 IS NULL
OR Source.Col2 IS NULL
OR Target.Col3 IS NULL
OR Source.Col3 IS NULL
)
Morning all, I need some help please.
I have a database with several tables. I'm trying to write an INSERT INTO and SELECT query that copies all values from one Table (TableA) into another Table (TableD) but substitutes one value with a value it looks up in another table (Table B).
Table A with various fields including TableBRef
Table B includes various fields starting with TableBRef and also includes a field TableCRef
I want to copy all of TableA into Table D but replace The TableBRef with the TableCRef ie I know TableBRef, I need to search for it in TableB and return the associated value from the TableCRef field.
INSERT INTO TableD
(DRef, CRef, DData1, DData2)
SELECT TableA.ARef, TableB.CRef, TableA.AData1, TableA.AData2
FROM TableD AS TableD_1 CROSS JOIN
TableC CROSS JOIN
TableA INNER JOIN
TableB ON TableA.BRef = TableB.BRef
Sorry, I thought that calling them generic table names may help but it's actually a little confusing :-)
I don't understand how you relate each table but see if this query will work for you
INSERT INTO TableD
(DRef, CRef, DData1, DData2)
SELECT a.ARef, c.CRef, a.AData1, a.AData2
from tableA a
left outer join tableB b on a.ARef = b.ARef
inner join tableC c on b.CRef = c.CRef