gpt4 book ai didi

mysql - 在 Access 中对多个表使用聚合函数

转载 作者:行者123 更新时间:2023-11-30 01:37:40 27 4
gpt4 key购买 nike

我有四个表 Items、Customer、Invoice_summary、Invoice_details。在这里,我想连接这四个表并获取特定 item_code 和特定日期范围的 Sum(Invoice_details.Item_quntity) 和 Sum(Invoice_details.Price) 。主要栏目如下:

Invoice_summary :Inv_num,Inv_date,Cus_id,Total 
Items :Item_code,Item_name,Unit_price
Invoice_details :Inv_num,Item_code,Item_qty,Price
Customers :Cus_id,Cus_name,Route

这是我目前拥有的。这返回的不仅仅是一个行(整个项目名称),我只需要特定的项目代码。有人可以解释我哪里出错了。

SELECT Invoice_Table.Item_Code, Items.Item_Name, 
(Select sum(Invoice_Table.Item_Quntity) from (Invoice_Table INNER JOIN Invoice ON Invoice_Table.Inv_Num = Invoice.Inv_Num) where ((Invoice_Table.Item_Code=[?]) And Invoice.inv_date Between #3/4/2013# And #6/4/2013#) group BY Invoice_Table.Item_Code) AS Quntity,
(Select sum(Invoice_Table.Price) from (Invoice_Table INNER JOIN Invoice ON Invoice_Table.Inv_Num = Invoice.Inv_Num) where ((Invoice_Table.Item_Code=[?]) And Invoice.inv_date Between #3/4/2013# And #6/4/2013#) group BY Invoice_Table.Item_Code) AS Price
FROM Invoice_Table
INNER JOIN Items ON Invoice_Table.Item_Code = Items.Item_Code
GROUP BY Invoice_Table.Item_Code, Items.Item_Name;

最佳答案

此查询应给出您感兴趣的总和。

select Inv_num, Item_code, 
sum(Item_qty) item_code_qty,
sum(Price) item_code_price
from invoice_details
group by Inv_num, Item_code

看起来应该在发票汇总表中找到日期范围。看起来 Invoice_summary 和 Invoice_details 应该可以在 Inv_num 上连接。

select inv_s.Inv_num, inv_s.Inv_date, inv_s.Cus_id, inv_s.Total,
inv_t.item_code_qty, inv_t.item_code_price
from Invoice_summary inv_s
inner join (select Inv_num, Item_code,
sum(Item_qty) item_code_qty,
sum(Price) item_code_price
from invoice_details
group by Inv_num, Item_code
) inv_t
on inv_s.Inv_num = inv_t.Inv_num
where inv_s.Inv_date between ? and ?;

为了解决这个问题,请在键上连接其他两个表,并将其中的一些列添加到 SELECT 子句中。

select inv_s.Inv_num, inv_s.Inv_date, inv_s.Cus_id, cus.Cus_name, inv_s.Total,
inv_t.item_code_qty, inv_t.item_code_price,
items.name
from Invoice_summary inv_s
inner join (select Inv_num, Item_code,
sum(Item_qty) item_code_qty,
sum(Price) item_code_price
from invoice_details
group by Inv_num, Item_code
) inv_t
on inv_s.Inv_num = inv_t.Inv_num
inner join items on items.Item_code = inv_t.Item_code
inner join Customers cus on cus.Cus_id = inv_s.Cus_id
where inv_s.Inv_date between ? and ?;

关于mysql - 在 Access 中对多个表使用聚合函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16633139/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com