gpt4 book ai didi

mysql - 复杂派生表的 SUM

转载 作者:行者123 更新时间:2023-11-29 21:11:18 24 4
gpt4 key购买 nike

如何获得这个复杂查询的总和?

查找 Money_volume_sum 的总和。 (我可以是一个单独的查询,这样我就可以将它用作变量来计算百分比。)

另外:有没有办法将其放入同一查询中?

基于 stackoverflow 上的原始问题: SQL SELECT query with custom column, then summed and grouped by column

干杯!

    SELECT SUM(money_volume) AS money_volume_sum,
exchange
FROM (
SELECT k.exchange, y.close * y.volume AS money_volume
FROM symbols k
JOIN stocks y ON k.id = y.id
WHERE y.t = '20160323'
ORDER BY money_volume DESC
) t
GROUP BY exchange
ORDER BY money_volume_sum DESC

最佳答案

如果你想要外部查询中的百分比,你可以使用 MySQL 中使用变量的技巧:

SELECT exchange, SUM(money_volume) AS money_volume_sum, @s as total_sum
FROM (SELECT k.exchange, y.close * y.volume AS money_volume,
(@s := @s + y.close * y.volume) as cumesum
FROM symbols k JOIN
stocks
y ON k.id = y.id CROSS JOIN
(SELECT @s := 0) params
WHERE y.t = '20160323'
) t
GROUP BY exchange
ORDER BY money_volume_sum DESC;

关于mysql - 复杂派生表的 SUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36346594/

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