为什么我的 SQL SUM 不会为我的加入添加?
why won't my SQL SUM add for my join?
所以我有代码可以将所有供应商付款汇总在一起,然后将其显示在降序列表中。
SELECT vendor_name, SUM(payment_total) AS payment_total
FROM Vendors JOIN Invoices
ON (Vendors.vendor_id = Invoices.vendor_id)
GROUP BY vendor_name, payment_total
ORDER BY payment_total DESC;
然而,它所做的只是制作一个重复的供应商名称列表以及刚刚列出的付款总额,而不是相加。我尝试使用 DISTINCT
但一直得到相同的结果。
删除 GROUP BY payment_total
,因为您希望总计按供应商分组:
SELECT
vendor_name,
SUM(payment_total) AS payment_total
FROM
Vendors
JOIN Invoices ON Vendors.vendor_id = Invoices.vendor_id
GROUP BY
vendor_name
ORDER BY
SUM(payment_total) DESC
并按 SUM(payment_total) DESC
排序
您想要对每个供应商名称的总数求和,而不是像当前 group by
子句所示的每个供应商名称和总数。只需从中删除 total_payment
就可以了:
SELECT vendor_name, SUM(payment_total) AS payment_total
FROM Vendors
JOIN Invoices ON (Vendors.vendor_id = Invoices.vendor_id)
GROUP BY vendor_name
ORDER BY payment_total DESC;
所以我有代码可以将所有供应商付款汇总在一起,然后将其显示在降序列表中。
SELECT vendor_name, SUM(payment_total) AS payment_total
FROM Vendors JOIN Invoices
ON (Vendors.vendor_id = Invoices.vendor_id)
GROUP BY vendor_name, payment_total
ORDER BY payment_total DESC;
然而,它所做的只是制作一个重复的供应商名称列表以及刚刚列出的付款总额,而不是相加。我尝试使用 DISTINCT
但一直得到相同的结果。
删除 GROUP BY payment_total
,因为您希望总计按供应商分组:
SELECT
vendor_name,
SUM(payment_total) AS payment_total
FROM
Vendors
JOIN Invoices ON Vendors.vendor_id = Invoices.vendor_id
GROUP BY
vendor_name
ORDER BY
SUM(payment_total) DESC
并按 SUM(payment_total) DESC
您想要对每个供应商名称的总数求和,而不是像当前 group by
子句所示的每个供应商名称和总数。只需从中删除 total_payment
就可以了:
SELECT vendor_name, SUM(payment_total) AS payment_total
FROM Vendors
JOIN Invoices ON (Vendors.vendor_id = Invoices.vendor_id)
GROUP BY vendor_name
ORDER BY payment_total DESC;