我想加入四个表并获取每个月的总销售额(价值*数量)。 每笔交易必须每月获得一次明智的选择(2018年7月) 例: Agent_ID代理商名称总销售额(每月)
Agent table
----------
Agent_ID
Agent Name
Agent address
Transaction table
-----------------
Transaction_ID
Transaction_Date(12/7/2018)
Agent_ID
Transation_Status
Transaction Detal table
-----------------------
Transaction_ID
Item_code
Quantity
Item Table
----------
Item_code
Item_name
Value
请为此情况提供支持
答案 0 :(得分:0)
假设Transaction_Date
是DATETIME
或TIMESTAMP
字段,这是一个Mysql
查询,可以为您提供所需的结果。
SELECT SUM(Value * Quantity) as total, DATE_FORMAT(Transaction_Date, '%Y-%M') date
FROM Agent JOIN Transaction USING (Agent_ID)
JOIN Transaction_Detail USING (Transaction_ID)
JOIN Item USING (Item_code)
GROUP BY Agent_ID, date;
答案 1 :(得分:0)
这是您的查询。您正在尝试按月份-年份排序。
select concat(DATENAME(month, cast(Transaction_Date as varchar)), year(cast(Transaction_Date as varchar))), sum(t4.Value * t3.Quantity), from table t1
inner join transaction_table t2 on t2.Agent_ID = t1.Agent_ID
inner join transaction_detail_table t3 on t3.Transactdion_ID = t2.Transaction_ID
inner join item_table t4 on t4.Item_code = t3.Item_code
group by concat(DATENAME(month, cast(Transaction_Date as varchar)), year(cast(Transaction_Date as varchar)))