\o mock_pairs.csv
\a
\f ,
\pset footer off
\set FETCH_COUNT 100000
WITH pairs AS (
SELECT 
 	a.source_id as sid1, b.source_id as sid2,
 	a.phot_g_mean_mag as gmag1, b.phot_g_mean_mag as gmag2,
 	q3c_dist(a.ra, a.dec, b.ra, b.dec)*3600 as dist
 FROM
 	gdr3mock.generated_data as a
 	JOIN gdr3mock.generated_data as b
 	ON (
 		a.source_id!=b.source_id and
 		q3c_join(a.ra, a.dec, b.ra, b.dec, 12/3600.)) 
WHERE
 	a.phot_g_mean_mag<11

UNION ALL

SELECT 
	a.source_id as sid1, b.source_id as sid2,
	a.phot_g_mean_mag as gmag1, b.phot_g_mean_mag as gmag2,
	q3c_dist(a.ra, a.dec, b.ra, b.dec)*3600 as dist
FROM
	gdr3mock.generated_data as a
	JOIN gdr3mock.generated_data as b
	ON (
		a.source_id!=b.source_id and
		q3c_join(a.ra, a.dec, b.ra, b.dec, 5/3600.))
WHERE
	a.phot_g_mean_mag>=11
)
SELECT * FROM pairs
WHERE 
	gmag1<gmag2
	-- see README
	AND dist<0.84*(gmag2-gmag1-0.5)+0.66
