gpt4 book ai didi

mysql - 求和列值,其中不同的其他列

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:01 24 4
gpt4 key购买 nike

我运行了这个查询:

SELECT
billed_currency,
sum(billed_total_price) as sum
FROM
invoice
WHERE
account_id = 91863

我得到了这个结果:

72012

这是正在求和的表值的样子

see image

第 3、4 行具有相同的 stripe_invoice_id。

我只需要对不重复 stripe_invoice_id 的发票的值求和,因此查询必须返回值 48012

我该怎么做?

最佳答案

如果 stripe_invoice_id 重复的行也有重复的 billed_price,并且您只想对它求和一次,您可以使用 DISTINCT:

SELECT DISTINCT stripe_invoice_id, billed_total_price
FROM invoice
WHERE account_id = 91863

您可以将其用作子查询:

SELECT stripe_invoice_id, SUM(billed_total_price)
FROM (
SELECT DISTINCT stripe_invoice_id, billed_total_price
FROM invoice
WHERE account_id = 91863
) s
GROUP BY stripe_invoice_id

关于mysql - 求和列值,其中不同的其他列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34084765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com