我正在尝试查找表格中列出的每种付款方式的总金额。 我有每行的总和,现在我需要添加这些总和。 查询是:
select payment_method, (deposit + pet_deposit + cleaning_fee + rental_rate) as
total
from reservations
group by total
order by payment_method;
这是结果集:
+----------------+---------+
| payment_method | total |
+----------------+---------+
| Check | 760.00 |
| Check | 960.00 |
| Check | 660.00 |
| MasterCard | 600.00 |
| MasterCard | 1160.00 |
| PayPal | 500.00 |
| Visa | 560.00 |
| Visa | 610.00 |
| Visa | 700.00 |
+----------------+---------+
9 rows in set (0.00 sec)
我如何才能实现,因此每种付款方式只有一个实例,而且总列数是全部?
答案 0 :(得分:2)
只需GROUP BY
付款方式,如下所示:
SELECT payment_method,
SUM(deposit + pet_deposit + cleaning_fee + rental_rate)
AS total
FROM reservations
GROUP BY payment_method
ORDER BY payment_method;
您的结果应如下所示(总和中的样本数据):
+----------------+---------+
| payment_method | total |
+----------------+---------+
| Check | 123.45 |
| MasterCard | 234.56 |
| PayPal | 345.67 |
| Visa | 456.78 |
+----------------+---------+
答案 1 :(得分:1)
按付款方式分组而非总计。