gpt4 book ai didi

mysql - 在 mySQL 中对多个相同的表求和

转载 作者:行者123 更新时间:2023-11-30 22:10:40 27 4
gpt4 key购买 nike

我有 4 个相同的表(每个季度一个)。我已经设法计算每个季度的每个起始终端和按起始终端分组的数量。但是,我想对所有 4 个季度(表格)求和以获得计数总和。

SELECT StartTerminal, COUNT(*)
FROM test.2013_q1
GROUP BY StartTerminal;

Click here to see screenshot

这是我尝试执行但失败的脚本:

SELECT StartTerminal, COUNT(*) as TotalCount
FROM (
SELECT StartTerminal
FROM test.2013_q1
GROUP BY StartTerminal
UNION ALL
SELECT StartTerminal
FROM test.2013_q2
GROUP BY StartTerminal
)
GROUP BY StartTerminal;

谢谢!!

最佳答案

我认为错误可能是您没有为子查询指定名称。请试试这个 -

SELECT StartTerminal, SUM(count) as TotalCount
FROM (
SELECT StartTerminal, COUNT(*) as count
FROM test.2013_q1
GROUP BY StartTerminal
UNION ALL
SELECT StartTerminal, COUNT(*) as count
FROM test.2013_q2
GROUP BY StartTerminal
) all_year_data
GROUP BY StartTerminal;

关于mysql - 在 mySQL 中对多个相同的表求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40208245/

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