0

次のように、2 つのディメンション テーブルと 1 つのファクト テーブルがあります。

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

次のエラーがあると思います。

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 に答える