在 SQL 查询中同时使用 union 和 count(*)
我有一个 SQL 查询,看起来像这样:
select name, count (*) from Results group by name order by name
和另一个相同的查询,它从存档结果表加载,但字段是相同的。
select name, count (*) from Archive_Results group by name order by name
我如何将两者合并到一个查询中? (所以分组仍然可以正常工作)。 我尝试使用 union all,但它不起作用。 我缺少什么?
I have a SQL query, looks something like this:
select name, count (*) from Results group by name order by name
and another, identical which loads from a archive results table, but the fields are the same.
select name, count (*) from Archive_Results group by name order by name
How would I combine the two in just one query? (So the group by would still function correctly). I tried with union all, however it won't work. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您有支持索引和相对较高的计数,类似这样的方法可能比建议的解决方案要快得多:
If you have supporting indexes, and relatively high counts, something like this may be considerably faster than the solutions suggested:
您的目标是...
两个表中的“Jones”(例如)
Jones”在
Results
中的一行中,并且“Bob Jones”的所有实例
Archive_Results
在单独的行中?假设它是#1,你会想要这样的东西......
Is your goal...
Jones" in both tables (for example)
Jones" in
Results
in one row andall the instances of "Bob Jones" in
Archive_Results
in a separate row?Assuming it's #1 you'd want something like...
这在 Oracle 中有效。 “作为TMP”正在扔它。 稍微不同的查询是对几行计数进行求和。
This worked in Oracle. The "As TMP" was throwing it. Slightly different query in that in sums several rows of counts.