Sql server 如何使用外键从外部表中获取主表列(例如:价格列)的和?

Sql server 如何使用外键从外部表中获取主表列(例如:价格列)的和?,sql-server,Sql Server,我需要使用外部表中的外键列(例如:fk_PriceID)来计算主表列(例如:Price)的总和 似乎您在tbl\u Lead的每行中保留tbl\u设备的id两次, 对于两列fk_ProductsID和fk_PriceID, 因此,连接表并获得总和: select sum(l.Price) Price from tbl_Lead l inner join tbl_Equipments e on e.id = l.fk_PriceID 从tbl_设备、tbl_Lead中选择总价(tbl_设备.价格

我需要使用外部表中的外键列(例如:fk_PriceID)来计算主表列(例如:Price)的总和


似乎您在
tbl\u Lead
的每行中保留
tbl\u设备的id两次,
对于两列
fk_ProductsID
fk_PriceID

因此,连接表并获得总和:

select sum(l.Price) Price
from tbl_Lead l inner join tbl_Equipments e
on e.id = l.fk_PriceID
从tbl_设备、tbl_Lead中选择总价(tbl_设备.价格),其中tbl_Lead.fk_价格Id=tbl_设备.Id


不要将示例数据作为图像提供。以文本形式提供。这看起来像一个简单的加法和,你怎么得到16000?详细说明你想要什么。fk_PriceID的含义是什么?还有另一个叫Price的表吗?@vasya,看起来它连接到了第一个表的pk上。4000 + 2000 + 4000 + 6000 = 16000@VasyaPrice是主表中的一个列名,如图所示。@ÅbďùൺŔbŽąhìď:因此它始终与fkďu ProductID相同。在这种情况下,您不需要此列。