mysql - 结果中 2 列的聚合

标签 mysql sql aggregate mysql-workbench star-schema

我正在尝试聚合结果表中的两列,但我无法理解它...我有一个星型模式和以下查询:

select 1und1.SubCategory,count(industry_facts.NACE_ID), statistics_2016.businesses 
from industry_facts, 1und1, statistics_2016
where 1und1.NACE_ID = industry_facts.NACE_ID
and statistics_2016.Stat_ID = industry_facts.Stat_ID
group by 1und1.SubCategory ASC, statistics_2016.businesses

结果表以这种方式返回正确的结果:

enter image description here

这是架构:

enter image description here

我无法找到按子类别聚合它的方法...所以它看起来像住宿,660, 1004390

尝试在 group by 子句的和处进行汇总,但这只聚合第二列,第三列总是返回“NULL”

你有什么想法吗?非常感谢您的帮助!

最佳答案

GROUP BY 中删除business。另外,决不FROM 子句中使用逗号。 始终使用正确、明确的JOIN语法:

select u.SubCategory, count(i.NACE_ID)
from industry_facts join i
     1und1 u
     on u.NACE_ID = i.NACE_ID join
     statistics_2016 s 
     on s.Stat_ID = i.Stat_ID
group by u.SubCategory ASC;

如果您想要这些商家,则可以在 SELECTgroup_concat(distinct s.businesses) 中使用 group_concat(s.businesses) >.

关于mysql - 结果中 2 列的聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43706381/

相关文章:

c# - 如何在mysql查询中使用控件值

Mysql 查询使用 Rails 返回数组?

MySQL 和 Memcached 用于大型数据集?

SQL 按天分组,带计数

r - 使用 data.table (R 3.1.1) 进行字符串分组(聚合)

mysql 随机最少使用

mysql - 按 COUNT(something) 排序与按 #column 排序(sqlzoo 示例)

php - Propel:从别名连接表中选择列

php - 为每组获取一行,其中包含订单和限制

mysql - 使用 Mysql JOIN 进行 DISTINCT SUM