我查看了此论坛和其他人的许多帖子,我找不到类似于我需要的解决方案。
我写了几个简单的查询,以便我们可以从我们的数据库中获得一些财务统计信息...然而,这一点,我似乎无法确定......
从这个查询中,我从他们放置的每个InsuranceCarrier和NumberOfOrders获得我们的TotalRevenue ...但是在这个特定查询中,我需要它将TotalRevenue除以NumberOfOrders,并将AverageTicket赋予“ORDER BY”否则,echo'd列表的顺序不正确,即使我可以在查询后进行除法计算...
这是我的查询...
SELECT OrderTickets.InsuranceCarrierID,
COUNT(OrderTickets.OrderTicketPayoutAmount) as NumberOfOrders,
SUM(OrderTickets.OrderTicketPayoutAmount) as TotalRevenue,
InsuranceCarriers.InsuranceCarrier
FROM OrderTickets
LEFT JOIN InsuranceCarriers
ON OrderTickets.InsuranceCarrierID = InsuranceCarriers.InsuranceCarrierID
GROUP BY OrderTickets.InsuranceCarrierID
ORDER BY TotalRevenue DESC
我尝试了一些改动,但没有成功,我没有发布,因为它不起作用,但如果有人想看到我的尝试,我会很乐意发布它。
答案 0 :(得分:2)
像这样使用AVG()
:
SELECT OrderTickets.InsuranceCarrierID,
COUNT(OrderTickets.OrderTicketPayoutAmount) as NumberOfOrders,
SUM(OrderTickets.OrderTicketPayoutAmount) as TotalRevenue,
AVG(OrderTickets.OrderTicketPayoutAmount) as AverageRevenue,
InsuranceCarriers.InsuranceCarrier
FROM OrderTickets
LEFT JOIN InsuranceCarriers
ON OrderTickets.InsuranceCarrierID = InsuranceCarriers.InsuranceCarrierID
GROUP BY OrderTickets.InsuranceCarrierID
ORDER BY AverageRevenue DESC
答案 1 :(得分:0)
使用临时表,您将能够轻松完成。
获取总收入,承运人和订单数量,并将其放入临时表中,然后进行下一步计算
我认为这比尝试单个复杂查询更好!!!!!
http://www.tutorialspoint.com/mysql/mysql-temporary-tables.htm