首页 > 解决方案 > 分组总和摘要

问题描述

我有如下的sql:

select 
count(*), sum(summary.imp), sum(summary.taps) 
from(
   select 
   sum(impressions) as imp, sum(taps) as taps 
   from report 
   where org_id = 1 and report_date between '2019-01-01' and '2019-10-10'
   group by org_id, country, text)
summary;

此查询在 MYSQL 中创建派生临时表。有没有办法用其他方法解决它?

标签: mysqlsqlmariadb

解决方案


我已经阅读(但尚未测试)MySQL 支持不同计数的多个列:

select 
  count(distinct org_id, country, text), sum(impressions) as imp, sum(taps) as taps 
from report 
where org_id = 1 and report_date between '2019-01-01' and '2019-10-10'

我很想知道这是不是真的。我也有理由相信这样的事情会奏效:

select 
  count(distinct concat_ws('###', org_id, country, text)), sum(impressions) as imp, sum(taps) as taps 
from report 
where org_id = 1 and report_date between '2019-01-01' and '2019-10-10'

推荐阅读