2017-08-16 94 views
0

我有一個包含3個表的數據庫:羣組時間段,用戶和事件。跨中間表加入

羣組有很多用戶,每個用戶都有很多事件。隊列還有一段時間與他們有關。我想知道每個隊列的每一段時間發生了多少事件。

如果有兩個表格,那麼很容易做一個CROSS JOIN,但是當有這個中間表時,我就卡住了。

這裏的DB結構:

create table time_periods (
    cohort_name varchar, 
    period_name varchar, 
    start_time timestamp, 
    end_time timestamp); 

create table users (
    cohort_name varchar, 
    user_name varchar 
); 

create table events (
    user_name varchar, 
    ts timestamp); 

insert into time_periods values 
('cohort1', 'first', '2017-01-01', '2017-01-10'), 
('cohort1', 'second', '2017-01-10', '2017-01-20'), 
('cohort2', 'first', '2017-01-15', '2017-01-20'); 

insert into users values 
    ('cohort1', 'alice'), 
    ('cohort2', 'bob'); 

insert into events values 
('alice', '2017-01-07'), 
('alice', '2017-01-17'), 
('bob', '2017-01-18'); 

這是據我可以用SQL得到 - 做一個三重交叉連接,但它是不正確的 - 結果是6個事件,當它應該只是每行1個。

select 
    time_periods.cohort_name, 
    period_name, 
    count(ts) 
from time_periods, users, events 
group by 1, 2 
order by time_periods.cohort_name 

這裏的SQLFiddle:

http://sqlfiddle.com/#!17/b141e/2

回答

1

你需要指定你想加入的表 如果我理解您的數據的列正確的,你想是這樣的:

select 
    tp.cohort_name, 
    tp.period_name, 
    count(*) 
from time_periods tp 
inner join users u on tp.cohort_name = u.cohort_name 
inner join events e on u.user_name = e.user_name and e.ts between tp.start_time and tp.end_time 
group by 1, 2 
order by tp.cohort_name 

在這裏,您只從time_periodsusers加入正確羣組的用戶然後加入到events只適用於指定的用戶和特定時間段的事件,然後按1和2分組以獲得正確的偶數

+0

啊!我從來沒有見過''between'語法。這就是我一直在尋找的!我試圖用CASE WHEN + SUM解決它,但它不起作用。謝謝:) – LittleBobbyTables

+0

aww,聽起來很複雜...... sql對於日期來說不是一半壞的,它只是 user3012759