总结Sql中的表

时间:2019-08-21 08:53:38

标签: sql

我想加入四个表并获取每个月的总销售额(价值*数量)。 每笔交易必须每月获得一次明智的选择(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

请为此情况提供支持

2 个答案:

答案 0 :(得分:0)

假设Transaction_DateDATETIMETIMESTAMP字段,这是一个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)))