Я не знаю ожидаемого результата, может быть, это поможет вам:
первый порядок случайным образом в группе (field_2)
выберите первое и второе поле_1 и затем выведите
with temp_shuffel as
(
select
*
from public.tbl_test
order by field_2, random()
),
temp_values as
(
select
nth_value(field_1,1) over (PARTITION by field_2 order by field_2 rows between unbounded preceding and unbounded following) as f1,
nth_value(field_1,2) over (PARTITION by field_2 order by field_2 rows between unbounded preceding and unbounded following) as f2
from temp_shuffel
)
select distinct on (f1,f2)
row1.field_1,
row1.field_2,
row1.field_4,
row2.field_1 as field2_1,
row2.field_2 as field2_2,
row2.field_4 as field2_4
from temp_values
left join public.tbl_test as row1 on row1.field_1 = temp_values.f1
left join public.tbl_test as row2 on row2.field_1 = temp_values.f2
order by f1,f2,row1.field_1