例データ:
create table event_tracking (event_id int, user_id int, event_wave int);
insert into event_tracking values
(123, 10, 3),
(123, 20, 2),
(123, 30, 1);
あなたがたevent_id = 123のためにすべてのユーザーを取得したい場合:
select count(*), max(event_wave), array_agg(user_id) users
from event_tracking
where event_id = 123;
count | max | users
-------+-----+------------
3 | 3 | {10,20,30}
(1 row)
あなたが最大event_waveとのevent_id = 123をユーザにしたい場合:
select distinct on (event_id)
row_number() over (order by event_wave) as count,
event_wave as max,
user_id
from event_tracking
where event_id = 123
order by event_id, event_wave desc;
count | max | user_id
-------+-----+---------
3 | 3 | 10
(1 row)
特定のuser_idに簡単なyes/noを設定したい場合:
select count(*), max(event_wave), 456 = any(array_agg(user_id)) yesno
from event_tracking
where event_id = 123;
または
select distinct on (event_id)
row_number() over (order by event_wave) as count,
event_wave as max,
user_id = 456 yesno
from event_tracking
where event_id = 123
order by event_id, event_wave desc;
を試してみてください。 – 3kt