このようなテーブルがあります
| customer_id | item_id | price | amount |
各顧客が 1 回のクエリで費やした金額を取得したいと考えています。
私が試してみました:
SELECT SUM(price * amount) AS total FROM table GROUP BY customer_id
しかし、これは の天文学的に高い値を吐き出しますtotal
。それは正しくありません。
私も試しました
SELECT @total := @total + (price * amount) AS total FROM table
CROSS JOIN (SELECT @total := 0) CONST
GROUP BY customer_id
0
しかし、これは顧客ごとに開始されるわけではないため、以前の合計が積み重なっていきます...
必要なデータを適切に取得するにはどうすればよいですか?