0
我有两个维度表和一个事实表如下:星型模式聚集问题
drop table if exists ref;
create table ref (country_id int not null, id_ref int not null);
insert into ref values(1,1);
insert into ref values(1,2);
drop table if exists conv;
create table conv (country_id int not null, id_ref int not null,id_conv int not null,item varchar(25));
insert into conv values (1,1,1,'AA');
insert into conv values (1,2,2,'CC');
insert into conv values(1,2,3,'CA');
insert into conv values(1,2,4,'CA');
drop table if exists fact;
create table fact as
select
r.country_id,c.item,
count(distinct r.id_ref) refs,
count(distinct c.id_conv) convs
from ref r
left join conv c
on r.country_id=c.country_id
and r.id_ref=c.id_ref
group by 1,2;
查询,得到的结果:
select f.country_id, sum(f.refs) refs,sum(f.convs) convs
from fact f
group by 1;
上述查询的结果是 1,3,4
,但我期待 1,2,4
如何ç我达到预期的结果还是我的概念错了?
你能解释为什么你期望1,2,4?你的事实表明确有3个结果,总结这些将等于3 ...为什么你需要逐个分组 - 这是行不通的(它会产生1,2,4):http:// sqlfiddle。 com /#!2/22d3e8/3 – sgeddes
因为ref表只有两行,所以如果我数他们我应该得到2作为id_ref的计数 – Developer