1

このようなテーブルがあります

| 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しかし、これは顧客ごとに開始されるわけではないため、以前の合計が積み重なっていきます...

必要なデータを適切に取得するにはどうすればよいですか?

4

4 に答える 4

1

select句に顧客IDを追加する必要があります。

于 2013-09-10T13:14:04.463 に答える
0

次のようなものが必要だと思います:

select customer_id,sum(price * amount) from table group by customer_id
于 2013-09-10T13:22:19.277 に答える
0

あなたの質問は正しいはずだと思います。サーバーで次のことを試しました:

create table test (customer_id int, item_id int, price money, amount int)

insert test values (1,1,10,2)
insert test values (1,2,12,4)

insert test values (2,1,10,2)
insert test values (2,3,5,1)
insert test values (2,4,0.5,21)

SELECT customer_id,sum(price*amount) FROM test
GROUP BY customer_id
于 2013-09-10T13:20:12.673 に答える