表1
IssNo Qty
120 10
表2
IssNo Qty
120 20
124 30
也使用它: 从表1 - > substring(Table1.IssNo,1,2),最后我需要添加“%”(例如)12%
现在我需要检查这个类似于table2中的条件。对于Qty ..并且我需要添加所有数量为12%(例如)20和30将来...后来SUM => 50
我的查询是
Select Name,id,IssNo,address,Qty from Table1
(此处仅数量应该是表2的总和)
预期o / p
IssNo Qty
120 50(Sum of Qty from Table2)
答案 0 :(得分:1)
试试这个(假设 IssNo 是varchar):
Select t1.IssNo, Min(t1.Qty), Sum(t2.Qty)
from Table1 t1
Inner Join Table2 t2 On SUBSTRING(t2.IssNo, 1, 2) = SUBSTRING(t1.IssNo, 1, 2)
Group By t1.IssNo
答案 1 :(得分:0)
这可能有助于你...
Select t1.Name,t1.id,t1.IssNo,t1.address,sum(t2.Qty)
from Table1 t1
join (
select (IssNo/10) as IssNo,Qty Table2 t2
) t2 on t2.IssNo = (t1.IssNo)/10
group by t1.Name,t1.id,t1.IssNo,t1.address