我坚持这个问题: 各位大家好(不知道为什么它没出现在第一行......)
我有两张桌子:
-factures (invoices)
-commandes_clients (orders)
每个表都包含唯一的id_client
。
我试图根据发票金额对每个客户的平均金额排序进行排序:(每个客户的总发票金额/同一客户的订单数量)
Client 1 Average order amount 1254.21
Client 2 Average order amount 951.88
Client 3 Average order amount 891.11
...
我想按平均金额排序以获得客户的掌声
Table facture contains :
id_client, invoice_number, total amount
Table commandes_clients contains :
id_client
感谢您的帮助
答案 0 :(得分:1)
SELECT id_client,sum(total_amount)/count(invoice_number) as avg
FROM factures group by id_client order by avg;
或者:
SELECT id_client,sum(total_amount)/count(CC.id) as avg
FROM factures FA
INNER JOIN commandes_clients CC ON FA.id_client=CC.id_client
group by FA.id_client order by avg;
尝试以上查询。