简体   繁体   English

SQL将来自两个不同表的两列相乘

[英]SQL multiplying two columns from two different tables

i have two tables 我有两张桌子

Items                                        Inventory
-----------                                  -----------
Unit_Price                                   Item_Code
Item_Code                                    Order_Num
Item_Type                                    Item_In_Stk

i am trying to calculate all the inventory items that have a unit price over $25 so im doing this by multipplying Unit_Price and Item_In_Stk 我正在尝试计算单价超过25美元的所有库存商品,因此我通过将Unit_Price和Item_In_Stk相乘来执行此操作

here is my code 这是我的代码

SELECT SUM(Unit_Price * Items_In_STk)
FROM Item, Inventory
WHERE Unit_Price > 25.00
AND Item.ITEM_CODE IN(3177,45219,23456);

i put the Item_Code numbers in the AND operator that are over 25.00 but i did the math and im getting the wrong answer. 我将超过25.00的Item_Code编号放入AND运算符中,但是我进行了数学运算,因此我得到了错误的答案。 How should i rearrange this because i think the logic behind it is right 我应该如何重新安排呢,因为我认为其背后的逻辑是正确的

SELECT items.item_code, SUM(Unit_Price * Items_In_STk)
FROM Items JOIN Inventory
on items.item_code = inventory.item_code 
WHERE Unit_Price > 25.00 
AND Items.ITEM_CODE IN(3177,45219,23456)
group by items.item_code

You are missing a group by clause on item_code. 您缺少item_code上的group by子句。 Also, you were missing a join clause. 另外,您缺少join子句。

Edit: If you only need the sum for all the 3 item codes, you can do 编辑:如果只需要所有3个商品代码的sum ,则可以

SELECT SUM(Unit_Price * Items_In_STk)
FROM Items JOIN Inventory
on items.item_code = inventory.item_code 
WHERE Unit_Price > 25.00 
AND Items.ITEM_CODE IN(3177,45219,23456)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM