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

我怎样才能达到预期的结果或我的概念是错误的?

4

2 回答 2

0

此查询的异常是错误的。您要加入两个基于国家/地区的表格。他们将是 4 个匹配的记录。在按国家 & 项目分组后,将有三个记录。总结与项目不同的 refid。实际结果是正确的。

country_id 项目参考转化
1 AA 1 1
1 CA 1 2
1 抄送 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

于 2015-01-06T14:31:55.020 回答
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- 一个始终为真的表达式)

于 2014-12-31T22:12:09.520 回答