1

假设我有一个如下表:

\d events 
    Table "public.events
   Column   |           Type           | Modifiers 
------------+--------------------------+-----------
 my_id      | bigint                   | 
 tstamp     | timestamp with time zone | 
 event_type | text                     | 

使用示例数据:

   my_id     |           tstamp           | event_type 
------------+----------------------------+------------
 1111111111 | 2015-11-14 09:02:46.185+02 | A
 1111111111 | 2015-11-14 17:32:58+02     | B
 1111111111 | 2015-11-28 15:06:30.895+02 | A
 1111111111 | 2015-12-05 15:22:31.582+02 | A
 2222222222 | 2015-11-17 15:06:07.481+02 | A
 2222222222 | 2015-11-17 20:30:03+02     | B
 2222222222 | 2015-12-04 15:36:31.532+02 | A
 3333333333 | 2015-11-20 15:06:01.621+02 | A
 3333333333 | 2015-11-20 19:15:09.908+02 | A
 3333333333 | 2015-11-21 15:06:01.621+02 | A
 3333333333 | 2015-11-26 09:07:45.134+02 | B
 3333333333 | 2015-11-27 14:39:31.657+02 | A
 4444444444 | 2015-12-05 10:21:21.441+02 | A
 4444444444 | 2015-12-05 20:00:40.772+02 | B

我想在事件 B 之前按 my_id 计算所有事件 A。

预期的输出将是:

   my_id   | events_before_B 
-----------+-----------------
1111111111 | 1
2222222222 | 2
3333333333 | 3
4444444444 | 1

Postgres 9.4 版

4

2 回答 2

1

做一个自我LEFT JOIN,用GROUP BY

select e1.my_id, count(e2.my_id)
from events e1
    left join (select my_id, min(tstamp) as b_tstamp from events
               where event_type = 'B'
               group by my_id) e2
    ON e1.my_id = e2.my_id AND e1.tstamp < e2.b_tstamp
group by e1.my_id

或者,NOT EXISTS版本:

select my_id, count(*)
from events e1
where not exists (select 1 from events e2
                  where e2.my_id = e1.my_id
                    and e2.event_type = 'B'
                    and e2.tstamp < e1.tstamp)
group by my_id
于 2015-12-10T10:04:37.337 回答
1
SELECT my_id
      ,position('B' IN string_agg(event_type, '')) - 1 events_before_B
FROM events
GROUP BY my_id
ORDER BY my_id

SQLFIDDLE-DEMO


解释:

string_agg()

select my_id,string_agg(event_type,'') from events group by my_id产量

my_id      string_agg 
---------- ---------- 
1111111111 ABAA       
2222222222 ABA        
3333333333 AAABA      
4444444444 AB   

position()

select position('B' in 'AAABAA')可用于找出B字符串中的位置AAABAA

position 
-------- 
4    

所以select position('B' in 'AAABAA')-1产生了A之前的位置B

position 
-------- 
3
于 2015-12-10T10:31:28.817 回答