2014-12-31 52 views
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

如何ç我达到预期的结果还是我的概念错了?

+0

你能解释为什么你期望1,2,4?你的事实表明确有3个结果,总结这些将等于3 ...为什么你需要逐个分组 - 这是行不通的(它会产生1,2,4):http:// sqlfiddle。 com /#!2/22d3e8/3 – sgeddes

+0

因为ref表只有两行,所以如果我数他们我应该得到2作为id_ref的计数 – Developer

回答

0

我认为你有一个错误:

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=r.country_id 
and r.id_ref=c.id_ref 
group by 1,2; 

请尝试

left join conv c 
    on r.country_id=c.country_id 
    and r.id_ref=c.id_ref 

,而不是

left join conv c 
    on r.country_id=r.country_id 
    and r.id_ref=c.id_ref 

(以下部分看起来像一个错误r.country_id=r.country_id - 总是为真表达)

0

您的例外与此查询有误。 您正在根据国家加入两张表格。他们将会有4个匹配的记录。按国家&项目,将有三条记录。总结清楚与项目。实际结果是正确的。

 
country_id item refs convs 
1 AA 1 1 
1 CA 1 2 
1 CC 1 1 

对你的期望,查询将

select 
 
r.country_id, 
 
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 r.country_id