表中的谷物类似于state_id | county_id | city_id | eventtype
。 eventtype是二进制的;它等于1或2. 我想按所有3列进行分组,并查看它等于1和2的事件类型的总和。如何正确执行此操作?如何正确连接两次在同一列上进行聚合?
当我做一个内部联接的
select *
from
(state_id, county_id, city_id, sum(eventtype) as views
select
poptable
where eventtype = 1
group by state_id, county_id, city_id) l
INNER JOIN
(state_id, county_id, city_id, sum(eventtype) as passes
select
poptable
where eventtype = 2
group by state_id, county_id, city_id) r
ON l.state_id = r.state_id
and l.county_id = r.county_id
and l.city_id = r.city_id
我得到大约500行。但是如果我做一个完整的外连接,我可以获得大约3000行。我知道会有缺失的组合,那么如何让它们一起出现?
你正在使用什么数据库? –