If I understand correctly, you are trying to determine the server role for a specific time. Understanding that Start Time is provided but End Time is on the next line for that server. To resolve this, you need to create a temp table with the start and end time on the same line to resolve the JOIN BETWEEN operation. So it would look like this in MS SQL (Sorry, you might have to translate for Postgres)
-- This is TSQL code SQL Server 2008 compatible
create table #svr(
server_name varchar(10),
server_role varchar(10),
stime datetime
)
create table #client(
client_name varchar(10),
server_name varchar(10),
latency int,
ctime datetime
)
create table #role(
server_name varchar(10),
server_role varchar(10),
stime datetime,
etime datetime
)
insert #svr values
('server1','PRIMARY','2013-05-15 01:01:00'),
('server2','SECONDARY','2013-05-15 01:02:00'),
('server3','SECONDARY','2013-05-15 01:02:00'),
('server1','PRIMARY','2013-05-15 01:05:00'),
('server2','SECONDARY','2013-05-15 01:06:00'),
('server3','PRIMARY','2013-05-15 01:10:00'),
('server1','SECONDARY','2013-05-15 01:11:00'),
('server1','PRIMARY','2013-05-15 01:22:00'),
('server3','SECONDARY','2013-05-15 01:23:00')
insert #client values
('client1','server1',2,'2013-05-15 01:01:30'),
('client2','server2',68,'2013-05-15 01:01:40'),
('client2','server1',99,'2013-05-15 01:01:50'),
('client1','server3',5,'2013-05-15 01:10:00'),
('client2','server3',78,'2013-05-15 01:10:30'),
('client2','server1',15,'2013-05-15 01:10:50')
insert #role
select s1.server_name, s1.server_role, s1.stime, s2.stime
from ( select row_number() over(order by server_name,stime) as RowId,*
from #svr
) as s1
join ( select row_number() over(order by server_name,stime) as RowId,*
from #svr
) as s2
on s1.RowId = s2.RowId-1
select C.client_name, C.server_name, C.latency, R.server_role, C.ctime
from #client C
left join #role R on R.server_name = C.server_name
and C.ctime between R.stime and R.etime
WHERE C.latency > 60
Here's the result: