--CREATE TABLE Table1 (IssNo INT, Qty INT)
--INSERT INTO Table1 VALUES (120,10)
--CREATE TABLE Table2 (IssNo INT, Qty INT)
--INSERT INTO Table2 VALUES (120,20)
--INSERT INTO Table2 VALUES (124,30)
表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 :(得分:2)
试试这个:
Select Table1.IssNo,Sum(Table2.Qty) as Qty
From Table1 inner join Table2
on Cast(Table2.IssNo as varchar) like substring(cast(Table1.IssNo as varchar),1,2) + '%'
Group By Table1.IssNo
答案 1 :(得分:0)
尝试以下查询。
declare @IssNo as varchar(10) -- whatever length you want
select @IssNo = substring(cast(Table1.IssNo as varchar),1,2) + '%' from Table1
Select Table2.IssNo,Sum(Table2.Qty) as Qty
From Table2
Where Cast(Table2.IssNo as varchar) like @IssNo
Group By Table2.IssNo
答案 2 :(得分:0)
试试这个
select t1.issno,(select sum(qty) from table2) as qty
from table1 t1 inner join table2 t2
on t1.issno=t2.issno;
OR
select t1.issno,(select sum(qty) from table2) as qty
from table1 t1;