Select 来自两个不同的 table 并将计算值存储在新创建的 table 中(Microsoft SQL Server 2014)
Select from two different tables and store calculated value in a newly created table (Microsoft SQL Server 2014)
从两个不同的 table 中获取值的最佳方法是什么,例如第一个 table 的值为 Price,其中第二个 table 的值为 Quantity,我将 Price 乘以 Quantity 和计算值, Total_Price 将存储在 table 3(新创建)中。一开始我尝试使用 FUNCTION,但弹出了很多错误,所以我将其更改为 CTE。但是我的老师建议我不要使用临时 tables,因为当新行数据添加到 tables 时,我们需要再次 运行 CTE 以在每次有新记录时更新它添加。还有其他方法吗?谢谢。
您可以尝试类似(语法未验证!):
INSERT INTO Table_3 (Cur_Date,Prod,Qty,Total_Price)
VALUES (GETDATE() ,
<the passed product_ID> ,
<the passed quantity> ,
(SELECT (A.Quantity * B.Price)
FROM Table_1 A ,
Table_2 B
WHERE A.Product = <Your passed product ID>
AND A.Product = B.Product
)
);
实际的措辞将取决于您的 DBMS。
从两个不同的 table 中获取值的最佳方法是什么,例如第一个 table 的值为 Price,其中第二个 table 的值为 Quantity,我将 Price 乘以 Quantity 和计算值, Total_Price 将存储在 table 3(新创建)中。一开始我尝试使用 FUNCTION,但弹出了很多错误,所以我将其更改为 CTE。但是我的老师建议我不要使用临时 tables,因为当新行数据添加到 tables 时,我们需要再次 运行 CTE 以在每次有新记录时更新它添加。还有其他方法吗?谢谢。
您可以尝试类似(语法未验证!):
INSERT INTO Table_3 (Cur_Date,Prod,Qty,Total_Price)
VALUES (GETDATE() ,
<the passed product_ID> ,
<the passed quantity> ,
(SELECT (A.Quantity * B.Price)
FROM Table_1 A ,
Table_2 B
WHERE A.Product = <Your passed product ID>
AND A.Product = B.Product
)
);
实际的措辞将取决于您的 DBMS。