I have two table table A and table B.
Table A Contain Id, Number, Time , Value1
Table B Contain Id, Data, Value2
Example of the Record on Table A:
Id Number Tried Value1
------- ---------- --------- ---------
1 123 23 5
2 124 23 6
3 1254 23 7
Example of the Record on Table B:
Id Data Value2
------ --------- -------
1 123,23 6
2 122,21 5
3 1254,23 7
My Purpose to Add Value 1 and Value 2 together by join condition of the table B Data with table A Number and Tried to match the record.
Example :
Id (Value1 + Value2)
------- -----------------
1 11
3 14
My Query:
select a.Id , a.Value1+ b.Value2
from a
join b on substring(b.Data,1,3) = a.Number and substring(b.Data,5,2) = a.Tried
I had tried substring but the value of Data Record Length is different compare on Id 1 and 3 and current of Query Result only show Id 1. Is there other way to join with 1 column field that split into 2 kind of value which take out ',' to join 2 field on table a?
Check if this query would help, in Oracle
select a.Id , (a.Value1+ b.Value2)
from a, b
where a.id = b.id and b.Data = (a.Number || ',' || a.Tried);
EDIT: Based on @Joachim Isaksson suggestion:
select a.Id ,b.Id (a.Value1+ b.Value2)
from a, b
where b.Data = (a.Number || ',' || a.Tried);
You can use the following query.
select A.Id, (A.Value1 + B.Value2) [Value1 + Value2] from tblA A
inner join tblB B on A.Number = SUBSTRING(B.Data, 1, charindex(',', B.Data, 1) - 1)
And A.Tried = SUBSTRING(B.Data, charindex(',', B.Data, 1) + 1, Len(A.Tried))
This I have tried in SQL Server.
Query of Nishanthi Grashia can also be converted in SQL Server. You just need to replace || with +
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.