我有两个表格表A和表格B.
表A包含Id,Number,Time,Value1
表B包含Id,数据,值2
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
我的目的是通过表B的连接条件将值1和值2加在一起数据与表A编号并尝试匹配记录。
Example :
Id (Value1 + Value2)
------- -----------------
1 11
3 14
我的查询:
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
我曾尝试使用子字符串,但数据记录长度的值与Id 1和3相比有所不同,查询结果的当前只显示Id 1.还有其他方法可以将1列字段连接成2种值拿出&#39; <#39; 加入表格a上的2个字段?
答案 0 :(得分:0)
在Oracle
中检查此查询是否有帮助select a.Id , (a.Value1+ b.Value2)
from a, b
where a.id = b.id and b.Data = (a.Number || ',' || a.Tried);
编辑:基于 @Joachim Isaksson 的建议:
select a.Id ,b.Id (a.Value1+ b.Value2)
from a, b
where b.Data = (a.Number || ',' || a.Tried);
答案 1 :(得分:0)
您可以使用以下查询。
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))
这是我在SQL Server中尝试过的。
Nishanthi Grashia的查询也可以在SQL Server中转换。您只需要将 || 替换为 +