简体   繁体   English

根据另一个表中的列值对一个表中的项目进行分组和求和

[英]Group and sum items from one table based on column value from another table

Attached image:附图片:

表的结构

Okay, I keep running around in circles on this one and I know that when I see the solution, I'll facepalm in my own direction.好吧,我一直在这个问题上绕圈子跑,我知道当我看到解决方案时,我会朝着我自己的方向看。 Just pretty much brain dead at this point.在这一点上几乎脑死亡。 Anyway.....反正.....

I need to sum the price of the "item" column on the orders table based upon the "type" column in the inventory table.我需要根据库存表中的“类型”列对订单表中“项目”列的价格求和。

The inventory table is used as the criteria to create a lookup field for the "item" column in the orders table.库存表用作为订单表中的“项目”列创建查找字段的条件。

Each part number in the "item" column in the inventory table is given a "type" value (1 - 5).库存表中“项目”列中的每个零件编号都被赋予一个“类型”值 (1 - 5)。 I need to get the total sum for each type in the orders table.我需要获取订单表中每种类型的总和。

Hopefully that all makes sense.希望这一切都有意义。 Thank you in advance for any assistance!提前感谢您的任何帮助!

EDIT:编辑:

As requested, hopefully this will clear it up :)根据要求,希望这会清除它:)

In the inventory table, there are roughly 50 items.在库存表中,大约有 50 个项目。 About 30 of these will be circuit board part numbers and are Type 1. Type 2 will be TV's.其中大约 30 个是电路板部件号,属于类型 1。类型 2 将是电视。 Type 3 will be logic controllers.类型 3 将是逻辑控制器。 Type 4 will be pumps.类型 4 将是泵。 Type 5 will be meters.类型 5 将是米。

So, I am trying to get the query to show:所以,我试图让查询显示:

ITEM TYPE  TOTAL PRICE

1      $xxxxxx
2      $xxxxxx
3   etc etc   
SELECT i.item,i.type, SUM(price) AS sum_price
FROM orders o JOIN inventory i ON TRY_CONVERT(INT,o.item)=i.item
GROUP BY i.item, i.type;

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

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